参考 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()
网友评论