Tips & Tricks
Making Django play nice with your database
Save your changes
No really, make sure they’re actually saved


• save() isn’t thread safe
• update() works pretty well
 • and is amazing for race conditions
 • and counters
Atomic UPDATEs
Atomic updates are great for thread safety
Atomic UPDATEs
But we didn’t want to keep typing that out
QuerySets are heavy

• no identity mapper, duplicate rows
 • django-idmapper can help
• instance creation is costly
• selects and joins are unoptimized
• joins dont scale
Partition your queries
  Joins don’t scale, and are costly...
Partition your queries
     ... and so are keystrokes
Caching is bad?
QuerySet caching is nice, but not always wanted
Instances are expensive
  Not all instances, mostly Django models


• Reddit comment model -- we pulled all
  objects into memory
• Creation of thousands of instances when
  we only need a small number
Instances are expensive
         Here’s what we mean




  Optimized to delay instance creation
Instances are expensive
                           Delaying creation until it was needed
                       700 µs
Average process time




                       525 µs

                       350 µs

                       175 µs

                         0 µs
                                       800                                 25
                                               Initial instances created


                         We didn’t even measure memory costs
                                     (and yes, this chart IS useless)
Thanks
             David Cramer

                @zeeg
           david@disqus.com
      http://www.davidcramer.net



P.S. we’re hiring kickass Django engineers
      (and some frontend guys too)
References & Stuff
•   django-idmapper
    http://github.com/dcramer/django-idmapper

•   instance.update()
    http://gist.github.com/550436

•   attach_foreignkey
    http://gist.github.com/550435

•   SkinnyQuerySet
    http://gist.github.com/550438

Db tips & tricks django meetup

  • 1.
    Tips & Tricks MakingDjango play nice with your database
  • 2.
    Save your changes Noreally, make sure they’re actually saved • save() isn’t thread safe • update() works pretty well • and is amazing for race conditions • and counters
  • 3.
    Atomic UPDATEs Atomic updatesare great for thread safety
  • 4.
    Atomic UPDATEs But wedidn’t want to keep typing that out
  • 5.
    QuerySets are heavy •no identity mapper, duplicate rows • django-idmapper can help • instance creation is costly • selects and joins are unoptimized • joins dont scale
  • 6.
    Partition your queries Joins don’t scale, and are costly...
  • 7.
    Partition your queries ... and so are keystrokes
  • 8.
    Caching is bad? QuerySetcaching is nice, but not always wanted
  • 9.
    Instances are expensive Not all instances, mostly Django models • Reddit comment model -- we pulled all objects into memory • Creation of thousands of instances when we only need a small number
  • 10.
    Instances are expensive Here’s what we mean Optimized to delay instance creation
  • 11.
    Instances are expensive Delaying creation until it was needed 700 µs Average process time 525 µs 350 µs 175 µs 0 µs 800 25 Initial instances created We didn’t even measure memory costs (and yes, this chart IS useless)
  • 12.
    Thanks David Cramer @zeeg david@disqus.com http://www.davidcramer.net P.S. we’re hiring kickass Django engineers (and some frontend guys too)
  • 13.
    References & Stuff • django-idmapper http://github.com/dcramer/django-idmapper • instance.update() http://gist.github.com/550436 • attach_foreignkey http://gist.github.com/550435 • SkinnyQuerySet http://gist.github.com/550438