On Wed, 2008-10-15 at 18:21 -0500, Tim Chase wrote:
> > i have to check the method's return value first with an 'if'
> > tag than run the query again for the 'for' loop. this doubles
> > the load on the db what i don't like, not to mention that the
> > two queries may return different lists.
> 
> Django's ORM should cache your query results, though it may need 
> to be primed with your results first (if the query object doesn't 
> already have results, it issues a COUNT(*) call first, and then 
> your request for results makes for a second call; but if they 
> were done in the other order, the len/bool testing would hit the 
> internal warmed cache first). 

This isn't quite correct. Because the count(*) call could be quite
expensive (any query could be expensive), we avoid making any extra call
like that. Since was already have the queryset at this point, Django
implements a boolean test by reading the first few results from the
result set (i.e. starting to fill the cache). If there are any results,
it can return "True" and since you're almost certain to then actually be
using the queryset, the results are already in memory. It doesn't read
all the results at once, but it reads a chunk (100 at a time).

So it's correct that there will still only be one database call here,
due to the caching nature of querysets. But there will *always* be only
one database call, regardless of when you do the boolean test on the
queryset.

Regards,
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