#4739: len(queryset) is slow
--------------------------------------------+-------------------------------
Reporter:
tcl...@umd.com.au | Owner: adrian
Status: closed | Component: Database wrapper
Version: SVN | Resolution: wontfix
Keywords: queryset, len, count, select | Stage: Unreviewed
Has_patch: 1 | Needs_docs: 0
Needs_tests: 0 | Needs_better_patch: 0
--------------------------------------------+-------------------------------
Changes (by mtredinnick):
* status: new => closed
* resolution: => wontfix
Comment:
Your solution requires a second database query, which isn't so great.
If a user ''only'' wants the size of a set of results, they can call
count() explicitly. If they are calling len() on the queryset, then
they've already created the queryset for other reasons, so actually using
the results we've already queried is efficient (recalling that the
queryset is going to cache the results anyway).
All that being said, one enhancement that will be introduced soon is to
speed up len() for most database backends by using the cursor's rowcount
attribute and (only if that isn't implemented) then falling back to the
size of the result set.
So, yes, we can make len() slightly more efficient for most database
backends (not SQLite, but life's like that sometimes), but it isn't going
to be by making another database call.
--
Ticket URL: <
http://code.djangoproject.com/ticket/4739#comment:2>