[Solved]-Django performance on querying models with many foreign keys?

12👍

select_related() is the correct solution; you’re wrong about how it’s supposed to work. I think you’re not using select_related correctly if you’re still getting multiple queries across the specified FK. A quick log of a Python session (Studio has a FK to django.auth.user here):

>>> from django.db import connection
>>> studios = Studio.objects.all().select_related('user')
>>> for studio in studios:
>>>     print studio.user.email
>>>        
email@notadomain.com
anotheremail@notadomain.com
>>> len(connection.queries) 
1

So, I got a list of Studio objects (2 in my test DB) and got the User for each one in one SQL query. Without the select_related() call, it takes three queries.

Note that select_related doesn’t handle many-to-many relationships — though I think you can manually query the intermediate table of the m2m to follow those FKs either direction without requiring the extra query, as long as you’re OK starting your queryset from the intermediate object. Maybe that’s what’s catching you? You only specified FK relationships, not m2ms, so I gave a simple illustration of that.

Leave a comment