为Django模型管理器批量更新或创建
django-bulk-update-or-create的Python项目详细描述
django批量更新或创建
每个使用Django ORM的人最终都会发现自己在执行批处理操作update_or_create
操作:从外部源获取文件,与外部api同步,等等
如果记录的数量很大,QuerySet.update_or_create
的缓慢性将非常突出:使用它非常实用,但它总是执行一个SELECT
,然后再执行一个INSERT
(如果select没有返回任何内容)或UPDATE
/.save
(如果有)。在
网上搜索显示,这确实发生在很多人身上,尽管这似乎不是什么好办法:
- ^如果知道所有记录都是新的(并且不使用多表继承),则{
}非常快 bulk_update
使用相同的UPDATE
语句(使用一个巨大的WHERE
条件和CASE
语句来更新多个记录,但需要确保它们都存在- upsert (INSERT .. ON DUPLICATE KEY UPDATE)看起来很有趣(不同包上的TODO),但它们将被
bulk_create
限制==>不能在具有多表继承的模型上使用
此软件包试图解决模型查询集/管理器的这一介绍bulk_update_or_create
:
update_or_create
:(1 SELECT + 1 INSERT/UPDATE) * N
bulk_update_or_create
:1 BIG_SELECT + 1 BIG_UPDATE + (lte_N) INSERT
对于一批记录:
SELECT
全部来自数据库(基于match_field
参数)- 更新内存中的记录
- 使用
bulk_update
来处理这些问题 - 在剩余的每一个上使用
INSERT
/.create
(SOFTCORE)performance test看起来很有前途,平均时间减少了70%以上:
$ make testcmd # default - sqliteDJANGO_SETTINGS_MODULE=settings tests/manage.py bulk_it loop of update_or_create - all creates: 3.966486692428589 loop of update_or_create - all updates: 4.020653247833252 loop of update_or_create - half half: 3.9968857765197754 bulk_update_or_create - all creates: 2.949239730834961 bulk_update_or_create - all updates: 0.15633511543273926 bulk_update_or_create - half half: 1.4585723876953125 # mysqlDJANGO_SETTINGS_MODULE=settings_mysql tests/manage.py bulk_it loop of update_or_create - all creates: 5.511938571929932 loop of update_or_create - all updates: 5.321666955947876 loop of update_or_create - half half: 5.391834735870361 bulk_update_or_create - all creates: 1.5671980381011963 bulk_update_or_create - all updates: 0.14612770080566406 bulk_update_or_create - half half: 0.7262606620788574 # postgresDJANGO_SETTINGS_MODULE=settings_postgresql tests/manage.py bulk_it loop of update_or_create - all creates: 4.3584535121917725 loop of update_or_create - all updates: 3.6183276176452637 loop of update_or_create - half half: 4.145816087722778 bulk_update_or_create - all creates: 1.044851541519165 bulk_update_or_create - all updates: 0.14954638481140137 bulk_update_or_create - half half: 0.8407495021820068
安装
^{pr2}$将其添加到您的INSTALLED_APPS
列表中settings.py
使用
- 使用
BulkUpdateOrCreateQuerySet
作为模型的管理器
fromdjango.dbimportmodelsfrombulk_update_or_createimportBulkUpdateOrCreateQuerySetclassRandomData(models.Model):objects=BulkUpdateOrCreateQuerySet.as_manager()uuid=models.IntegerField(unique=True)data=models.CharField(max_length=200,null=True,blank=True)
- 呼叫
bulk_update_or_create
items=[RandomData(uuid=1,data='data for 1'),RandomData(uuid=2,data='data for 2'),]RandomData.objects.bulk_update_or_create(items,['data'],match_field='uuid')
- 或者使用上下文管理器(如果要更新大量项),因为它管理批处理队列
withRandomData.objects.bulk_update_or_create_context(['data'],match_field='uuid',batch_size=10)asbulkit:foriinrange(10000):bulkit.queue(RandomData(uuid=i,data=i+20))
bulk_update_or_create
支持yield_objects=True
,因此您可以迭代创建/更新的对象。bulk_update_or_create_context
向指定为status_cb
的回调函数提供相同的信息
文件
在制品
托多
- []医生!在
- []添加使用
bulk_create
进行创建的选项:如果启用,则断言模型不是多表 - []修复排序规则混乱:应删除关键字arg
case_insensitive_match
,并在运行时检测排序规则 - []添加对多个
match_field
的支持-可能需要使用WHERE (K1=X and K2=Y) or (K1=.. and K2=..)
而不是{},因为SQL标准似乎还没有被广泛采用 - []完成后,链接到
UPSERT
替代包!在
- 项目
标签: