Python Mysql数据批量插入测试

测试表有215万条数据,34个字段

最普通的循环插入,100条耗时 1.283,1000条耗时 12.603

t1 = time.time()
for n in range(100):
    sql = 'insert into amazon_month_copy1 (order_id) values("test_order_id")'
    cursor.execute(sql)
connect.commit()
print(time.time() - t1)

先拼接好多行语句,再插入,100条耗时 0.028,1000条耗时 0.073

t1 = time.time()
sql = 'insert into amazon_month_copy1 (order_id) values'
for n in range(100):
    sql += '("test_order_id"),'
sql = sql[0:-1] + ';'
cursor.execute(sql)
connect.commit()
print(time.time() - t1)

使用executemany,100条耗时 0.027,1000条耗时 0.052

t1 = time.time()
ls = list()
sql = 'insert into amazon_month_copy1 (order_id) values(%s)'
for n in range(100):
    ls.append(('test_order_id'))
cursor.executemany(sql, ls)
connect.commit()
print(time.time() - t1)

结论:executemany最快

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。