Good morning Javier,

Thanks a lot for the quick answer. The thing is that I'm having the problem
on the admin page, it is not involving any coding from my part what so
ever. So that is why I don't get it. When you say "the admin objects can be
annotated to make
the necessary prefetching." what do you mean by that? May I have an example
please. Another thing, I installed the django_debug_toolbar and I see that
in the sql part I have 1852 queries in 18228.00ms. This is a lot,
considering my object:


class TmyObject(models.Model):
    STAT_CHOICES = (
                           (u'OPERATING', u'OPERATING'),
                           (u'Not Ready', u'Not Ready'),
                    )

    name = models.CharField('CI Name', max_length=150)
    versionid = models.ForeignKey(TrProductsVersion, verbose_name="The
product", blank=True, null=True)
    parent = models.ForeignKey('self', blank=True, null=True,
verbose_name="Parent class")
    supported = models.BooleanField('CI is Supported?')
    status = models.CharField('CI Status', max_length=20,
choices=STAT_CHOICES)
    description = models.TextField('Object description', blank=True,
null=True)
    locationid = models.ForeignKey(TLocation, verbose_name="Location",
blank=True, null=True)
    clientservices = models.ManyToManyField(TrRelatedServ,
through='TrRelatedService', verbose_name="Related service")

    def __unicode__(self):
        return u'%s' %self.name

    class Meta:
        verbose_name = "Object"
        ordering = ['name']


The "TrProductsVersion" has 2 foreignkey, "TLocation" has no foreign key
and "TrRelatedService" has a 2 foreignkey (one on TmyObject and one that
the object has several other foreign keys). How can I reduce the queries
numbers? It is too much...

Regards,
VB


2012/3/13 Javier Guerra Giraldez <[email protected]>

> 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.
>
>

-- 
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