Opened 15 years ago

Closed 12 years ago

Last modified 12 years ago

#11295 closed Cleanup/optimization (fixed)

If ModelAdmin.queryset returns a filtered QS don't require a 2nd count call

Reported by: Alex Gaynor Owned by: Wiktor
Component: contrib.admin Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

There is an optimization that tests QuerySet.query.where to see if we can reuse a count call, however we don't apply this optimization aggressively enough.

Attachments (2)

admin-count.diff (1.1 KB ) - added by Alex Gaynor 15 years ago.
admin-count.2.diff (3.5 KB ) - added by Alex Gaynor 15 years ago.

Download all attachments as: .zip

Change History (12)

by Alex Gaynor, 15 years ago

Attachment: admin-count.diff added

comment:1 by Alex Gaynor, 15 years ago

Triage Stage: UnreviewedAccepted

by Alex Gaynor, 15 years ago

Attachment: admin-count.2.diff added

comment:2 by Julien Phalip, 14 years ago

Severity: Normal
Type: Cleanup/optimization

comment:3 by patchhammer, 14 years ago

Easy pickings: unset
Patch needs improvement: set

admin-count.2.diff fails to apply cleanly on to trunk

comment:4 by Aymeric Augustin, 13 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:5 by Wiktor, 12 years ago

Owner: changed from Alex Gaynor to Wiktor
Status: newassigned

comment:6 by Wiktor, 12 years ago

I've updated patch so it applies correctly on current master.
Also, test in original patch assumes that there are 6 queries to db (including messages framework).

Currently, before applying patch there are 5 of them (including duplicated COUNT(*):

(Pdb) pprint(connection.queries)
[{'sql': 'QUERY = \'SELECT "django_session"."session_key", "django_session"."session_data", "django_session"."expire_date" FROM "django_session" WHERE ("django_session"."session_key" = %s  AND "django_session"."expire_date" > %s )\' - PARAMS = (\'2nj8364ycp90oge7xl5tf9mviz7grety\', \'2013-02-23 17:53:11.374277\')',
  'time': '0.000'},
 {'sql': 'QUERY = \'SELECT "auth_user"."id", "auth_user"."password", "auth_user"."last_login", "auth_user"."is_superuser", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."date_joined" FROM "auth_user" WHERE "auth_user"."id" = %s \' - PARAMS = (100,)',
  'time': '0.001'},
 {'sql': 'QUERY = \'SELECT COUNT(*) FROM "admin_views_emptymodel" WHERE "admin_views_emptymodel"."id" > %s \' - PARAMS = (1,)',
  'time': '0.000'},
 {'sql': 'QUERY = \'SELECT COUNT(*) FROM "admin_views_emptymodel" WHERE "admin_views_emptymodel"."id" > %s \' - PARAMS = (1,)',
  'time': '0.000'},
 {'sql': 'QUERY = \'SELECT "admin_views_emptymodel"."id" FROM "admin_views_emptymodel" WHERE "admin_views_emptymodel"."id" > %s  ORDER BY "admin_views_emptymodel"."id" DESC\' - PARAMS = (1,)',
  'time': '0.000'}]

After patch was applied, COUNT(*) is executed once:

(Pdb) pprint(connection.queries)
[{'sql': 'QUERY = \'SELECT "django_session"."session_key", "django_session"."session_data", "django_session"."expire_date" FROM "django_session" WHERE ("django_session"."session_key" = %s  AND "django_session"."expire_date" > %s )\' - PARAMS = (\'5y65r95ng27kccdmkrtoo1qom7q4vhln\', \'2013-02-23 17:54:51.024189\')',
  'time': '0.000'},
 {'sql': 'QUERY = \'SELECT "auth_user"."id", "auth_user"."password", "auth_user"."last_login", "auth_user"."is_superuser", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."date_joined" FROM "auth_user" WHERE "auth_user"."id" = %s \' - PARAMS = (100,)',
  'time': '0.001'},
 {'sql': 'QUERY = \'SELECT COUNT(*) FROM "admin_views_emptymodel" WHERE "admin_views_emptymodel"."id" > %s \' - PARAMS = (1,)',
  'time': '0.000'},
 {'sql': 'QUERY = \'SELECT "admin_views_emptymodel"."id" FROM "admin_views_emptymodel" WHERE "admin_views_emptymodel"."id" > %s  ORDER BY "admin_views_emptymodel"."id" DESC\' - PARAMS = (1,)',
  'time': '0.000'}]

comment:7 by Wiktor, 12 years ago

New patch created.

Pull request here:

https://github.com/django/django/pull/820

comment:8 by Wiktor, 12 years ago

Patch needs improvement: unset

comment:9 by Wiktor Kolodziej <wiktor@…>, 12 years ago

Resolution: fixed
Status: assignedclosed

In f07a5f0a21857204465019b4e68f914d31cb396a:

Fixed #11295: If ModelAdmin.queryset returns a filtered QS don't require a 2nd count call

Original patch rewritten, added tests and get_filters_params method for ChangeList class.
Thanks Alex for the report.

comment:10 by Honza Král <Honza.Kral@…>, 12 years ago

In d7e835f76d81d66a6d8964dba20506a1c6e559d8:

Merge pull request #820 from viciu/11295

Fixed #11295: If ModelAdmin.queryset returns a filtered QS don't require a 2nd count call

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