On Tue, Mar 13, 2012 at 7:57 AM, Virginia <[email protected]> wrote:
> We add datas but not that much to slow it down so
> much.

this is usually not about data itself, being heavy but about having a
bad algorithmic behavior.

the most common culprit is having unexpected queries inside a loop.
for example:

{% for msg in message.objects.all %}
    {{ msg.author.get_full_name}}
    {{ msg.text }}
{% endfor %}

sounds clean, no?  but if the 'author' field is a ForeignKey, getting
the full name might do a new query for each message.  then you have
the  awful "N+1 queries" problem (1 to get all messages, and N for all
the users).  in this case it's easy to add 'select_related' to the
first line.

a slightly harder case (but almost as common) is when you have nested
loops (say, listing all books for each author).  here select_related
usually won't help; you have to reformulate the loop.  typically you
can list all books sorted by author and insert an {% ifchanged
book.author%} within the loop to display the author info at the
appropriate places.

another one that bite me is fetching related fields in the __unicode__
function, this makes a simple list (or popup menu) increasingly heavy.
 it could be fixed with select_related, just like the first case; but
if if happens in the admin pages, you might not be able to add it
where needed.  fortuately, the admin objects can be annotated to make
the necessary prefetching.

finally, to find this before going to production, you just have to
check on the number of queries per page. it's not important to cut
everything down to just one or two; just check that the total number
of queries doesn't grow when the data grows.  For this a great tool is
the django_bebug_toolbar, it clearly shows every query used to
generate each page.

-- 
Javier

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

Reply via email to