left-icon

NHibernate Succinctly®
by Ricardo Peres

Previous
Chapter

of
A
A
A

CHAPTER 13

Performance Optimizations

Performance Optimizations


Optimizing Queries

Here are some general tips for optimizing queries with NHibernate:

  • Select only the properties you need; that is, avoid selecting the full entity when you don’t need it.
  • Avoid loading an entity when you only need its key (use ISession.Load<T> instead of ISession.Get<T>).
  • Use paging instead of retrieving all records at the same time; all querying APIs support it.
  • Fetch at query time all the references and collections that you will need.
  • Choose lazy and eager loading carefully, and choose an appropriate fetch mode. If you are certain that you need a child collection’s items each time you load a specific entity, mark this collection as not lazy and with fetch mode JOIN.
  • Resort to SQL queries when performance is crucial.

Optimizing Making Changes

Consider this:

  • Always use explicit transactions.
  • Use executable HQL for bulk updates and deletes.
  • Use batching and stateless sessions for insertions.
  • Evict unneeded entities from the session.
  • Mark entities that you never want to change as immutable so that the session does not waste time with them when tracking changes.
  • Use explicit flushing and, when flushing, also clear the session or evict unneeded entities.
  • In production, disable logging and statistics.
Scroll To Top
Disclaimer
DISCLAIMER: Web reader is currently in beta. Please report any issues through our support system. PDF and Kindle format files are also available for download.

Previous

Next



You are one step away from downloading ebooks from the Succinctly® series premier collection!
A confirmation has been sent to your email address. Please check and confirm your email subscription to complete the download.