Db tips & tricks django meetup

8,217 views

Published on

Published in: Technology
1 Comment
15 Likes
Statistics
Notes
No Downloads
Views
Total views
8,217
On SlideShare
0
From Embeds
0
Number of Embeds
88
Actions
Shares
0
Downloads
109
Comments
1
Likes
15
Embeds 0
No embeds

No notes for slide













  • 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 david@disqus.com http://www.davidcramer.net P.S. we’re hiring kickass Django engineers (and some frontend guys too)
    13. 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

    ×