Code

Opened 4 years ago

Closed 4 years ago

#12192 closed (fixed)

Don't execute sql query with limit 0

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

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 4 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 4 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 4 years ago.
12192-patch2-trunk-r12492.diff (540 bytes) - added by ramiro 4 years ago.
12192.diff (2.0 KB) - added by ramiro 4 years ago.
Patch for review

Download all attachments as: .zip

Change History (9)

Changed 4 years ago by ramiro

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

Changed 4 years ago by ramiro

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

comment:1 Changed 4 years ago by ramiro

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to ramiro
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design 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 Changed 4 years ago by ramiro

  • Version changed from 1.1 to SVN

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.

Changed 4 years ago by ramiro

Changed 4 years ago by ramiro

comment:3 Changed 4 years ago by ramiro

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.

Changed 4 years ago by ramiro

Patch for review

comment:4 Changed 4 years ago by ramiro

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

(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.

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.