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)
Change History (9)
by , 15 years ago
Attachment: | 12192-patch1.diff added |
---|
by , 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 , 15 years ago
Has patch: | set |
---|---|
Owner: | changed from | to
Triage Stage: | Unreviewed → 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 by , 15 years ago
Version: | 1.1 → 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.
by , 15 years ago
Attachment: | 12192-patch1-trunk-r12492.diff added |
---|
by , 15 years ago
Attachment: | 12192-patch2-trunk-r12492.diff added |
---|
comment:3 by , 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.
comment:4 by , 14 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Don't query the DB when we know it's going to have LIMIT 0