Hi Jani, That's a correct diagnosis. I knew Django was doing lazy loading, but didn't fully understand the implications.
In my case I have a filtered and ordered queryset resulting in ~400 records. Upon processing in the template, each was prompting a database visit 3 times when outputting information: hence the ~1200 trips to the database. I've added prefetch_related specifying the related models I'm working with in the template. My 'slow' case has now become: 46 queries in 39 ms, Time: 506 ms: a factor of 20 speed improvement. Thanks for the suggestion! R. On Sunday, January 4, 2015 10:09:58 PM UTC-8, Jani Tiainen wrote: > By judging amount of queries of your "slow" page you probably have > model(s) with foreign keys that you lazy load - which means that each fk is > loaded individually from the database with separate query. > > For example if you have a model that contains 4 foreign keys and you query > 100 instances (rows) you would actually invoke 1 + 4 * n queries, which in > example case would be 401. > > Without actually knowning your code one of the first options you usually > do to optimize queries is to use select_related and prefetch_related > queryset methods to cut down number of queries needed. > > -- > > Jani Tiainen > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3e081983-0d18-4197-8d7a-b78b069bfbd6%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.

