Reducing the query count in Django applications

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

1 comments

Comments 1 - 1 of 1 previous next Post a comment

  • + joejaz joejaz 5 months ago
    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.
Post a comment
Embed Video
Edit your comment Cancel

5 Favorites

Reducing the query count in Django applications - Presentation Transcript

  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]
SlideShare Zeitgeist 2009

+ danielrosemandanielroseman Nominate

custom

1307 views, 5 favs, 0 embeds more stats

More info about this document

© All Rights Reserved

Go to text version

  • Total Views 1307
    • 1307 on SlideShare
    • 0 from embeds
  • Comments 1
  • Favorites 5
  • Downloads 39
Most viewed embeds

more

All embeds

less

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

Cancel
File a copyright complaint
Having problems? Go to our helpdesk?

Categories