django mysql update_django 1.5 批量插入或更新数据库

本文介绍了在Django应用中如何进行批量更新和创建操作,通过对比不同方法的效率,如使用`bulk_create`和`update()`,强调了正确方法能显著减少视图加载时间。示例代码展示了从Django 1.4前的手动事务管理到使用内置方法的性能提升。
摘要由CSDN通过智能技术生成

Doing bulk update and bulk create with Django ORM

It's not unusual the need to do bulk update/create in django applications, but if you don't use the right approach your views will increase the load time to unacceptable values.

Here is the common example where people starts out:# took 37 seconds def auto_transaction():

for i in range(10000):

name="String number %s"%i

Record.objects.create(name=name)

Before django 1.4 we didn't have the built-in bulk_create, so the common way to do a bulk insertion was disabling the auto transaction in the beginning of operation and do it only once in the end:# took 2.65 seconds

@transaction.commit_manually

def manual_transaction():

for i in range(10000):

name="String number %s"%i

Record.objects.create(name=name)

transaction.commit()

But since Django 1.4 an

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值