Google Groups Home
Help | Sign in
select_related additions
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
David Cramer  
View profile
 More options Jul 30 2007, 3:39 pm
From: David Cramer <dcra...@gmail.com>
Date: Mon, 30 Jul 2007 19:39:50 -0000
Local: Mon, Jul 30 2007 3:39 pm
Subject: select_related additions
I have taken a bit of time to rewrite part of the QuerySet code
(including select_related). Please, no lectures about a QuerySet
refactoring.

The changes force .filter() to take advantage of select_related.

Before: .filter(mykey__name='Hello') would give you results, but if
you didn't do .select_related(depth=1) you would not have mykey in
memory.
After: it does :P

The changes also change select_related's args:
MyModel.objects.filter(mykey__name="hello").select_related('mykey__author')
will automatically join on mykey__author and keep it in memory.

The biggest thing this fixes is the SQL query optimization, previously
it would have done two JOINs on mykey's table, even if the SQL engine
optimized it, it's best to optimize it in the code.

This also letse you say
MyModel.objects.all().select_related('mykey__author') which will hold
both mykey, and mykey__author values in memory on the objects.

The depth argument still exists, so
MyModel.objects.all().select_related('mykey', depth=1). The result of
this query.. is nothing, because you implicitly set the fields to
"mykey". If however, you said select_related('mykey__author', depth=1)
it would not select on author, only on mykey, because that was
specified.

If people are willing to discuss, or accept this, I will patch SVN
with it. I will still most likely release the patch as I know a LOT of
people want the "fields" selector in select_related().


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David Cramer  
View profile
 More options Aug 1 2007, 12:17 pm
From: David Cramer <dcra...@gmail.com>
Date: Wed, 01 Aug 2007 16:17:39 -0000
Local: Wed, Aug 1 2007 12:17 pm
Subject: Re: select_related additions
I've submitted a ticket and attached part of the patch (the fields
selection on select_related): http://code.djangoproject.com/ticket/5020

On Jul 30, 12:39 pm, David Cramer <dcra...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michael Radziej  
View profile
 More options Aug 1 2007, 12:26 pm
From: Michael Radziej <m...@noris.de>
Date: Wed, 1 Aug 2007 18:26:27 +0200
Local: Wed, Aug 1 2007 12:26 pm
Subject: Re: select_related additions

On Wed, Aug 01, David Cramer wrote:

> I've submitted a ticket and attached part of the patch (the fields
> selection on select_related): http://code.djangoproject.com/ticket/5020

Funny, I'm working on something similar.

I don't understand how the new API looks like, and particularily how this
could work:

  def select_related(self, *args, **kwargs):
    ...
    return self._clone(_select_related=true_or_false,
            _max_related_depth=depth, _recurse_fields=fields)

I don't see how `fields` gets populated. Can you please help with a
pointer?

Michael

--
noris network AG - Deutschherrnstraße 15-19 - D-90429 Nürnberg -
Tel +49-911-9352-0 - Fax +49-911-9352-100
http://www.noris.de - The IT-Outsourcing Company

Vorstand: Ingo Kraupa (Vorsitzender), Joachim Astel, Hansjochen Klenk -
Vorsitzender des Aufsichtsrats: Stefan Schnabel - AG Nürnberg HRB 17689


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David Cramer  
View profile
 More options Aug 1 2007, 8:12 pm
From: David Cramer <dcra...@gmail.com>
Date: Thu, 02 Aug 2007 00:12:40 -0000
Local: Wed, Aug 1 2007 8:12 pm
Subject: Re: select_related additions
Seems I slipped in the diff. I have to actually apply the patch to at
least 2 different versions of Django with our current setup, can get
tedious :P

*args should be *fields :)

On Aug 1, 9:26 am, Michael Radziej <m...@noris.de> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michael Radziej  
View profile
 More options Aug 2 2007, 5:28 am
From: Michael Radziej <m...@noris.de>
Date: Thu, 2 Aug 2007 11:28:45 +0200
Local: Thurs, Aug 2 2007 5:28 am
Subject: Re: select_related additions

On Thu, Aug 02, David Cramer wrote:

> Seems I slipped in the diff. I have to actually apply the patch to at
> least 2 different versions of Django with our current setup, can get
> tedious :P

> *args should be *fields :)

;-)

Well, for the deeper levels, is there a way to express "follow
myforeignkey__user recursively" and also "follow myforeignkey__user, but no
deeper" without using the depth parameter (because you might want to follow
to different depths in different foreign keys)?

Michael

--
noris network AG - Deutschherrnstraße 15-19 - D-90429 Nürnberg -
Tel +49-911-9352-0 - Fax +49-911-9352-100
http://www.noris.de - The IT-Outsourcing Company

Vorstand: Ingo Kraupa (Vorsitzender), Joachim Astel, Hansjochen Klenk -
Vorsitzender des Aufsichtsrats: Stefan Schnabel - AG Nürnberg HRB 17689


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David Cramer  
View profile
 More options Aug 2 2007, 11:22 am
From: David Cramer <dcra...@gmail.com>
Date: Thu, 02 Aug 2007 15:22:20 -0000
Local: Thurs, Aug 2 2007 11:22 am
Subject: Re: select_related additions
This was my best solution. I honestly wouldnt ever encourage anything
where you dont specify which foreignkeys to follow. JOINs can get very
slow when they expand beyond 2 or 3 tables, especially when table
sizes increase.

On Aug 2, 2:28 am, Michael Radziej <m...@noris.de> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michael Radziej  
View profile
 More options Aug 2 2007, 11:54 am
From: Michael Radziej <m...@noris.de>
Date: Thu, 2 Aug 2007 17:54:48 +0200
Local: Thurs, Aug 2 2007 11:54 am
Subject: Re: select_related additions

On Thu, Aug 02, David Cramer wrote:

> This was my best solution. I honestly wouldnt ever encourage anything
> where you dont specify which foreignkeys to follow. JOINs can get very
> slow when they expand beyond 2 or 3 tables, especially when table
> sizes increase.

Agreed ;-) I even think the max_depth argument should be removed, it doesn't
really make sense.

I have something similar that is able to correctly generate outer joins for
nullable foreign keys. It's currently only for my use since it's probably
mysql only, and it can't correctly nest joins if you need an inner join to
an outer join (hope this makes sense to you). It's also written for a
different API (but I prefer your idea).

I've attached it for a quick look, are you interested in joining our
efforts?

Michael

--
noris network AG - Deutschherrnstraße 15-19 - D-90429 Nürnberg -
Tel +49-911-9352-0 - Fax +49-911-9352-100
http://www.noris.de - The IT-Outsourcing Company

Vorstand: Ingo Kraupa (Vorsitzender), Joachim Astel, Hansjochen Klenk -
Vorsitzender des Aufsichtsrats: Stefan Schnabel - AG Nürnberg HRB 17689

  select_related.diff
9K Download

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google