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
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
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