#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 <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/062.eca7768cbe879a6f68b4089eb6450ab1%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to