I am trying to use pymysql.executemany
to multi insert.
But the less of a space after VALUES
in sql causes the running time to increase. Why is that?
Here is my code, check the two run
function.
import pymysql.cursors
from functools import wraps
from datetime import datetime
def running_time(func):
@wraps(func)
def deco():
t0 = datetime.now()
res = func()
t1 = datetime.now()
print(t1 - t0)
return res
return deco
CONFIG = {
'host': 'localhost',
'port': 3306,
'user': 'root',
'password': '',
'db': 'test',
'charset': 'utf8',
'cursorclass': pymysql.cursors.DictCursor,
'autocommit': True,
}
connection = pymysql.connect(**CONFIG)
cur = connection.cursor()
@running_time
def run():
sql = """INSERT INTO table_name(c) VALUES (%s)"""
for i in range(100):
param = ['1'] * 2000
cur.executemany(sql, param)
run()
@running_time
def run():
sql = """INSERT INTO table_name(c) VALUES(%s)"""
for i in range(100):
param = ['1'] * 2000
cur.executemany(sql, param)
run()
outputs:
0:00:02.765183
0:01:13.729428