On Sun, 2008-12-07 at 18:09 -0800, Dave Dash wrote:
> Without knowing too much about your code, the only thing I can say,
> having had a similar issue, was that select_related witha  specified
> depth generally made for more efficient queries.

This is quite true. Also remember that if you know which related models
you really want, passing those into select_related() is even more
efficient.

However, in the original poster's case, this isn't relevant, since he is
querying across a reverse ForeignKey (the "foo_set" stuff is the clue)
and select_related() has no effect whatsoever for ManyToManyFields or
reverse ForeignKeys (anything that returns potentially multiple values
does not benefit from select_related()).

In fact, the original poster's question is quite hard to write an
efficient group of queries for. It's actually a hard SQL problem, not a
hard Django problem. I started thinking about it on the train last night
and haven't arrived a final solution yet. It almost falls out with a
clever "group by", but not quite.

[...]
> Another strategy that worked for me, is having an *ideal* SQL query
> and then trying to fit that into the Django way of grabbing objects.
> I already knew the most efficient query to use, so I had to just hack
> around until Django did what I want.

This is another very good point. There's a large group of problems that
are pure SQL issues (and Erik's case certainly falls into that as well).
Solving them in SQL is definitely going to be the easiest solution.
Whether you ultimately end up writing a direct SQL call
(cursor.execute(...)) or finding an equivalent way to use the Django ORM
to express it is personal preference sometimes. But realising that you
are trying to solve an SQL problem and thinking about it in the right
terms is half the game.

So, yes, good reply. Thinking about the problem solution at the right
level is important.

Cheers,
Malcolm


--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to