是否可以将不同的插入/IDbCommands 与 Nhibernate 一起批处理?
示例 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 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.