pymssql executemany插入值非常慢

时间:2019-05-12 09:16:01

标签: python sql-server-2012 pymssql executemany

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)

“ base.csv”大小的文件为21.7 MB和30374行。当我执行以上代码时,将需要929秒才能完成。这意味着只有32.7行/秒,这太慢了。谁能帮助我找出原因?非常感谢。 :-)

enter image description here

0 个答案:

没有答案