Question

How does query caching works and how does it improves performance in nHibernate?

Was it helpful?

Solution

When a query is cached, NHibernate will cache the IDs of the entities resulting from the query.

Very importantly, it does not cache the entities themselves - only their IDs. This means that you almost certainly want to ensure that those entities are also set to be cachable in your second level cache. Otherwise, NHiberate will get the IDs of the entities from the query cache, but then be forced to go to the database to get the actual entities. That could be more costly than just going to the database in the first place!

Also important: queries are cached based on their exact SQL and parameter values. Any differences in either of those will mean that the database will be hit. So you probably only want to cache those queries that have little variance in their inputs.

OTHER TIPS

When you enable the caching the nHibernate will store query results somewhere inside when you execute query. When you try to execute query with SAME parameters again it will get results from cache, not from database, and of course it is much faster! but beware that other apps can modify database in background! But nHibernate can update caches.

By using it nHibernate doesn't need to access the Data store it access whats in the cache.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top