我尝试使用Pyodbc将数据从dataframe加载到SQL Server,Pyodbc逐行插入数据,速度非常慢
我尝试了两种在线(中等)的方法,但没有发现性能有任何改善
正在尝试在SQLAzure中运行,所以SQLAlchemy不是一种简单的连接方法。请找出我所遵循的方法,以及是否有其他方法可以提高批量加载的性能
方法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%吗?这仍然需要同样的时间
也许你只需要先克服这个障碍。然后您可以将pandasto_sql与
fast_executemany=True
一起使用。比如说如果您有一个工作的pyodbc连接字符串,则可以将其转换为SQLAlchemy连接URI,如下所示:
有几件事
为什么要迭代ProductInventory两次
executemany
调用不应该在构建了整个元组或一批元组之后发生吗pyodbc文档指出,“使用fast_executemany=False运行executemany()通常不会比直接运行多个execute()命令快多少。”因此,您需要在这两个示例中设置
cursor.fast_executemany=True
(有关更多详细信息/示例,请参见https://github.com/mkleehammer/pyodbc/wiki/Cursor)。我不确定为什么在示例2中省略了它下面是一个例子,说明你如何完成我认为你正在努力做的事情。
math.ceil
和end_idx = ...
中的条件表达式占最后一批,可能是奇数大小。因此,在下面的示例中,您有10行,批大小为3,因此最终有4个批,最后一个只有1个元组示例输出:
相关问题 更多 >
编程相关推荐