#35124 closed Cleanup/optimization (invalid)
Could bulk_update() aggregate the pk's in WHEN clauses?
Reported by: | Vlada Macek | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I'd like to ask if there's a reason why instead of this
when_statements = [] for obj in batch_objs: attr = getattr(obj, field.attname) if not hasattr(attr, "resolve_expression"): attr = Value(attr, output_field=field) when_statements.append(When(pk=obj.pk, then=attr))
this code isn't used
when_updates = defaultdict(list) for obj in batch_objs: attr = getattr(obj, field.attname) if not hasattr(attr, "resolve_expression"): attr = Value(attr, output_field=field) when_updates[attr].append(obj.pk) when_statements = [ When(pk__in=pks, then=attr) if len(pks) > 1 else When(pk=pks[0], then=attr) for attr, pks in when_updates.items() ]
in bulk_update()
. Instead of having WHEN
for each pk, aggregated WHEN ("t"."id" IN (14679095, 14679134, 14679335, ...)) THEN ...
would be produced.
That could massively reduce the query length.
Change History (4)
comment:1 by , 11 months ago
Summary: | Could bulk_update() aggregate the primary keys in CASE clauses? → Could bulk_update() aggregate the pk's in WHEN clauses? |
---|
comment:2 by , 11 months ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
comment:3 by , 11 months ago
Thank you, Natalia.
Ok, while I don't see why this topic is invalid under the rules you reference and I think I correctly used the Optimization type, I opened the topic in the forum.
https://forum.djangoproject.com/t/could-bulk-update-aggregate-the-pks-in-when-clauses/27093
Hello Vlada Macek, thank you for your ticket!
This issue tracker has the goal to track bugs about Django, so we often ask contributors to post their questions and/or discuss Django internals in the Django Forum instead. With that in mind, would you consider starting a new conversation in the Django Internals category? You'll reach a wider audience and likely get extra feedback using that approach.
I'll be closing this ticket as invalid following the ticket triaging process, but if the forum conversation reaches a conclusion that this is a desired change, please come back to this issue pasting the link of the forum thread.