#29081: select_related and hitting the database
-------------------------------------+-------------------------------------
Reporter: Дилян Палаузов | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Documentation | Version: 2.0
Severity: Normal | Resolution:
Keywords: select_related | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Дилян Палаузов:
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.}}}, and
> * changing {{{p = b.author # Hits the database.}}} to {{{p =
> b.author # Doesn't hit the database.}}}
New 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.}}}
--
--
Ticket URL: <https://code.djangoproject.com/ticket/29081#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/072.d9a0ad1d7d8087972b3c80ee134553e1%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.