#35947: prefetch_related makes duplicate queries for the same records by
different
relations
-------------------------------------+-------------------------------------
Reporter: Jake Douglas | Type:
| Uncategorized
Status: new | Component: Database
| layer (models, ORM)
Version: 5.1 | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Prefetching the same records via different relations using
`prefetch_related` results in duplicated queries for the same set of
records. This is a problem because many web applications traverse
relations by many different paths, even in the same request. The smallest
example I could reproduce follows:
{{{
from django.db import models
# Create your models here.
class House(models.Model):
pass
class Story(models.Model):
house = models.ForeignKey(House, related_name = "stories", on_delete =
models.CASCADE)
class Room(models.Model):
story = models.ForeignKey(Story, related_name = "rooms", on_delete =
models.CASCADE)
house = models.ForeignKey(House, related_name = "rooms", on_delete =
models.CASCADE)
class Window(models.Model):
story = models.ForeignKey(Story, related_name = "windows", on_delete =
models.CASCADE)
room = models.ForeignKey(Room, related_name = "windows", on_delete =
models.CASCADE)
House.objects.prefetch_related(
"rooms__windows",
"stories__rooms__windows"
).all()
}}}
This results in something like this:
{{{
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
}}}
The same behavior occurs when the last relation in the chain is one-to-
one.
--
Ticket URL: <https://code.djangoproject.com/ticket/35947>
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/010701936eeb86c0-f913952d-cd9e-462e-bdd5-2ec28e09d19e-000000%40eu-central-1.amazonses.com.