在 CastleProject ActiveRecord 中批量保存

发布于 2024-08-25 13:38:24 字数 122 浏览 5 评论 0原文

我需要在数据库中保存数千条记录。我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哆啦不做梦 2024-09-01 13:38:24

看来我找到了解决方案。主要有两个步骤:

  1. 将批处理添加到配置文件:
  2. 使用 TransactionScope 来保存对象:

using (TransactionScope scope = new TransactionScope())
{
  for (int i = 0; i < 100; i++)
  {
    Contact contact = new Contact();
    contact.Save();
  }
}

It seems I found the solution. There are two main steps:

  1. Add a batching to config file: <add key="hibernate.batch_size" value="100" />
  2. Use TransactionScope around saving your objects:

using (TransactionScope scope = new TransactionScope())
{
  for (int i = 0; i < 100; i++)
  {
    Contact contact = new Contact();
    contact.Save();
  }
}
去了角落 2024-09-01 13:38:24

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文