On May 7, 5:56 pm, Eric Chamberlain <e...@rf.com> wrote:
> Our view is taking too long to render, can anyone offer suggestions to  
> improve its speed?
>
> There are 5830 profiles.
>
> Total query count: 23809
> Total duplicate query count: 493
> Total SQL execution time: 66.003
> Total Request execution time: 142.931

<snip models>

>
> profiles =  
> Profile
> .objects
> .filter(partner=request.partner,user__is_active=True).order_by('-
> user__date_joined')
> for p in profiles:
>        p.provider_list = list(
>            account.provider for account in  
> ProviderAccount
> .objects
> .filter(user=p.user,provider__provider_partners=request.partner))
>        p.call_count = p.call_records.filter().count()
> return PartnerResponse(request, {'profiles': profiles})
>
> PartnerResponse returns the view to the user, the template uses a for  
> loop to iterate through the profiles.


As it happens, reducing query counts is something of an obsession of
mine at the moment - just gave a talk at EuroDjangoCon on this very
subject.

In your case, the first thing to do is take that ProviderAccount query
out of the loop. What I would do is first get a list of all provider
user_ids, then query all ProviderAccounts with those users. You can
then put them into a dictionary keyed by user_id and then loop through
to re-associate them with the profile.

Something like this (untested):

profiles = Profile.objects.filter(partner=request.partner,
    user__is_active=True).order_by('-user__date_joined')
user_ids = [p.user_id for p in profiles]

accounts = ProviderAccount.objects.filter(user__in=user_ids,
    provider__provider_partners=request.partner)
account_dict = dict([(pa.user_id, pa) for pa in accounts])

for p in profiles:
    p.provider_list = account_dict[p.user_id]

You will probably need to tweak this - it doesn't take account of the
fact that a user might be associated with multiple ProviderAccounts -
but hopefully you get the idea, you should try and get all your
accounts in one big query and *then* associate them with the relevant
profiles.

The other thing to look at is the proper use of select_related. You
might find that doing Profile.objects.select_related().filter
(whatever) has a big impact on reducing the count.

Finally, you have not stated what call_records is, it doesn't appear
to come from anywhere in this code, so I don't know if that's also the
cause of some big queries.

--
DR.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to