[Django] #34158: Optimise count() for querysets that contain field lookups

3 views
Skip to first unread message

Django

unread,
Nov 15, 2022, 2:25:29 AM11/15/22
to django-...@googlegroups.com
#34158: Optimise count() for querysets that contain field lookups
-------------------------------------+-------------------------------------
Reporter: Irfan | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Database | Version: 4.1
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
[https://github.com/mo3rfan/django-qs-
count/blob/main/test_orm/testapp/tests.py#L31-L32 These two lines are
equivalent in terms of the count], yet [https://github.com/mo3rfan/django-
qs-count/actions/runs/3467684164/jobs/5792783131#step:3:351 one is much
slower than the other].

Is there a way Django's {{{count()}}} can be optimised for such querysets?

This becomes especially useful for
[https://docs.djangoproject.com/en/4.1/ref/paginator/ Paginators] where
{{{count()}}} is used
[https://github.com/django/django/blob/4.1.3/django/core/paginator.py#L78
internally], and such querysets with large number of rows containing
foreign key field lookups won't slowdown pagination due to the unnecessary
joins.

--
Ticket URL: <https://code.djangoproject.com/ticket/34158>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 15, 2022, 2:44:19 AM11/15/22
to django-...@googlegroups.com
#34158: Optimise count() for querysets that contain field lookups
-------------------------------------+-------------------------------------
Reporter: Irfan | Owner: nobody
Type: | Status: closed
Cleanup/optimization |
Component: Database layer | Version: 4.1
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => invalid


Comment:

> Is there a way Django's {{{count()}}} can be optimised for such
querysets?

Not really. Moreover, this is a support question and Trac is not a support
channel. Closing per TicketClosingReasons/UseSupportChannels.

> This becomes especially useful for
[https://docs.djangoproject.com/en/4.1/ref/paginator/ Paginators] where
{{{count()}}} is used
[https://github.com/django/django/blob/4.1.3/django/core/paginator.py#L78
internally], and such querysets with large number of rows containing
foreign key field lookups won't slowdown pagination due to the unnecessary
joins.

See #23771 for some ideas on how to optimize the `Paginator` case.

--
Ticket URL: <https://code.djangoproject.com/ticket/34158#comment:1>

Reply all
Reply to author
Forward
0 new messages