#35947: prefetch_related makes duplicate queries for the same records by 
different
relations
-------------------------------------+-------------------------------------
     Reporter:  Jake Douglas         |                    Owner:  (none)
         Type:                       |                   Status:  closed
  Cleanup/optimization               |
    Component:  Database layer       |                  Version:  5.1
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:  wontfix
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Comment (by Simon Charette):

 > For the record, here's an example request from our production
 application:
 > (313 queries including 279 similar and 242 duplicates)

 The example you provided should limit the number of SQL queries to 6 which
 you have detailed to be

 {{{#!sql
 SELECT ••• FROM "houses_house"
 SELECT ••• FROM "houses_room" WHERE "houses_room"."house_id" IN (1)
 SELECT ••• FROM "houses_window" WHERE "houses_window"."room_id" IN (1) #
 DUPLICATE
 SELECT ••• FROM "houses_story" WHERE "houses_story"."house_id" IN (1)
 SELECT ••• FROM "houses_room" WHERE "houses_room"."story_id" IN (1)
 SELECT ••• FROM "houses_window" WHERE "houses_window"."room_id" IN (1) #
 DUPLICATE
 }}}

 Which is one for the top-level houses queryset, two for the
 `rooms__windows` prefetch, and three for the `stories__rooms__windows`
 prefetch where two are duplicates. As explained above, invasive changes
 would be required to take that number to 4 under circumstances that your
 specific schema design seems to be exhibiting.

 If your application is executing 50x the number of 6 queries that could be
 reduced to 4 by implementing this optimization there is likely something
 else to look on your side before implementing this optimization. In other
 words `prefetch_related` is not a feature that systemically make elevated
 number of queries issues go away; it's a tool that bounds the N+1 query
 problem and not an identity mapper solution #17.
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35947#comment:3>
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 view this discussion visit 
https://groups.google.com/d/msgid/django-updates/010701936fa1efca-79d42314-7fa8-44d1-842f-f2b4b8231647-000000%40eu-central-1.amazonses.com.

Reply via email to