Opened 7 years ago
Closed 2 years ago
#28692 closed Cleanup/optimization (wontfix)
QuerySet.bulk_create() combine with select/prefetch_related()
Reported by: | Дилян Палаузов | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.11 |
Severity: | Normal | Keywords: | bulk_create select_related prefetch_related |
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
When several objects are created with Models.objects.bulk_create() the called might want to prefetch/JOIN some related models to the new objects, so that additional SELECTs are avoided.
for m in ModelA.objects.select_related('b').bulk_create([ModelA(...), ModelA(...), ModelA(...)]): print(m.b.description) # this line shall now cause a new SELECT on each iteration
Change History (7)
comment:1 by , 7 years ago
comment:2 by , 7 years ago
Resolution: | → needsinfo |
---|---|
Status: | new → closed |
comment:3 by , 7 years ago
Resolution: | needsinfo |
---|---|
Status: | closed → new |
#28600 asks for adding .prefetch_related() support to RawQuerySet . I don't see why that ticket is clear, but this one, asking for adding prefetch_related() support to bulk_create() is anyhow different and hence unclear.
Despite there are already two other ways to reduce the number of queries, prefetch_related() is more coherent, as this what users are used to in other circumstances.
comment:4 by , 7 years ago
I think it will be impossible to implement on backends other than postgres, as you would want to do SELECT modela.id, modelb.* FROM modela JOIN modelb ON modela.b_id = modelb.id WHERE modela.id IN (...)
, however the value of (...)
is only available in postgres.
comment:5 by , 7 years ago
Triage Stage: | Unreviewed → Accepted |
---|---|
Type: | Uncategorized → Cleanup/optimization |
I guess a patch could be evaluated.
comment:6 by , 7 years ago
I guess a patch will be provided for prefetch_related() once #28600 gets integrated, as stated in the fourth comment of that ticket.
comment:7 by , 2 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
No patch was provided and I agree that this will be very hard to implement select_related
in a backend agnostic way. Now that prefetch_related_objects is a documented public API it can be used it to achieve the same results as what prefetch_related
would have done.
Closing as wontfix for now, I wouldn't be opposed to reopening once we get better support for RETURNING
handling at the sql.Query
level if a patch is provided.
https://docs.djangoproject.com/en/dev/internals/contributing/bugs-and-features/
Please send the feature request again to the mailing list. However, here's my opinion:
I don't think it will be possible to implement
select_related
as the behaviour ofINSERT .. RETURNING ..
varies a lot between databases. It would be possible to implementprefetch_related
.There are already two ways to reduce the number of queries, is either good for you? One is:
Another is setting the
b
attribute on your models, instead ofb_id