NHibernate Batch Size - NHibernate

What is NHibernate Batch Size?

In this section, we will be covering the batch size update. The batch size allows you to control the number of updates that go out in a single round trip to your database for the maintained databases.

  • The update batch size has been defaulted as of NHibernate 3.2.
  • But if you are using a previous version or essential to tune your NHibernate application, you must look at the update batch size, which is an actual useful parameter that can be used to tune NHibernate's performance.
  • Really batch size controls how a lot of inserts to push out in a group to a database.
  • At the instant, only SQL Server and Oracle support this option since the primary database provider needs to support query batching.

Let’s have a look into a simple example in which we have set the batch size to 10 that will insert 10 records in a set.

Here is the complete implementation in which 25 records will be added to the database.

Currently let’s run your application and you see all those updates are hopping over to NHibernate profiler. We have 26 separate round trips to the database 25 for insertion and one retrieving the list of students.

Currently, why is that? The aim is because NHibernate needs to do a select scope identity as we are using the native identifier generation strategy in the mapping file for ID as shown in the following code.

So we need to use a altered method such as the guid.comb method. If we're going to go to guid.comb, we need to go over to our customer and change this to a guid. So that will work fine. Now let’s change from the native to guid.comb using the following code.

So it's the database that's answerable for making those IDs. The only way NHibernate can find out what ID was made was to select it directly afterwards. Besides, if we have created a batch of students, it will not be able match up the ID of the student that was created.

We must update our database. Let’s drop the student table and create a new table by specifying the following query, so go the SQL Server Object Explorer and right-click on the database and select the New Query… option.

It will open the query editor and and then specify the following query.

This question will first drop the current student table and then create a new table. As you can see that we have used UNIQUEIDENTIFIER rather than using an integer primary key as an ID.

Perform this query and then go to the Designer view and you will see that now the ID is created with a unique identifier as shown in the following image.


Now we need to remove the ID from the program.cs file, while inserting data, because now it will generate the guids for it automatically.

Currently run the application for a second time and have a look at the NHibernate profiler. Now the NHibernate profiler rather than creating 26 round trips will make only four.


It's implanted ten rows into the table, then additional ten rows, and later the lasting five. And after obligate, it has inserted one more for retrieving all the records.

  • So it's divided it up into groups of ten, as best it can.
  • So if you're doing a lot of inserts, this can vividly recover the insert performance in your application, because you can batch it up.
  • This is since NHibernate assigns those guids itself using the guid.comb algorithm, and it doesn't have to trust on the database to do this.
  • So by means of the batch size is a great way to tune it.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

NHibernate Topics