#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
-------------------------------------+-------------------------------------
Changes (by Simon Charette):
* resolution: => wontfix
* status: new => closed
* type: Uncategorized => Cleanup/optimization
Comment:
I don't think we can fix this without a significant refactor to how
prefetch related work given the Django ORM does not maintain a map of
object identities by related fields so I'm going to ''won't fix'' this
one.
In order to implement such a feature the prefetch related logic would have
to pass a memoization object of the form `dict[tuple[type[Model],
models.Field], dict[Any, Model]]` that maps model types and from field to
another map of from field value to model instances to
`prefetch_related_objects` and `prefetch_one_level`. All that to support
the likely rare cases where the same relationship is prefetched through
different paths and happens to target the exact set of objects (as any
difference would require an extra query anyway).
If you disagree with the resolution of this ticket
[https://docs.djangoproject.com/en/5.1/internals/contributing/bugs-and-
features/#requesting-features feel free to open a thread on the forums]
about the benefits vs complexity tradeoff of this requested optimization.
--
Ticket URL: <https://code.djangoproject.com/ticket/35947#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 view this discussion visit
https://groups.google.com/d/msgid/django-updates/010701936f1efad1-71a3a3fd-4491-4359-a627-7055fbcdaec4-000000%40eu-central-1.amazonses.com.