Opened 15 years ago

Closed 11 years ago

#11881 closed Bug (fixed)

ORDER BY in aggregate subqueries is not necessary and might cause problems on some SQL servers

Reported by: egenix_viktor Owned by: Anssi Kääriäinen <akaariai@…>
Component: Database layer (models, ORM) Version: 1.1
Severity: Normal Keywords: aggregate subquery order_by optimization incompatibility
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Some SQL servers, like MS SQL 2005 does not allow ORDER BY in subqueries without a TOP modifier. (It might apply to other database servers as well.) It is also suboptimal to use ORDER BY in the subquery of an aggregate SELECT, since it does not affect the result.

It might be better to just clear ordering in the subquery in this case. I suggest adding the following line in the BaseQuery.get_aggregation method before the query.add_subquery(obj) call:

obj.clear_ordering(True)

Patch against the db\models\sql\query.py file of the 1.1 release version has been attached.

Attachments (1)

query.py.patch (502 bytes ) - added by egenix_viktor 15 years ago.
Patch against db/models/sql/query.py (Django 1.1, release version)

Download all attachments as: .zip

Change History (12)

by egenix_viktor, 15 years ago

Attachment: query.py.patch added

Patch against db/models/sql/query.py (Django 1.1, release version)

comment:1 by Jacob, 14 years ago

milestone: 1.2
Triage Stage: UnreviewedAccepted

comment:2 by anonymous, 14 years ago

Summary: ORDER BY in aggregate subqueries is not necessary and might causes on some SQL serversORDER BY in aggregate subqueries is not necessary and might cause problems on some SQL servers

comment:3 by Russell Keith-Magee, 14 years ago

Component: Database layer (models, ORM)ORM aggregation
milestone: 1.21.3
Needs tests: set
Owner: nobody removed

Not critical for 1.2

comment:4 by Julien Phalip, 13 years ago

Severity: Normal
Type: Bug

comment:5 by Jacob, 12 years ago

milestone: 1.3

Milestone 1.3 deleted

comment:11 by Aymeric Augustin, 12 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:12 by Aymeric Augustin, 12 years ago

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:13 by Anssi Kääriäinen, 11 years ago

Component: ORM aggregationDatabase layer (models, ORM)

comment:14 by Anssi Kääriäinen, 11 years ago

Resolution: fixed
Status: newclosed

There is a clear_ordering clause in get_aggregation(), line 356 of django/db/models/sql/query.py

comment:15 by Anssi Kääriäinen, 11 years ago

Resolution: fixed
Status: closednew

The above isn't correct, the clear_ordering is applied to the outer query, not to the inner query. So, back to "accepted".

comment:16 by Anssi Kääriäinen <akaariai@…>, 11 years ago

Owner: set to Anssi Kääriäinen <akaariai@…>
Resolution: fixed
Status: newclosed

In 7bc57a6d71dd4d00bb09cfa67be547591fd759ce:

Fixed #11881 -- removed junk from aggregation subqueries

There were clauses that weren't needed in the subqueries. These were
ORDER BY, SELECT FOR UPDATE and related selections.

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