#29081: select_related and hitting the database
-------------------------------------+-------------------------------------
               Reporter:  Дилян      |          Owner:  nobody
  Палаузов                           |
                   Type:             |         Status:  new
  Cleanup/optimization               |
              Component:             |        Version:  2.0
  Documentation                      |
               Severity:  Normal     |       Keywords:  select_related
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 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.}}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29081>
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/057.99c07d6d427645b9e1f3043b487ec3fd%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to