美文网首页
Peewee批量插入数据

Peewee批量插入数据

作者: 小淼不卖萌 | 来源:发表于2018-09-11 12:05 被阅读0次

参考 Peewee批量插入数据

使用原生的insert_many()方法,并放入事务中

from xModels import XUser, database
import time

NUM = 10000
data = [{
            'phone': '13847374833',
            'password': '123456'
        } for i in range(NUM)]

start_time = time.time()

with database.atomic():
    for i in range(0, NUM, 100):
        # 每次批量插入100条,分成多次插入
        XUser.insert_many(data[i:i + 100]).execute()

print("插入{}条数据, 花费: {:.3}秒".format(NUM, time.time()-start_time))

经查阅文档,peewee支持分片 批量查询
参考:peewee-doc

from peewee import chunked

# insert 100 rows at a time.
with db.atomic():
    for batch in chunked(data, 100):
        XUser.insert_many(batch).execute()

相关文章

网友评论

      本文标题:Peewee批量插入数据

      本文链接:https://www.haomeiwen.com/subject/vqtugftx.html