是否可以将不同的插入/IDbCommands 与 Nhibernate 一起批处理?

发布于 2024-12-20 12:27:46 字数 1440 浏览 1 评论 0原文

示例 1,无论您的配置如何,以下插入都会产生 2 个批次:

INSERT INTO <b>entitytable1</b> (someInt) VALUES (1)
INSERT INTO <b>entitytable2</b> (someInt) VALUES (3)
-- Notice tables are <b>different</b>
-- Results in <b>2</b> round trips to the db
-- C# code that can generate such statements:
   session.Save(typeof(entitytable<b>1</b>), new entitytable<b>1</b>{someInt = 1})
   session.Save(typeof(entitytable<b>2</b>), new entitytable<b>2</b>{someInt = 3})
   transction.Commit();

但是,在下面的示例 2 中,插入会产生 1 个批次:

INSERT INTO <b>entitytable1</b> (someInt) VALUES (1)
INSERT INTO <b>entitytable1</b> (someInt) VALUES (3)
-- Notice tables are the <b>same</b>
-- Results in <b>1</b> round trip to the db
-- C# code that can generate such statements:
   session.Save(typeof(entitytable<b>1</b>), new entitytable<b>1</b>{someInt = 1})
   session.Save(typeof(entitytable<b>1</b>), new entitytable<b>1</b>{someInt = 3})
   transction.Commit();

是否可以使示例 1 与 2 一样工作,即批量发送不同的插入一起到数据库(例如1个往返)?

针对少数人的批处理定义:将多个语句/插入组合在一起(最多 Ado_Batch_size)在一次往返中发送到数据库。

上面显示的 sql 当然是由 Nhibernate 生成的!这就是您在 NHProfiler 上看到的内容。

版本:休眠3.2

Example 1, the following insert results in 2 batches regardless of your configuration:

INSERT INTO <b>entitytable1</b> (someInt) VALUES (1)
INSERT INTO <b>entitytable2</b> (someInt) VALUES (3)
-- Notice tables are <b>different</b>
-- Results in <b>2</b> round trips to the db
-- C# code that can generate such statements:
   session.Save(typeof(entitytable<b>1</b>), new entitytable<b>1</b>{someInt = 1})
   session.Save(typeof(entitytable<b>2</b>), new entitytable<b>2</b>{someInt = 3})
   transction.Commit();

However, in example 2, below, the inserts result in 1 batch:

INSERT INTO <b>entitytable1</b> (someInt) VALUES (1)
INSERT INTO <b>entitytable1</b> (someInt) VALUES (3)
-- Notice tables are the <b>same</b>
-- Results in <b>1</b> round trip to the db
-- C# code that can generate such statements:
   session.Save(typeof(entitytable<b>1</b>), new entitytable<b>1</b>{someInt = 1})
   session.Save(typeof(entitytable<b>1</b>), new entitytable<b>1</b>{someInt = 3})
   transction.Commit();

Is it possible to get example 1 to work as 2, that is, to send different inserts batched together to the db (e.g. 1 roundtrip)?

Batching definition, for the few: To send multiple statements/inserts grouped together, up to Ado_Batch_size, to the database in one round trip.

The sql shown above is generated by Nhibernate of course!. It is what you would see on NHProfiler.

Version: Nhibernate 3.2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

暮倦 2024-12-27 12:27:46

根据 nHibernate 批量插入不适用于关联(原贴已经找到),看来当前版本的 NHibernate 不支持这一点。

文章 Profiling NHibernate Batching 似乎证实了这一点,并建议使用 无状态会话(不幸的是忽略关联)。

或者,此答案建议使用 HQL。

警告:我不是 NHibernate 用户。请务必使用 NHProfiler 检查实际行为。

As per nHibernate batch insert doesn't work with associations (which the original poster already found), it appears that this is not supported by the current version of NHibernate.

The article Profiling NHibernate Batching seems to confirm this, and suggests using stateless sessions (which unfortunately ignore associations).

Alternately, this answer suggests using HQL.

Caveat: I am not an NHibernate user. Be sure to check the actual behavior with NHProfiler.

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