Yes, this conversation hits the nail on the head.

'select related' only works with forward facing keys.

http://code.google.com/p/django-selectreverse/ is a resonably good
piece of code to improve performance:

using a structure like:

class model1(model.Models)   1<----* class model2(model.Models)
                                                                model1
= models.foreignkey(model1)
                                                1<----* class
model3(model.Models)
                                                                model1
= models.foreignkey(model1)
                                                etc...

where your view/template code is:

for x in model1.objects.all()
    #stuff
    for y in x.model2_set.all()
        #stuff
    for y in x.model3_set.all()
        #stuff

Gets awfully slow as you add modelX_set.all or scale it up to thousands of rows.

eg:
where model1 is 945 rows in the sql db:

time taken:  1.023
SQL queries:  1883

using select related:

time taken:  0.493
SQL queries:  21

I have not tried select related performance where there may be another
model with a reverse foreignkey facing model2.
I don't know how well it will face a multi level reverse foreign
key/many to many db structure.

Also don't forget to look at indexing for certain fields where
appropriate it can be a good performance improver:

http://docs.djangoproject.com/en/1.1/ref/models/fields/#db-index

cheers
sam_w




On Fri, Mar 19, 2010 at 5:15 AM, koenb <koen.bierm...@werk.belgie.be> wrote:
> On 18 mrt, 10:12, bruno desthuilliers <bruno.desthuilli...@gmail.com>
> wrote:
>> On Mar 18, 8:42 am, koenb <koen.bierm...@werk.belgie.be> wrote:
>>
>> > Take a look at something like django-selectreverse [1] for inspiration
>> > on how to reduce your querycount for M2M relations.
>>
>> > Koen
>>
>> > [1]http://code.google.com/p/django-selectreverse/
>>
>> I must be missing the point, but it looks seriously like reinventing
>> the wheel ? How is it better than using select_related ???
>
> Select_related only populates forward one-to-many relationships
> (foreignkeys).
> It does not work for many-to-many relationships or reverse one-to-many
> relationships (the XX_set descriptors on the other side of fk's).
> You can not load those in one and the same query, but you can load all
> those objects in one single query instead of N extra queries.
> That is what selectreverse helps you to do. Look at the example on the
> introduction page of the project to see what kind of queries I mean.
>
> Koen
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To post to this group, send email to django-us...@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.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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