Updating 200,000 rows: bulk update() vs batched bulk_update()

Backend Live Coding November 13, 2026 python

bulk_update/update does not call save() or signals.

python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# ======================================================================
# Q: You have to update 200,000 records. How?
#
# bulk_update/update does not call save() or signals. If there is logic there
# (e.g. cache invalidation), I call it explicitly.
# ======================================================================

# If the new value is computed in the database: a single UPDATE, nothing is loaded into Python
Screen.objects.filter(workspace=ws).update(playlist=new_playlist)

# If the value differs per row: batches, so you don't hold a huge transaction and locks
qs = Screen.objects.filter(needs_migration=True).only('pk', 'settings')
for batch in batched(qs.iterator(chunk_size=2000), 2000):
    for s in batch:
        s.settings = migrate_settings(s.settings)
    with transaction.atomic():
        Screen.objects.bulk_update(batch, ['settings'], batch_size=1000)