#26172: Don't query the database when filtering an already empty queryset
----------------------------------------------+---------------------------
Reporter: seddonym | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Database layer (models, ORM) | Version: 1.9
Severity: Normal | Keywords: orm, queryset
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+---------------------------
If you have a queryset that has already been evaluated as empty, and you
filter (or exclude) it, Django will hit the database again, even though
the result will always be an empty queryset.
{{{
>>> queryset = MyModel.objects.filter(my_field='foo')
>>> bool(queryset)
# Hits the database - running bool will cache it
False
>>> queryset
[]
>>> queryset.filter(another_field='bar')
# Hits the database
[]
>>> queryset.exclude(another_field='bar')
# Hits the database
[]
}}}
The ORM should check to see if the queryset is empty before running any
further filters / excludes pointlessly.
--
Ticket URL: <https://code.djangoproject.com/ticket/26172>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/051.9247396551cc1f3abd9dbc2bcbc1afd2%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.