nhibernate更改批次大小 [英] nhibernate alternates batch size

查看:73
本文介绍了nhibernate更改批次大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用NHibernate执行查询时,如果将其设置为大于实际返回的结果,则似乎并没有考虑批量大小.

When performing a query with NHibernate does not seem to be respecting the batch-size if it is set to more than the results actually returned.

我正在使用最新版本的NHibernate 2.1.0.4000和Linq的GA到NHibernate.我有一个类似于Order的对象结构,其中有OrderLines的集合. OrderLines已定义为带有以下xml的包:

I am using the latest version of NHibernate 2.1.0.4000 and the GA of Linq to NHibernate. I have an object structure similar to the Order which has a collection of OrderLines. The OrderLines have been defined as a bag with the following xml:

<bag name="OrderLines" access="field.camelcase" table="MyDatabase.OrderLines" lazy="true"   batch-size="50">
    <key column="OrderId"/>
    <one-to-many class="OrderLine"/>
</bag>

如果我查询订单并获得50条结果,它会在一次查询中正确选择所有OrderLines,但是如果我获得少于50条结果,则它似乎不符合所定义的批次大小.

If I query for Orders and get 50 results back it correctly selects all the OrderLines in a single query, but if I get less than 50 results back it does not seem to respect the defined batch size.

例如如果我得到40个结果而不是50个,则执行3个查询,批处理大小分别为25、12和3

E.g. If I get 40 results back instead of 50 if performs 3 queries with a batch size of 25, 12 and 3

看起来好像正在尝试猜测要使用的正确批处理大小(即,它先做1/2的批处理大小,然后再做1/2的剩余处理,依此类推).我希望它始终执行50个批处理大小,如果数量较少,则使该批处理大小尽可能地大,在这种情况下,应使批处理大小为40个.

Which looks like it is trying to guess the correct batch size to use (ie it does 1/2 the batch size first, then 1/2 the remainder etc). I would expect it to perform a batch size of 50 all the time and if there are less then make the batch size as large as it can, in this case a batch size of 40.

在所有情况下,如何让NHibernate遵守我定义的批量大小?

How can I get NHibernate to respect the batch size I have defined in all cases?

推荐答案

我在碰到同样的奇怪行为. 我发现人们也在Hibernate(Java)的同一件事上绊脚石.

I was stumbling over the same strange behavior. I found that people were stumbling over the same thing in Hibernate (Java) too.

此处记录了Hibernate的行为:

The behavior is documented here for Hibernate:

http://opensource.atlassian.com/projects/hibernate/browse/HB-1457
https://forum.hibernate.org/viewtopic.php?p=2233747#2233747
https://forum.hibernate.org/viewtopic.php?p=2422139

http://opensource.atlassian.com/projects/hibernate/browse/HB-1457
https://forum.hibernate.org/viewtopic.php?p=2233747#2233747
https://forum.hibernate.org/viewtopic.php?p=2422139

我想这种行为是直接从Hibernate移植的.

I guess this behavior is ported directly from Hibernate.

简而言之:
由Hibernate准备的少数批处理SQL语句.每个都有固定的批处理大小,用于定义IN子句中的参数数量. 然后,Hibernate使用这些准备好的语句来满足批处理加载. 您在映射文件中指定为批量大小的数字仅定义最大值.可能发生的批量大小.

In short:
There are only a few SQL statements for batch-fechting prepared by Hibernate. Each with a fixed batch-size which definines the count of parameters in the IN-clause. Hibernate then uses these prepared statements for satisfying the batch-loading. The number you specify as batch-size in the mapping file, only defines the max. batch size that can occur.

例如,给定的batch-size = 1000.如果您有200个父实体,并希望加载这些子实体,则nHibernate决定使用4条语句:其中一个在IN子句中具有125、62、10和3个参数(总计200个).

For instance given batch-size=1000. If you have 200 parent entities and want to load the child-collections of these, nHibernate decides to use 4 statements: one with 125, 62, 10 and 3 parameters in the IN clause (summing up to 200).

但是,如果您只有125个父实体,则hibernate决定仅使用一条语句,该语句具有125个参数.

However if you only have 125 parent entities, then hibernate decides to use only one statement, the one with 125 parameters.

(以上数字是我在NH 2.1中的观察结果)

(The numbers above are my observations in NH 2.1)

其背后原因:(根据链接的论坛讨论)
当最大批处理大小很大时,创建许多不同的PreparedStatement会对性能产生负面影响,这点值得关注. (PreparedStatements是一个Java构造,我想知道这种对性能的关注是否同样适用于.NET)

The reason behind this: (according to the linked forum discussion)
Concern about the negative performance impact of creating many different PreparedStatements when the maximum batch-size is large. (PreparedStatements are a Java construct, I wonder if this performace concern is equally valid for .NET)

这篇关于nhibernate更改批次大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆