2024-06-08 06:13:17 发布
网友
当我们通过pandas将文件加载到SQL Server时,在创建SQLAlchemy引擎对象时,我已经开始使用这个fast_executemany属性。我理解它在加载数据时的好处
fast_executemany
是否存在不建议为SQL Server任务启用它的情况?也许只要一直做单件插入就好了?我还是不知道执行官的速度会有多快
Are there situations when it is not recommended to have it enabled for SQL Server tasks? Maybe if only doing singleton inserts all the time?
否,fast_executemany=True如果调用pyodbc的.execute()方法,则对单行插入没有影响。一个例子是this pandas issue,其中具有单行(.execute())和多行(.executemany())的数据帧的行为不同。解决这个问题的方法是让pandas始终调用.executemany(),即使数据帧只有一行。(还请注意fast_executemany=True不会导致问题,而是解决问题。)
fast_executemany=True
.execute()
.executemany()
但是,在特定情况下fast_executemany=True和.to_sql()还有一些其他已知问题:
.to_sql()
如果数据库是使用默认的“…\u SC”排序规则定义的,例如
cnxn.execute(f"CREATE DATABASE {db_name} COLLATE Latin1_General_100_CI_AS_SC")
然后.to_sql()对于长度超过2000个字符的字符串将失败
pyodbc issue on GitHub
相对稀疏的数据帧(包含大量类似NULL的值,如None、NaN、NaT等)会降低.executemany()的插入性能,尽管最坏的情况是fast_executemany=True的运行速度与fast_executemany=False一样慢
None
NaN
NaT
fast_executemany=False
[n]varchar(max)
to_sql()默认情况下,将字符串列创建为varchar(max),这可能会导致fast_executemany=True内存膨胀
to_sql()
varchar(max)
否,
fast_executemany=True
如果调用pyodbc的.execute()
方法,则对单行插入没有影响。一个例子是this pandas issue,其中具有单行(.execute()
)和多行(.executemany()
)的数据帧的行为不同。解决这个问题的方法是让pandas始终调用.executemany()
,即使数据帧只有一行。(还请注意fast_executemany=True
不会导致问题,而是解决问题。)但是,在特定情况下
fast_executemany=True
和.to_sql()
还有一些其他已知问题:一,。具有默认“补充字符”(_SC)排序规则的数据库
如果数据库是使用默认的“…\u SC”排序规则定义的,例如
然后
.to_sql()
对于长度超过2000个字符的字符串将失败pyodbc issue on GitHub
二,。具有大量类空值的数据帧
相对稀疏的数据帧(包含大量类似NULL的值,如
None
、NaN
、NaT
等)会降低.executemany()
的插入性能,尽管最坏的情况是fast_executemany=True
的运行速度与fast_executemany=False
一样慢pyodbc issue on GitHub
三,。使用
[n]varchar(max)
列增加内存消耗to_sql()
默认情况下,将字符串列创建为varchar(max)
,这可能会导致fast_executemany=True
内存膨胀pyodbc issue on GitHub
相关问题 更多 >
编程相关推荐