Skip to content

Commit

Permalink
Improved documentation of when QuerySet.exists() and .count() are a g…
Browse files Browse the repository at this point in the history
…enuine optimization.

Also fixed a typo.



git-svn-id: http://code.djangoproject.com/svn/django/trunk@11960 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
spookylukey committed Dec 22, 2009
1 parent 5160212 commit a5af81f
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions docs/ref/models/querysets.txt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1037,7 +1037,8 @@ Example::


``count()`` performs a ``SELECT COUNT(*)`` behind the scenes, so you should ``count()`` performs a ``SELECT COUNT(*)`` behind the scenes, so you should
always use ``count()`` rather than loading all of the record into Python always use ``count()`` rather than loading all of the record into Python
objects and calling ``len()`` on the result. objects and calling ``len()`` on the result (unless you need to load the
objects into memory anyway, in which case ``len()`` will be faster).


Depending on which database you're using (e.g. PostgreSQL vs. MySQL), Depending on which database you're using (e.g. PostgreSQL vs. MySQL),
``count()`` may return a long integer instead of a normal Python integer. This ``count()`` may return a long integer instead of a normal Python integer. This
Expand Down Expand Up @@ -1140,8 +1141,11 @@ Aggregation <topics-db-aggregation>`.
Returns ``True`` if the :class:`QuerySet` contains any results, and ``False`` Returns ``True`` if the :class:`QuerySet` contains any results, and ``False``
if not. This tries to perform the query in the simplest and fastest way if not. This tries to perform the query in the simplest and fastest way
possible, but it *does* execute nearly the same query. This means that calling possible, but it *does* execute nearly the same query. This means that calling
:meth:`QuerySet.exists()` is faster that ``bool(some_query_set)``, but not by :meth:`QuerySet.exists()` is faster than ``bool(some_query_set)``, but not by
a large degree. a large degree. If ``some_query_set`` has not yet been evaluated, but you know
that it will be at some point, then using ``some_query_set.exists()`` will do
more overall work (an additional query) than simply using
``bool(some_query_set)``.


Field lookups Field lookups
------------- -------------
Expand Down

0 comments on commit a5af81f

Please sign in to comment.