Nhibernate and Bulk Import Performance

Written By : Lam Chan

When it comes to ORMs, Nhibernate and Entity Framework ranks the worst in performance because they do so much.  These frameworks are really geared for small transaction processing such as web request work where you are fetching a small piece of information with clear direction from a top down level.  When it comes to bulk processing, the behavior for data look up is significant different because you are having to bring in large data sets to apply conditional logic with, most of this data is throw away and only a tiny bit would be used in conjunction with the bulk import data.

Killer in performance

A big killer in performance in NHibernate is the ISession bag.  Anything you query through ISession will be stored in memory and later inspected for dirtiness if a Session.Flush() or a transactional commit happens.  

How to fix it

First, you need to look at NHibernate stateless session here.  The only drawback of stateless session is it only brings the object mapping feature of NH to the table which is all you really need anyways if you were using these data set for look ups.  Then for any lookup data, use the IStatelessSession object in lieu of ISession and you will see a huge increase in performance without doing anything else.

For any queries that still require the stateful management of NHibernate, look into using .ToFuture() calls.  This will help you aggregate multiple sql queries into a batch.  It is also helpful in avoiding cartesian result sets when Querying with FetchMany() and ThenFetchMany() query structures.


Tags: ,

No comments yet.

Post your comment