CHAPTER 13
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.
Build apps 2X faster
using Syncfusion Essential Studio® suite
- 1800+ high-performance UI components.
- Includes popular controls such as Grid, Chart, Scheduler, and more.
- 24x5 unlimited support by developers.
Thank you for your interest in the Succinctly® Series Premier Collection!
I agree to receive emails for updates about products, free resources, and promotional offers in exchange for downloading premier Succinctly® ebooks.
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.