Dataframe to SQL Server using Execute many from pyodbc(使用Execute More From pyodbc将数据帧传输到SQL Server)
本文介绍了使用Execute More From pyodbc将数据帧传输到SQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Pyodbc将数据从DataFrame加载到SQL Server,它逐行插入,速度非常慢。
我已经尝试了两种在网上找到的方法(中等),但我没有发现任何性能改善。
尝试在SQL Azure中运行,因此SQL AlChemy不是一种简单的连接方法。请找到我遵循的方法,以及是否有其他方法可以提高批量加载的性能。
方法1
cursor = sql_con.cursor()
cursor.fast_executemany = True
for row_count in range(0, df.shape[0]):
chunk = df.iloc[row_count:row_count + 1,:].values.tolist()
tuple_of_tuples = tuple(tuple(x) for x in chunk)
for index,row in ProductInventory.iterrows():
cursor.executemany("INSERT INTO table ([x]],[Y]) values (?,?)",tuple_of_tuples)
方法2
cursor = sql_con.cursor()
for row_count in range(0, ProductInventory.shape[0]):
chunk = ProductInventory.iloc[row_count:row_count + 1,:].values.tolist()
tuple_of_tuples = tuple(tuple(x) for x in chunk)
for index,row in ProductInventory.iterrows():
cursor.executemany(""INSERT INTO table ([x]],[Y]) values (?,?)",tuple_of_tuples
谁能告诉我为什么性能没有提高1%?仍然需要同样的时间
推荐答案
尝试在SQL Azure中运行,因此SQL AlChemy不是一种简单的连接方法。
也许你只需要先跨过那个障碍。然后您可以使用 pandas to_sql和fast_executemany=True
。例如
from sqlalchemy import create_engine
#
# ...
#
engine = create_engine(connection_uri, fast_executemany=True)
df.to_sql("table_name", engine, if_exists="append", index=False)
如果您有一个有效的pyodbc连接字符串,则可以将其转换为SQLAlChemy连接URI,如下所示:
connection_uri = 'mssql+pyodbc:///?odbc_connect=' + urllib.parse.quote_plus(connection_string)
这篇关于使用Execute More From pyodbc将数据帧传输到SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:使用Execute More From pyodbc将数据帧传输到SQL Server


猜你喜欢
- 我如何卸载 PyTorch? 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01