Opened 4 years ago

Closed 4 years ago

#34158 closed Cleanup/optimization (invalid)

Optimise count() for querysets that contain field lookups

Reported by: Irfan Owned by: nobody
Component: Database layer (models, ORM) Version: 4.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

​These two lines are equivalent in terms of the count, yet ​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 ​Paginators where count() is used ​internally, and such querysets with large number of rows containing foreign key field lookups won't slowdown pagination due to the unnecessary joins.

Change History (1)

in reply to:  description comment:1 by Mariusz Felisiak, 4 years ago

Resolution: → invalid
Status: new → closed

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 ​Paginators where count() is used ​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.

Note: See TracTickets for help on using tickets.
Back to Top