SQLAlchemy 和 max_allowed_packet 问题
由于我的应用程序的性质,我需要支持将大量数据快速插入数据库。使用executemany() 可以提高性能,但有一个警告。例如,MySQL 有一个名为 max_allowed_packet 的配置参数,如果我的插入查询的总大小超过其值,MySQL 就会抛出错误。
问题#1:有没有办法告诉 SQLAlchemy 将数据包分割成几个较小的数据包?
问题#2:如果其他 RDBS 也有类似的限制,我应该如何解决这些限制?
PS 我之前已经发布过这个问题,但当我错误地认为我可能不会遇到这个问题时,我删除了它。遗憾的是,事实并非如此。
Due to the nature of my application, I need to support fast inserts of large volumes of data into the database. Using executemany() increases performance, but there's a caveat. For example, MySQL has a configuration parameter called max_allowed_packet, and if the total size of my insert queries exceeds its value, MySQL throws an error.
Question #1: Is there a way to tell SQLAlchemy to split the packet into several smaller ones?
Question #2: If other RDBS have similar constraints, how should I work around them as well?
P.S. I had posted this question earlier but deleted it when I wrongly assumed that likely I will not encounter this problem after all. Sadly, that's not the case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最近遇到了类似的问题,并使用了 - 不是很优雅 - 解决方法:
max_allow_packets
的值,如果我找不到它,则最大值设置为默认值。max_allow_packets
的 75%(为了安全起见,SQL 查询也会占用空间)。这种方法并不是很漂亮,但对我来说却完美无缺。
I had a similar problem recently and used the - not very elegant - work-around:
max_allow_packets
, if I can't find it, the maximum is set to a default value.max_allow_packets
(as SQL queries will take up space as well, just to be on the safe side).This approach is not really beautiful, but it worked flawlessly for me.