Ramon Smits
Ramon Smits

Reputation: 2618

NHibernate slow when reading large bag

I am having performance problems where an aggregate has a bag which has a large number of entities (1000+). Usually it only contains at most 50 entities but sometimes a lot more.

Using NHibernate profiler I see that the duration to fetch 1123 records of this bag from the database is 18ms but it takes NHibernate 1079ms to process it. Problem here is that all those 1123 records have one or two additional records. I fetch these using fetch="subselect" and fetching these additional records takes 16ms to fetch from the database and 2527ms processing by NHibernate. So this action alone takes 3,5 seconds which is way too expensive.

I read that this is due the fact that updating the 1st level cache is the problem here as it performance gets slow when loading a lot of entities. But what is alot? NHibernate Profiler says that I have 1145 entities loaded by 31 queries (which is in my case the absolute minimum). This number of entities loaded does not seem like a lot to me.

In the current project we are using NHibernate v3.1.0.4000

Upvotes: 3

Views: 845

Answers (1)

Stefan Steinegger
Stefan Steinegger

Reputation: 64638

I agree, 1000 entities aren't too many. Are you sure that the time isn't used in one of the constructors or property setters? You may stop the debugger during the load time to take a random sample where it spends the time.

Also make sure that you use the reflection optimizer (I think it's turned on by default).

I assume that you measure the time of the query itself. If you measure the whole transaction, it most certainly spends the time in flushing the session. Avoid flushing by setting the FlushMode to Never (only if there aren't any changes in the session to be stored) or by using a StatelessSession.

A wild guess: Removing the batch-size setting may even make it faster because it doesn't need to assign the entities to the corresponding collections.

Upvotes: 2

Related Questions