策略在Django加快一批ORM操作 [英] Strategies for speeding up batch ORM operations in Django
问题描述
我的一个API调用可能导致更新大量对象(Django模型)。我遇到性能问题与此,因为我单独更新每一个项目,保存和移动到下一个:
One of my API calls can result in updates to a large number of objects (Django models). I'm running into performance issues with this since I'm updating each item individually, saving, and moving on to the next:
for item in Something.objects.filter(x='y'):
item.a="something"
item.save()
有时我的过滤条件看起来像,其中x在('A','B','C',......)。
Sometimes my filter criterion looks like "where x in ('a','b','c',...)".
看来这个官方的回答是不会修复。我想知道的策略的人使用,以提高在这些情况下的表现是什么。
It seems the official answer to this is "won't fix". I'm wondering what strategies people are using to improve performance in these scenarios.
推荐答案
您链接到门票是用于批量创建 - 如果你不依赖于一个重写保存
方法或pre /后保存信号做保存工作位,<一个href=\"http://docs.djangoproject.com/en/dev/topics/db/queries/#updating-multiple-objects-at-once\"><$c$c>QuerySet$c$c>有一个更新
,你可以用它来对滤波行执行更新
方法:
The ticket you linked to is for bulk creation - if you're not relying on an overridden save
method or pre/post save signals to do bits of work on save, QuerySet
has an update
method which you can use to perform an UPDATE
on the filtered rows:
Something.objects.filter(x__in=['a', 'b', 'c']).update(a='something')
这篇关于策略在Django加快一批ORM操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!