问题
python-2.7.15, pymssql-2.1.4, SQL_Server-2018, Windows 10 Pro, MS-Office-2016
import time
import csv
import pymssql
db_settings = {
"host" : "127.0.0.1",
"port" : "1433",
"user" : "sa",
"password" : "********",
"database" : "testdb",
"charset" : "utf8"
}
conn = pymssql.connect(**db_settings)
cursor = conn.cursor()
ff = csv.reader(open('base.csv', 'r'))
sql = """
BEGIN
INSERT INTO Base([name], [year], [update], [status],
[timeline], [language], [pic]) VALUES (%s, %s, %s, %s, %s, %s, %s)
END
"""
now=time.strftime("%M:%S")
t = []
for i in ff:
i = i[1:]
if "year" in i:
pass
else:
t.append((i[0], i[1], i[3], i[4], i[6], i[5], i[8]))
cursor.executemany(sql, t)
conn.commit()
end=time.strftime("%M:%S")
print(now+","+end)
The file of "base.csv" size is 21.7 MB and 30374 rows. When I execute the above code, It will take 929 seconds to completed. This is meaning only 32.7 rows/second, it too slow. Who can to help me find out the reason?Thank a lot. :-)
来源:https://stackoverflow.com/questions/56097768/pymssql-executemany-insert-value-very-slow