使用fast_executemany属性的缺点还是缺点?

2024-06-08 06:13:17 发布

您现在位置:Python中文网/ 问答频道 /正文

当我们通过pandas将文件加载到SQL Server时,在创建SQLAlchemy引擎对象时,我已经开始使用这个fast_executemany属性。我理解它在加载数据时的好处

是否存在不建议为SQL Server任务启用它的情况?也许只要一直做单件插入就好了?我还是不知道执行官的速度会有多快


Tags: 文件数据对象引擎pandassql属性server
1条回答
网友
1楼 · 发布于 2024-06-08 06:13:17

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.to_sql()还有一些其他已知问题:

一,。具有默认“补充字符”(_SC)排序规则的数据库

如果数据库是使用默认的“…\u SC”排序规则定义的,例如

cnxn.execute(f"CREATE DATABASE {db_name} COLLATE Latin1_General_100_CI_AS_SC")

然后.to_sql()对于长度超过2000个字符的字符串将失败

pyodbc issue on GitHub

二,。具有大量类空值的数据帧

相对稀疏的数据帧(包含大量类似NULL的值,如NoneNaNNaT等)会降低.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

相关问题 更多 >

    热门问题