在 CastleProject ActiveRecord 中批量保存
我需要在数据库中保存数千条记录。我正在使用 CastleProject ActiveRecord。存储这么多对象的周期运行时间太长。
是否可以使用 ActiveRecord 批量运行保存?提高性能的推荐方法是什么?
I need to save thousand of records in a database. I am using CastleProject ActiveRecord. The cycle which stores that amount of objects works too long.
Is it possible to run saving in a batch using ActiveRecord? What is recommended way to improve performance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看来我找到了解决方案。主要有两个步骤:
It seems I found the solution. There are two main steps:
ActiveRecord(以及大多数 ORM)不太适合批处理操作。由于所有变更跟踪都在 ORM 内部进行,它确实会减慢您的速度。
为了获得最大性能,我会寻求直接的 ADO.NET 或某种 SQL 批量导入。
ActiveRecord (and most ORM's) are not a good fit for batch operations. With all of the change tracking going on inside an ORM, it can really slow you down.
For maximum performance, I would look to either straight ADO.NET or some kind of SQL bulk import.
我同意帕特里克的观点。只是想提一个中间解决方案:无状态会话。请参阅:
I agree with Patrick. Just wanted to mention a middle-ground solution: a stateless session. See: