Hi Richard,

Like you've seen, the _number_ of queries often has a large effect on speed.

Also, try comparing your page load time with and without debug toolbar 
enabled. I found that DDT is slow itself :).

Collin

On Monday, January 5, 2015 at 10:19:44 AM UTC-5, Richard Brockie wrote:
>
> 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/eb15e0ab-2f12-42c1-8971-84f3361368be%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to