>> offers = Offer.objects.all() >> for term in ('ThemePark', 'London'): >> offers = offers.extra(where=[""" >> EXISTS ( >> SELECT 0 >> FROM app_offer_term ot >> ON o.id = ot.offer_id >> INNER JOIN app_term t >> ON ot.term_id = t.id >> WHERE app_offer.id = ot.offer_id >> AND t.term = %s >> )"""], params=[term]) >> > > please, could someone explain to me: > - what does "SELECT 0" do?
"SELECT 0" just returns *something*. The EXISTS clause only cares if any *rows* were returned, not what their content was. It could just as easily have been "SELECT *" or "SELECT 'frobniculator'" or anything else. However, if the database doesn't optimize the call, anything more than one column and anything requiring a lookup into a table (such as a column value) is extra effort/cycles. Thus, "SELECT 0" is a suggestion to the database that if it has to bring something back, a single non-calculated common constant will suffice. A minor optimization, and I find it helpful as a reminder to myself that I don't really care about the contents of the the SELECT statement, just that *something* came back. > - and "ON o.id = ot.offer_id"? Is it needed? Hmm...looks like a bogus copy&paste on my part; a remnant of my testing. With that line, it's bad SQL. It should just be ... FROM app_offer_term ot INNER JOIN app_term t ON ot.term_id = t.id ... -tim PS: you sent your reply directly to me, rather than to the mailing list. In this case, I could answer your question, but in general, you may want to reply to the list so that others can help too as it's pretty easy to hit the limits of my answers. :) I've replied CC'ing the ML so that in case others have the same questions you did, they can benefit from the answers too. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---