oursgris
oursgris

Reputation: 2882

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)

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.

// 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.

So if I want to make it work, I have 2 methods :

Does nhibernate have a method to handle that case ?

Regards

Edit

Thank you Diego, it worked.

Well I forgot to mention that I used mapping by code in 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

Upvotes: 2

Views: 700

Answers (1)

Diego Mijelshon
Diego Mijelshon

Reputation: 52725

Use batch-size on the mapping of the affected collections instead of Fetch.

It's a better solution in 90% of the cases.

(I was going to link to the relevant docs section, but the site is down at the moment)

Upvotes: 1

Related Questions