Reducing the query count in Django applications

4,887 views

Published on

Published in: Technology
1 Comment
8 Likes
Statistics
Notes
  • I really like the bit of code that you provided on slide 6 regarding caching. However, what do you mean 'single instance'. After implementing this change, I've been having problems seeing any updates to the queries on which the cashing was applied. I figured single instance referred to each page load - but does it mean each session? I'm just trying to figure out when I can use this technique. Thanks again for the slides. It's hard to find good references on query optimization for django so this slideshow is much appreciated.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
4,887
On SlideShare
0
From Embeds
0
Number of Embeds
33
Actions
Shares
0
Downloads
82
Comments
1
Likes
8
Embeds 0
No embeds

No notes for slide
















  • Reducing the query count in Django applications

    1. Reducing the query count Daniel Roseman
    2. Why look at the query count? • Query count is only one factor in performance • But an important one • Number of easy tricks to reduce the count • Can have a major impact on your app • In one case, 3000 queries went down to 8
    3. Monitoring • connection.queries • Debug toolbars • QueryAnalysis middleware - aggregates common queries
    4. Low hanging fruit • Go for repeated queries first • select_related - use sparingly, but can have a big impact in the right place • Avoid ORM calls in loops: get all items first, then loop through them • Be careful of hidden calls within templates
    5. Pre-processing • Denormalisation • Save complex data as separate fields
    6. ‘Cache’ within instance • Useful for expensive lookups that will be called several times on a single instance def get_assets(self): if not hasattr(self, '_assets'): self._assets = self.assets.all() return self._assets • Could be a property
    7. Generic relations • select_related doesn’t work • Get all related instances, and store in a dict • Again, can stash within model instance
    8. Generic relations shared_objects = Shared_Objects.objects.select_related('category').filter( pagemapping__in=article_ids, content_type=article_type, ) sharedobj_dict = dict((so.object_id, so) for so in shared_objects) articles = Article.objects.filter( id__in=sharedobj_dict.keys()) for article in articles: article._shared = sharedobj_dict[article.id]

    ×