Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Db tips & tricks django meetup


Published on

Published in: Technology

Db tips & tricks django meetup

  1. 1. Tips & Tricks Making Django play nice with your database
  2. 2. 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
  3. 3. Atomic UPDATEs Atomic updates are great for thread safety
  4. 4. Atomic UPDATEs But we didn’t want to keep typing that out
  5. 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. 6. Partition your queries Joins don’t scale, and are costly...
  7. 7. Partition your queries ... and so are keystrokes
  8. 8. Caching is bad? QuerySet caching is nice, but not always wanted
  9. 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. 10. Instances are expensive Here’s what we mean Optimized to delay instance creation
  11. 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. 12. Thanks David Cramer @zeeg P.S. we’re hiring kickass Django engineers (and some frontend guys too)
  13. 13. References & Stuff • django-idmapper • instance.update() • attach_foreignkey • SkinnyQuerySet