mysql批量插入数据 excutemany
2021/11/28 19:12:07
本文主要是介绍mysql批量插入数据 excutemany,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
# coding=utf8
"""
问题:
往数据库批量插入10条数据的时候,在for循环里面使用excute插入,接口请求耗时>1s,严重影响效率;
遂考虑使用excutemany批量插入,接口请求耗时400ms
"""
# 逐条插入:cursor.excute()
# 批量插入:cursor.excutemany()
# 使用:executemany(templet, args)
# templet:sql模板字符串,例如:insert into table(id,name) values(%s,%s)
# args: 模板字符串中的参数,是一个list或者tuple, eg: [(1,"a"),(2,"b")] 或者 ((1,"a"),(2,"b"))
# 实例-节选 import time draw_list = [...] # len(draw_list) = 10 sql = ''' insert into table(remix_uid, stage, tp, price, draw_result) values (%s,%s,%s,%s,%s)'''
cursor = db.cursor() start_time = time.time() for i in draw_list: cursor.excute(sql, [args]) end_time = time.time() - start_time print(end_time) # >1s
# ----修改为以下----
cursor = db.cursor() start_time = time.time() data = ((item[''], item[''], ...) for item in draw_list) cursor.executemany(sql, data) end_time = time.time() - start_time print(end_time) # 400ms
这篇关于mysql批量插入数据 excutemany的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-25MySQL报错Duplicate entry '0' for key 'PRIMARY'
- 2024-05-29阿里 Canal 实时同步 MySQL 增量数据至 ClickHouse 数据库
- 2024-05-24在Linux下管理MySQL的大小写敏感性
- 2024-04-26MySQL查出时间比实际晚8小时的解决方案
- 2024-04-01JPA不识别MySQL的枚举类型
- 2024-03-30mysql数据库表卡死解决方法
- 2024-03-15MySQL多数据源笔记5-ShardingJDBC实战
- 2024-03-11natural join mysql
- 2024-03-11关于VS2017,VS2015 中利用 EF使用Mysql 不显示数据源问题解决方案
- 2024-02-26mysql 阿里云xb后缀备份文件恢复-icode9专业技术文章分享