Technology with opinion

Thursday, April 16, 2009

NHibernate Performance Tuning

OR/M as a central part of your application architecture will require tuning over time.  Below are some tweaks which can have incremental to significant performance effects on your code.
  1. Enable 2nd level cache and have a strategy for using it for some of your classes. reference 
  2. Enable batch processing for persistence of objects.  This will allow NHibernate to send batches of your transactions (Inserts, Updates and Deletes) to the server in a batch. reference 
  3. Have a good strategy for flushing and transaction management.  Remember that if you commit a transaction that you do not need to flush the object.  reference 
  4. Make read-only classes immutable. reference 
  5. Understand lazy loading and use effectively, for instance when you may but not always need an associated object(s) reference 
  6. To bypass lazy loading understand eager fetching and use when you know you need associated object(s) and want to bypass lazy loading. reference 
  7. Remove any columns and properties that you will never or should never need or use.
  8. Using value type objects for your properties and components as opposed to reference types, when you have a choice, this will lower memory footprint.
  9. Avoid composite keys (exception legacy databases).  Composite keys are problematic for database design as well as OR/M therefore avoid them.  reference 
  10. Consider your logging strategy for production (less verbose) and usually disable show_sql in production.
Orin's NHib Profiler can also help you find improvements you can make to your configuration of NHibernate.

No comments: