Code

Opened 2 years ago

Closed 2 years ago

#18354 closed Bug (fixed)

Duplicate query in archive and year class based views

Reported by: aaugustin Owned by: nobody
Component: Generic views Version: 1.4
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

In BaseArchiveIndexView and BaseYearArchiveView, get_dated_items:

  • calls get_dated_queryset, which is going trigger the evaluation of said queryset (unless pagination is enabled);
  • adds a new ordering clause on the queryset before making it the object_list.

Because of the added ordering clause, the queryset will be re-evaluated when accessing context['object_list'] / context[context_object_name].

The ordering clause should probably be moved into get_dated_queryset. There are two ways to achieve this:

  • Add an optional ordering parameter to get_dated_queryset. This is the fastest solution and it'll be totally backwards compatible.
  • Add an API to control the order of the queryset for all views. Currently no order is enforced, unless the model's meta.ordering is set -- which is often a bad practice as it adds the sorting overhead to every query involving the models.

I'm probably going to adopt the first solution to fix this problem. The second is a larger feature request.

Attachments (0)

Change History (1)

comment:1 Changed 2 years ago by Aymeric Augustin <aymeric.augustin@…>

  • Resolution set to fixed
  • Status changed from new to closed

In [03f86a5adb930eac55dea1e903fb958c002d5bc4]:

Fixed #18354 -- Performance issue in CBV.

Prevented repeating a query twice when the model isn't ordered by
-date_field (in Meta), allow_empty is False and pagination isn't
enabled.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.