简体   繁体   English

通过代码映射优化Nhibernate查询

[英]Optimize Nhibernate query with Mapping by code

I have a nhibernate 3.2 query which returns the first 500 Intervention items (in the original query there is a filter) 我有一个nhibernate 3.2查询,它返回前500个干预项(在原始查询中有一个过滤器)

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

Then I iterate on all value and use ReponsePointVerification value. 然后,我迭代所有值并使用ReponsePointVerification值。

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

    }

  listeInterventionsws.Add(interventionws);
}

This query is optimized but it doesn't work well because the Take method will take 500 lines and if there are ReponsePointVerification value, I won't have my 500 Intervention items. 此查询已优化,但效果不佳,因为Take方法将占用500行,并且如果有ReponsePointVerification值,则不会有500个干预项目。

So if I want to make it work, I have 2 methods : 因此,如果我想使其工作,我有2种方法:

  • remove Take(500) and take only the first 500 interventions (heavy when the database grow) 删除Take(500)并仅进行前500次干预(数据库增长时很重)
  • Remove the fetch but if I will have 500 + 1 query. 删除获取,但是如果我要查询500 + 1。

Does nhibernate have a method to handle that case ? nhibernate是否有处理这种情况的方法?

Regards 问候

Edit 编辑

Thank you Diego, it worked. 谢谢迭戈,它奏效了。

Well I forgot to mention that I used mapping by code in nh 3.2 好吧,我忘了提到我在nh 3.2中使用代码映射

The batch size with mapping by code can be configured like this : 通过代码映射的批处理大小可以这样配置:

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

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

Use batch-size on the mapping of the affected collections instead of Fetch . 在受影响的集合的映射上使用batch-size而不是Fetch

It's a better solution in 90% of the cases. 在90%的情况下,这是一个更好的解决方案。

(I was going to link to the relevant docs section, but the site is down at the moment) (我原本打算链接到相关的文档部分,但目前该网站已关闭)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM