#28561 closed Cleanup/optimization (fixed)
Docs inaccurate about QuerySet.order_by('foo__id') creating a join
Reported by: | Marten Kenbeek | Owned by: | nobody |
---|---|---|---|
Component: | Documentation | Version: | 1.11 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description (last modified by )
The documentation on order_by()
states:
It is also possible to order a queryset by a related field, without incurring the cost of a JOIN, by referring to the _id of the related field:
# No Join Entry.objects.order_by('blog_id') # Join Entry.objects.order_by('blog__id')
(link)
However, in recent versions the latter query is optimised and won't create a join:
>>> print(Bar.objects.order_by('foo__id').query) SELECT "foo_bar"."id", "foo_bar"."foo_id" FROM "foo_bar" ORDER BY "foo_bar"."foo_id" ASC
Change History (8)
comment:1 by , 7 years ago
Description: | modified (diff) |
---|
comment:2 by , 7 years ago
Summary: | .order_by('foo__id') doesn't create a join → Docs inaccurate about QuerySet.order_by('foo__id') creating a join |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:3 by , 7 years ago
Has patch: | set |
---|
Note:
See TracTickets
for help on using tickets.
I found that the behavior changed (removing the join for
.order_by('blog__id')
) in ccbba98131ace3beb43790c65e8f4eee94e9631c (Django 1.7). Oddly, the text in question was added in 21b858cb6735cdfdc695ff7b076e4cbc1981bc88 as part of Django 1.8, so it seems it was inaccurate since its introduction.Unless there's a subtlety I'm not thinking of, I think we can remove that text. PR