On Sep 25, 9:08 am, Jani Tiainen <rede...@gmail.com> wrote:
> Chris Withers kirjoitti:
>
> > Brian McKeever wrote:
> >> .count is definitely the way to go. Although, I would probably pass it
> >> to your template instead of determining it there.
>
> > What difference does it make?
>
> len(qs) evaluates queryset - thus pulling in _every_ object from DB to
> Python - which you might not want specially if queryset is large.
>
> .count() executes count query on DB side returing only single scalar
> value to Python.
>
> Figure out which one is faster...
>
> --
> Jani Tiainen

Beware that it's not always as clear-cut as you might think. Django is
clever enough that if you've already evaluated the queryset, .count()
uses len() internally rather than going back to the database. But if
you need the count *before* evaluating the queryset, but will still
need to evaluate it later, using .count() initially will result in an
unnecessary database call. If you care about that level of
optimisation, it's worth bearing in mind.
--
DR.


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