Opened 15 years ago

Closed 14 years ago

#12192 closed (fixed)

Don't execute sql query with limit 0

Reported by: Alexander Schepanovski Owned by: Ramiro Morales
Component: Database layer (models, ORM) Version: dev
Severity: Keywords:
Cc: Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When QuerySet.query has low_mark == high_mark (happens in pagination for example) ORM executes a query like:

    select ... from ... limit 0

Attachments (5)

12192-patch1.diff (687 bytes ) - added by Ramiro Morales 15 years ago.
Don't query the DB when we know it's going to have LIMIT 0
12192-patch2.diff (556 bytes ) - added by Ramiro Morales 15 years ago.
This one doesn even generate the SQL of the query if it's going to have LIMIT 0
12192-patch1-trunk-r12492.diff (715 bytes ) - added by Ramiro Morales 15 years ago.
12192-patch2-trunk-r12492.diff (540 bytes ) - added by Ramiro Morales 15 years ago.
12192.diff (2.0 KB ) - added by Ramiro Morales 14 years ago.
Patch for review

Download all attachments as: .zip

Change History (9)

by Ramiro Morales, 15 years ago

Attachment: 12192-patch1.diff added

Don't query the DB when we know it's going to have LIMIT 0

by Ramiro Morales, 15 years ago

Attachment: 12192-patch2.diff added

This one doesn even generate the SQL of the query if it's going to have LIMIT 0

comment:1 by Ramiro Morales, 15 years ago

Has patch: set
Owner: changed from nobody to Ramiro Morales
Triage Stage: UnreviewedDesign decision needed

I've attached two patches that implement detection of the high_mark == low_mark condition and avoid going to the DB with such a query, the second patch could be even slightly more efficient because it shortcuts execution of Query.as_sql() early completely avoiding the setup and generation of the SQL query text.

One thing to review and decide is that isn't clear from the docstrings and comments what Query.as_sql() is supposed to return to signal no SQL query text was generated. The patches currently return an empty string but I don't know now if it should be None.

Tests that exercise this already exist in modeltests/basic and regressiontests/queries.

comment:2 by Ramiro Morales, 15 years ago

Version: 1.1SVN

I've updated the patches for this ticket to trunk status after the multi-db merge.

If/once one of the are chosen and applied to trunk, then the similarly named chosen among 12192-patch1.diff and 12192-patch2.diff could be applied to 1.1.x as they should still apply correctly there.

by Ramiro Morales, 15 years ago

by Ramiro Morales, 15 years ago

comment:3 by Ramiro Morales, 15 years ago

Patches were fixed and updated to r12492. Both of them were tested by running the Django test suite against MySQL, PostgreSQL and sqlite3 and it was verified none of them introduced errors.

by Ramiro Morales, 14 years ago

Attachment: 12192.diff added

Patch for review

comment:4 by Ramiro Morales, 14 years ago

Resolution: fixed
Status: newclosed

(In [14204]) Fixed #12192 -- Don't execute any DB query when the QS slicing being performed
will result in use of LIMIT 0. Thanks Suor for the report.

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