Search code examples
nhibernatequery-cache

How does Query caching improves performance in nHibernate?


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


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.