您正在使用Microsoft的“ODBC驱动程序…用于SQL Server”,因此
fast_executemany
应使用pyodbc 4.0.21。但是,您可以在仍使用
DataFrame#to_sql
通过使用SQLAlchemy执行事件,如所示
this question
.
示例:以下代码没有利用
fast\u ExecuteMy
import pandas as pd
from sqlalchemy import create_engine
import time
engine = create_engine('mssql+pyodbc://@SQL_panorama')
# test environment
num_rows = 1000
df = pd.DataFrame(
[[x, f'row{x:03}'] for x in range(num_rows)],
columns=['id', 'txt']
)
#
cnxn = engine.connect()
try:
cnxn.execute("DROP TABLE df_to_sql_test")
except:
pass
cnxn.execute("CREATE TABLE df_to_sql_test (id INT PRIMARY KEY, txt NVARCHAR(50))")
# timing test
t0 = time.time()
df.to_sql("df_to_sql_test", engine, if_exists='append', index=False)
print(f"{num_rows} rows written in {(time.time() - t0):.1f} seconds")
结果:
1000 rows written in 25.2 seconds
添加SQLAlchemy执行事件处理程序可以显著减少执行时间
import pandas as pd
from sqlalchemy import create_engine, event
import time
engine = create_engine('mssql+pyodbc://@SQL_panorama')
@event.listens_for(engine, 'before_cursor_execute')
def receive_before_cursor_execute(conn, cursor, statement, params, context, executemany):
if executemany:
cursor.fast_executemany = True
# test environment
num_rows = 1000
df = pd.DataFrame(
[[x, f'row{x:03}'] for x in range(num_rows)],
columns=['id', 'txt']
)
#
cnxn = engine.connect()
try:
cnxn.execute("DROP TABLE df_to_sql_test")
except:
pass
cnxn.execute("CREATE TABLE df_to_sql_test (id INT PRIMARY KEY, txt NVARCHAR(50))")
# timing test
t0 = time.time()
df.to_sql("df_to_sql_test", engine, if_exists='append', index=False)
print(f"{num_rows} rows written in {(time.time() - t0):.1f} seconds")
结果:
1000 rows written in 1.6 seconds
有关此方法的更完整讨论,请参阅
this answer
.