Opened 15 years ago
Closed 15 years ago
#12258 closed (fixed)
QuerySet 'get' method should clear ordering before executing query
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.1 |
Severity: | Keywords: | ordering, database, queryset, mq, mq-200912-dallas | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
The SQL generated by the ORM when calling get
includes ordering parameters specified in Meta.ordering
. This might be small in the greater scheme of things, but clearing the ordering before evaluating the cloned QuerySet
would prevent unnecessary filesorts by the database.
Attachments (3)
Change History (10)
by , 15 years ago
Attachment: | order_by.patch added |
---|
follow-up: 2 comment:1 by , 15 years ago
comment:2 by , 15 years ago
Replying to nbv4:
I'm not 100% positive, but I'm pretty sure SQL databases internally optimize queries so that "LIMIT 1" automatically "cancels out" any ordering clause so that it doesn't execute the ordering code on that query.
If they did, they would produce wrong results. .get()
does not do a LIMIT 1 anyway — the only reason we can remove the ORDER BY here is because we retrieve all results and throw an exception if there isn't exactly one i.e. we do the limiting to one after the database has returned results, and excess rows cause no data to be returned.
comment:3 by , 15 years ago
Updated patch which passes all tests. The existing patch broke .latest() because you can't change ordering after slicing.
by , 15 years ago
comment:4 by , 15 years ago
Keywords: | mq mq-200912-dallas added |
---|---|
Triage Stage: | Unreviewed → Ready for checkin |
comment:5 by , 15 years ago
Triage Stage: | Ready for checkin → Accepted |
---|
by , 15 years ago
comment:6 by , 15 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
comment:7 by , 15 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
I'm not 100% positive, but I'm pretty sure SQL databases internally optimize queries so that "LIMIT 1" automatically "cancels out" any ordering clause so that it doesn't execute the ordering code on that query.