﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
35124	Could bulk_update() aggregate the pk's in WHEN clauses?	Vlada Macek	nobody	"I'd like to ask if there's a reason why instead of this

{{{#!python

                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

{{{#!python
                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."	Cleanup/optimization	closed	Database layer (models, ORM)	dev	Normal	invalid			Unreviewed	0	0	0	0	0	0
