How does query caching works and how does it improves performance in nHibernate?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.