The first and most dramatic performance issue you may encounter with NHibernate is to create a new factory session for each session you create. Only one factory session must be created for each application execution, and all sessions must be created using the factory.
Along these lines, you should continue to use the same session, if that makes sense. It depends on the application, but for most web applications it is recommended to use one session for each request. If you often give up your session, you do not get the benefits of your cache. Intelligent use of the session cache can change the procedure with a linear (or worse) number of requests for a constant number without much work.
Equally important, you want to make sure you're lazy loading object references. If you do not, all graph objects can be loaded even for the simplest queries. There are only certain reasons not to do this, but it is always best to start with lazy loading and switch as needed.
This leads us to impatient collection, the opposite of lazy loading. When you move the hierarchy of objects or traverse collections, you can easily lose information about how many queries you make, and you get an exponential number of queries. Easy fetching can be done on request based on a FETCH JOIN request. In rare cases, for example, if there is a certain pair of tables, you always choose a connection, consider abandoning lazy loading for these relationships.
As always, SQL Profiler is a great way to find queries that are slow or run repeatedly. At my last job, we had a development function that also took into account page requests. A large number of requests for a subroutine is the most obvious indicator that your program does not work with NHibernate. If the number of queries per routine or query looks good, perhaps you will configure the database settings; make sure you have enough memory to store execution plans and data in the cache, properly index your data, etc.
One tricky issue we are facing is SetParameterList (). The function makes it easy to pass a list of parameters to the request. NHibernate implemented this by creating one parameter for each element passed. This leads to a different tariff plan for each number of parameters. Our execution plans almost always came out of the cache. In addition, numerous parameters can significantly slow down the request. We performed the usual NHibernate hacking to send items as a delimited list in one parameter. The list was split in SQL Server by the table value function, which our hack automatically inserted into the IN clause of the query. There may be other land mines depending on your application. SQL Profiler is the best way to find them.
Chuck Sep 15 '08 at 22:03 2008-09-15 22:03
source share