On Mon, Feb 23, 2009 at 4:10 PM, Sean Patrick Hogan <spho...@gmail.com>wrote:

> It isn't *an* extra SQL query that I'm worried about.  It's that it's going
> to be about 2,000 extra SQL queries in a loop.  If it were just constructing
> a page with one item and then its related items, that's not a problem to do
> two queries, but when you're looping over 2,000 items and running an extra
> query for all 2,000, it becomes a large performance issue.  When you're
> dealing with that many items, it does affect the viability of your
> application.
>
> Rails can do this.  However, the problem isn't a simple one to solve.  I'll
> probably look at their code sometime later this week.  Going back to my
> example, SQL is going to give you the list of tuples of scalars as Aaron
> notes.  This isn't a problem that SQL can solve since an SQL result set will
> look like a spreadsheet and (for obvious reasons) you just can't have many
> related objects shown in a spreadsheet.
>
> BUT, you can iterate over objects in python.  So, let's say I had this
> result set:
> Article1, Comment1
> Article 1, Comment 4
> Article 1, Comment 8
> Article 2, Comment 3
> Article 2 Comment5
>
> Ugly!  But I could loop over that and aggregate them together as such:
>
> final_results = []
> for item in results:
>   if item.pk != last_item.pk:
>     final_results.append(item)
>   item.comment_set = []
>   item.comment_set.append(#a comment object from those columns)
>   last_item = item
>
> Basically, each loop you look to see if it's the same article and so you
> realise that you should only be appending the comment to the comments list.
> That seems easy enough, but then what do you do when you have multiple
> relations you're trying to get in the select_related?  It gets a lot harder.
>
> Anyway, I'll look over the Rails source sometime later this week and see if
> there is a simple enough pattern to port or maybe just hack a simple case
> through for single calls for the reverse relationship which isn't so hard.
>
> One thing that would be nice, however, is if select_related() would raise
> an exception when you tried to call it and passed in a name that wasn't a
> foreign key (and therefore it couldn't get) so that you'd get an error
> message rather than having that error message pass silently.
>
> Errors should never pass silently.
> Unless explicitly silenced.
> ~Zen of Python
>
> Well, I guess I have a project for this week!
> Sean.
>
>
>
> On Mon, Feb 23, 2009 at 3:55 PM, Alex Gaynor <alex.gay...@gmail.com>wrote:
>
>>
>>
>> On Mon, Feb 23, 2009 at 3:49 PM, Torsten Bronger <
>> bron...@physik.rwth-aachen.de> wrote:
>>
>>>
>>> Hallöchen!
>>>
>>> Alex Gaynor writes:
>>>
>>> > On Mon, Feb 23, 2009 at 12:40 PM, sphogan <spho...@gmail.com> wrote:
>>> >
>>> >> [...]
>>> >>
>>> >> However, select_related() only works on the object the foreign
>>> >> key is declared on
>>> >> (http://docs.djangoproject.com/en/dev/ref/models/querysets/ #id4
>>> >> <http://docs.djangoproject.com/en/dev/ref/models/querysets/%0A#id4>;
>>> >> specifically, "You can only refer to ForeignKey relations in the
>>> >> list of fields passed to select_related.")  Presumably this is
>>> >> because it does an INNER JOIN rather than a LEFT OUTER JOIN and
>>> >> would therefore miss articles with no comments.
>>> >>
>>> >> What I would like to be able to do is:
>>> >>
>>> >> Article.objects.select_related('comment_set').all()
>>> >>
>>> >> This seems like a somewhat simple case and there seems to be no
>>> >> mention of it in the documentation anywhere.
>>> >
>>> > That's not possible,
>>>
>>> By principle, or just because Django can't do it (yet)?  The related
>>> feature request can be found at
>>> <http://code.djangoproject.com/ticket/2238>.  It it, I refer to
>>>
>>> http://rubynugs.blogspot.com/2008/07/couple-of-stupid-things-about-djano.html
>>> where a Rails fan claims that Rails can do it.
>>>
>>> I don't know much about SQL so I really would like to know whether
>>> this could be realised.
>>>
>>> Tschö,
>>> Torsten.
>>>
>>> --
>>> Torsten Bronger, aquisgrana, europa vetus
>>>                   Jabber ID: torsten.bron...@jabber.rwth-aachen.de
>>>
>>>
>>>
>>>
>> I have no idea if it's possible, by my inclination is that it isn't since
>> no one seems to have ponied up the SQL to make it happen :) .  Further SQL
>> generally returns a list of tuples of scalars(to use there python datatypes)
>> SQL doesn't really ever return a collection, so my inclination is that it
>> can't.  Someone better versed in SQL can probably say for sure.  Regardless
>> it's still easy to get the data:
>>
>> p =Post.object.get(pk=2)
>> p.comment_set.all() # all the comments for that post.
>>
>> Alex
>> --
>> "I disapprove of what you say, but I will defend to the death your right
>> to say it." --Voltaire
>> "The people's good is the highest law."--Cicero
>>
>>
>>
>
> >
>
Sean,

If you figured out how exactly the SQL that makes the eager loading of
reverse relations possible I'm sure there would be quite a few people
interested in making it possible in Django(myself among them, though not
because I have a use, I just like puzzles :) ).

Alex

-- 
"I disapprove of what you say, but I will defend to the death your right to
say it." --Voltaire
"The people's good is the highest law."--Cicero

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