This is part of a series of posts about NHibernate Pitfalls. See the entire collection here.
NHibernate allows setting a batch size for some entity or to collections of some entity. The entity setting applies to lazy loaded many to one or one to one associations, and the collection applies to lazy loaded one to many or many to many collections. This is something that might improve the performance of your application.
In the first case, what it does is, if there are more than one loaded entities of some type with some lazy loaded association which wasn’t already loaded, and one tries to access one such association property, triggering its loading, then up to batch-size identical associations will be fetched at the same time, on the loaded entities of the same type. For example:
This may be regarded as as optimization, because, if you are certain that you will need all Customer properties, then all of them are loaded when the first is accessed.
The same thing happens with collections:
However, this may not be what you expect, because it might return a lot of data, and there’s a chance you wont even use it.
If you are not certain, disable batching by not setting a batch size either at the entity and the collection level. Be aware, however, of its consequences, and consider it wisely.