Opened 7 years ago

Last modified 7 years ago

#28561 closed Cleanup/optimization

.order_by('foo__id') doesn't create a join — at Version 1

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 Marten Kenbeek)

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 (1)

comment:1 by Marten Kenbeek, 7 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top