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最快

你可能感兴趣的:(Python Mysql数据批量插入测试)