[Django] #28561: .order_by('foo__id') doesn't create a join

7 views
Skip to first unread message

Django

unread,
Sep 2, 2017, 11:25:28 AM9/2/17
to django-...@googlegroups.com
#28561: .order_by('foo__id') doesn't create a join
------------------------------------------------+------------------------
Reporter: Marten Kenbeek | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 1.11
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
------------------------------------------------+------------------------
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')
> }}}

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
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/28561>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 2, 2017, 11:33:39 AM9/2/17
to django-...@googlegroups.com
#28561: .order_by('foo__id') doesn't create a join
-------------------------------------+-------------------------------------

Reporter: Marten Kenbeek | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Documentation | Version: 1.11
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Marten Kenbeek:

Old description:

> 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')
> > }}}
>
> 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
> }}}

New description:

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')
> }}}

([https://docs.djangoproject.com/en/1.11/ref/models/querysets/#order-by
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
}}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/28561#comment:1>

Django

unread,
Sep 2, 2017, 12:31:36 PM9/2/17
to django-...@googlegroups.com
#28561: Docs inaccurate about QuerySet.order_by('foo__id') creating a join
--------------------------------------+------------------------------------

Reporter: Marten Kenbeek | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 1.11
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Tim Graham):

* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/28561#comment:2>

Django

unread,
Sep 4, 2017, 4:02:44 PM9/4/17
to django-...@googlegroups.com
#28561: Docs inaccurate about QuerySet.order_by('foo__id') creating a join
--------------------------------------+------------------------------------
Reporter: Marten Kenbeek | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 1.11
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Tim Graham):

* has_patch: 0 => 1


Comment:

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. [https://github.com/django/django/pull/9024 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/28561#comment:3>

Django

unread,
Sep 6, 2017, 7:22:10 PM9/6/17
to django-...@googlegroups.com
#28561: Docs inaccurate about QuerySet.order_by('foo__id') creating a join
--------------------------------------+------------------------------------
Reporter: Marten Kenbeek | Owner: nobody
Type: Cleanup/optimization | Status: closed
Component: Documentation | Version: 1.11
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by GitHub <noreply@…>):

* status: new => closed
* resolution: => fixed


Comment:

In [changeset:"44a6c27fd461e1d2f37388c26c629f8f170e8722" 44a6c27f]:
{{{
#!CommitTicketReference repository=""
revision="44a6c27fd461e1d2f37388c26c629f8f170e8722"
Fixed #28561 -- Removed inaccurate docs about QuerySet.order_by() and
joins.

As of ccbba98131ace3beb43790c65e8f4eee94e9631c, both examples don't use
a join.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/28561#comment:4>

Django

unread,
Sep 6, 2017, 7:22:25 PM9/6/17
to django-...@googlegroups.com
#28561: Docs inaccurate about QuerySet.order_by('foo__id') creating a join
--------------------------------------+------------------------------------
Reporter: Marten Kenbeek | Owner: nobody
Type: Cleanup/optimization | Status: closed
Component: Documentation | Version: 1.11

Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"a86d95726bdf84efaba7f30c44edddf6c34d2c2e" a86d9572]:
{{{
#!CommitTicketReference repository=""
revision="a86d95726bdf84efaba7f30c44edddf6c34d2c2e"
[1.11.x] Fixed #28561 -- Removed inaccurate docs about QuerySet.order_by()
and joins.

As of ccbba98131ace3beb43790c65e8f4eee94e9631c, both examples don't use
a join.

Backport of 44a6c27fd461e1d2f37388c26c629f8f170e8722 from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/28561#comment:5>

Django

unread,
Sep 6, 2017, 7:32:47 PM9/6/17
to django-...@googlegroups.com
#28561: Docs inaccurate about QuerySet.order_by('foo__id') creating a join
--------------------------------------+------------------------------------
Reporter: Marten Kenbeek | Owner: nobody
Type: Cleanup/optimization | Status: closed
Component: Documentation | Version: 1.11

Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"260bc84901951d9a9f23f23a4d7ae8bd4e2de298" 260bc84]:
{{{
#!CommitTicketReference repository=""
revision="260bc84901951d9a9f23f23a4d7ae8bd4e2de298"
[1.10.x] Fixed #28561 -- Removed inaccurate docs about QuerySet.order_by()
and joins.

As of ccbba98131ace3beb43790c65e8f4eee94e9631c, both examples don't use
a join.

Backport of 44a6c27fd461e1d2f37388c26c629f8f170e8722 from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/28561#comment:6>

Django

unread,
Sep 6, 2017, 7:32:59 PM9/6/17
to django-...@googlegroups.com
#28561: Docs inaccurate about QuerySet.order_by('foo__id') creating a join
--------------------------------------+------------------------------------
Reporter: Marten Kenbeek | Owner: nobody
Type: Cleanup/optimization | Status: closed
Component: Documentation | Version: 1.11

Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"3df8ccf6fc3fa0ab2acf9a03da43fea87f8ff392" 3df8ccf6]:
{{{
#!CommitTicketReference repository=""
revision="3df8ccf6fc3fa0ab2acf9a03da43fea87f8ff392"
[1.9.x] Fixed #28561 -- Removed inaccurate docs about QuerySet.order_by()
and joins.

As of ccbba98131ace3beb43790c65e8f4eee94e9631c, both examples don't use
a join.

Backport of 44a6c27fd461e1d2f37388c26c629f8f170e8722 from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/28561#comment:7>

Django

unread,
Sep 6, 2017, 7:34:29 PM9/6/17
to django-...@googlegroups.com
#28561: Docs inaccurate about QuerySet.order_by('foo__id') creating a join
--------------------------------------+------------------------------------
Reporter: Marten Kenbeek | Owner: nobody
Type: Cleanup/optimization | Status: closed
Component: Documentation | Version: 1.11

Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"e3cf8d2e94fe5f0b6d41774edbab0a9800b397ff" e3cf8d2e]:
{{{
#!CommitTicketReference repository=""
revision="e3cf8d2e94fe5f0b6d41774edbab0a9800b397ff"
[1.8.x] Fixed #28561 -- Removed inaccurate docs about QuerySet.order_by()
and joins.

As of ccbba98131ace3beb43790c65e8f4eee94e9631c, both examples don't use
a join.

Backport of 44a6c27fd461e1d2f37388c26c629f8f170e8722 from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/28561#comment:8>

Reply all
Reply to author
Forward
0 new messages