I’m doing some tests with nhibernate and I’m modifying batch_size to get bulk inserts.
I’m using mssql2005 and using the northwind db. I created 1000 object and insert them to the database. I’ve changed the values of batch_size from 5 to 100 but found no change in the performance. I’m getting value of around 300ms. Using the sql profiler, I see that 1000 sql insert statements at the sever side. Please help.
app.config
<property name='adonet.batch_size'>10</property>
Code
public bool MyTestAddition(IList<Supplier> SupplierList) { var SupplierList_ = SupplierList; var stopwatch = new Stopwatch(); stopwatch.Start(); using (ISession session = dataManager.OpenSession()) { int counter = 0; using (ITransaction transaction = session.BeginTransaction()) { foreach (var supplier in SupplierList_) { session.Save(supplier); } transaction.Commit(); } } stopwatch.Stop(); Console.WriteLine(string.Format('{0} milliseconds. {1} items added', stopwatch.ElapsedMilliseconds, SupplierList_.Count)); return true; }
The following is a great post on batch processing in Hibernate, which is what NHibernate is based upon and closely follows:
http://relation.to/Bloggers/BatchProcessingInHibernate
As you can see, the suggested actions are to set a reasonable batch size in the config, which you have done, but to also call
session.flush()andsession.clear()every 20 or so records.We have employed this method ourselves and can now create and save 1000+ objects in seconds.