查询缓存如何提高 nHibernate 的性能?
查询缓存如何工作以及如何提高 nHibernate 中的性能?
How does query caching works and how does it improves performance in nHibernate?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当缓存查询时,NHibernate 将缓存查询结果的实体 ID。
非常重要的是,它不缓存实体本身 - 只缓存它们的 ID。这意味着您几乎肯定希望确保这些实体也设置为可在二级缓存中缓存。否则,NHiberate 将从查询缓存中获取实体的 ID,但随后被迫前往数据库获取实际实体。这可能比一开始就访问数据库的成本更高!
同样重要的是:查询是根据其确切的 SQL 和参数值进行缓存的。其中任何一个的差异都意味着数据库将受到影响。因此,您可能只想缓存那些输入变化不大的查询。
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.
当您启用缓存时,当您执行查询时,nHibernate 会将查询结果存储在内部某处。当您尝试再次使用相同的参数执行查询时,它将从缓存中获取结果,而不是从数据库中获取结果,当然它要快得多!但请注意其他应用程序可以在后台修改数据库!但nHibernate可以更新缓存。
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.
通过使用它,nHibernate 不需要访问数据存储,它可以访问缓存中的内容。
By using it nHibernate doesn't need to access the Data store it access whats in the cache.