使用代码映射优化Nhibernate查询

时间:2012-01-29 18:17:45

标签: c# nhibernate

我有一个nhibernate 3.2查询,返回前500个干预项目(在原始查询中有一个过滤器)

var (from interv in Session.Query<Intervention>()
                .Fetch(rep => rep.ReponsePointVerification)
                orderby interv.DateModification
                select interv)
                .Take(500)
                .ToList();

然后我迭代所有值并使用ReponsePointVerification值。

// a (very) simplified example
foreach (var intervention in listeInterventions)
  {
  foreach (var reponse in intervention.ReponsePointVerification)
    {

    }

  listeInterventionsws.Add(interventionws);
}

此查询已经过优化但效果不佳,因为Take方法需要500行,如果有ReponsePointVerification值,我将不会有500个Intervention项目。

所以,如果我想让它发挥作用,我有两种方法:

  • 删除Take(500)并仅采取前500次干预(数据库增长时很重)
  • 删除提取,但如果我有500 + 1查询。

nhibernate有办法处理这种情况吗?

此致

修改

谢谢迭戈,它有效。

好吧,我忘了提到我在nh 3.2

中使用了代码映射

按代码映射的批处理大小可以像这样配置:

Bag(x => x.ReponsePointVerification, map =>
{
  map.Key( k => k.Column( "IdIntervention" ) );
  map.BatchSize(50);
}, rm => rm.OneToMany()); 

比照http://puredotnetcoder.blogspot.com/2011/07/mapping-conventions-with-mapping-by.html

1 个答案:

答案 0 :(得分:1)

使用batch-size对受影响的馆藏进行映射,而不是Fetch

在90%的案例中,这是一个更好的解决方案。

(我打算链接到相关的文档部分,但网站目前已关闭)