#29081: Clarify QuerySet.select_related() example regarding "hitting the 
database"
--------------------------------------+------------------------------------
     Reporter:  Дилян Палаузов        |                    Owner:  nobody
         Type:  Cleanup/optimization  |                   Status:  new
    Component:  Documentation         |                  Version:  2.0
     Severity:  Normal                |               Resolution:
     Keywords:  select_related        |             Triage Stage:  Accepted
    Has patch:  1                     |      Needs documentation:  0
  Needs tests:  0                     |  Patch needs improvement:  0
Easy pickings:  0                     |                    UI/UX:  0
--------------------------------------+------------------------------------
Changes (by Tim Graham):

 * has_patch:  0 => 1
 * stage:  Unreviewed => Accepted


Old description:

> https://docs.djangoproject.com/en/2.0/ref/models/querysets/#select-
> related says:
>
> > … then a call to Book.objects.select_related('author_
> _hometown').get(id=4) will cache the related Person and the related City:
> {{{
> b = Book.objects.select_related('author_ _hometown').get(id=4)
> p = b.author         # Doesn't hit the database.
> c = p.hometown       # Doesn't hit the database.
>
> b = Book.objects.get(id=4) # No select_related() in this example.
> p = b.author         # Hits the database.
> c = p.hometown       # Hits the database.
> }}}
>
> This leaves the impression, that the first snipped does not hit the
> database and the second snippet hits twice the database.
>
> I propose:
> * wring after {{{b = Book.objects.select_related('author_
> _hometown').get(id=4)}}} the comment # Hits the database with INNER JOIN.
> * changing {{{b = Book.objects.get(id=4) # No select_related() in this
> example.}}} to {{{b = Book.objects.get(id=4) # No select_related() in
> this example, hits the database.}}}

New description:

 [[Image()]]https://docs.djangoproject.com/en/2.0/ref/models/querysets
 /#select-related says:

 > … then a call to Book.objects.select_related('author_
 _hometown').get(id=4) will cache the related Person and the related City:
 {{{
 b = Book.objects.select_related('author_ _hometown').get(id=4)
 p = b.author         # Doesn't hit the database.
 c = p.hometown       # Doesn't hit the database.

 b = Book.objects.get(id=4) # No select_related() in this example.
 p = b.author         # Hits the database.
 c = p.hometown       # Hits the database.
 }}}

 This leaves the impression, that the first snipped does not hit the
 database and the second snippet hits twice the database.

 I propose:
 * wring after {{{b = Book.objects.select_related('author_
 _hometown').get(id=4)}}} the comment # Hits the database with INNER JOIN.
 * changing {{{b = Book.objects.get(id=4) # No select_related() in this
 example.}}} to {{{b = Book.objects.get(id=4) # No select_related() in this
 example, hits the database.}}}

--

Comment:

 [https://github.com/django/django/pull/9629 PR]

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29081#comment:2>
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 django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/072.92bb131621c63ca9eef8ff1e14c9e806%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to