我正在使用pyodbc从MSQL Server获取一些数据。工作正常,除了检索大量数据时:它真的很慢。
我有大约4000行,这不是一个非常大的数字。
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=192.168.1.10;DATABASE=MyDB;Trusted_Connection=yes;')
cur = conn.cursor()
cur.execute('SELECT * FROM myTable')
我尝试了以下内容:
time1 = datetime.datetime.now()
a = cur.fetchall()
time2 = datetime.datetime.now()
print time2 - time1
print len(a)
0:00:27.286000
time1 = datetime.datetime.now()
for i in range(0, 4017):
cur.fetchone()
time2 = datetime.datetime.now()
print time2 - time1
0:00:21.196000
time1 = datetime.datetime.now()
allIDRows = list(cur.fetchmany(4017))
time2 = datetime.datetime.now()
print time2 - time1
0:00:23.845000
time1 = datetime.datetime.now()
a = cur.fetchmany(4017)
time2 = datetime.datetime.now()
print time2 - time1
0:00:24.348000
def ResultIter(cursor, arraysize=100):
'An iterator that uses fetchmany to keep memory usage down'
while True:
results = cursor.fetchmany(arraysize)
if not results:
break
for result in results:
yield result
time1 = datetime.datetime.now()
for result in ResultIter(cur):
pass
time2 = datetime.datetime.now()
print time2 - time1
0:00:31.181000
知道为什么它如此缓慢以及我应该做些什么才能让它变得快速? (好吧,如果有可能......)
由于