什么时候应该避免使用NHibernate的延迟加载功能? [英] When should one avoid using NHibernate's lazy-loading feature?

查看:75
本文介绍了什么时候应该避免使用NHibernate的延迟加载功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所听到的关于NHibernate的延迟加载的大多数信息是,使用它比不使用它更好.为了减少瓶颈,最小化数据库访问似乎很有意义.但是很少有事情需要权衡取舍,当然,它会迫使您具有virtual属性,从而稍微限制了设计.但是我也注意到一些开发人员拒绝对某些经常使用的对象进行延迟加载.

Most of what I hear about NHibernate's lazy-loading, is that it's better to use it, than not to use it. It seems like it just makes sense to minimize database access, in an effort to reduce bottlenecks. But few things come without trade-offs, certainly it slightly limits design by forcing you to have virtual properties. But I've also noticed that some developers turn lazy-loading off on certain often-used objects.

这使我想知道是否在某些特定情况下使用延迟加载会损害数据访问性能.

This makes me wonder if there are some definite situations where data-access performance is hurt by using lazy-loading.

所以我想知道,什么时候以及在什么情况下应该避免延迟加载我的NHibernate持久对象之一?

So I wonder, when and in what situations should I avoid lazy-loading one of my NHibernate-persisted objects?

延迟加载只是在额外的处理时间方面的缺点,还是休眠延迟加载还会增加数据访问时间(例如,通过对数据库进行额外的往返)吗?

Is the downside to lazy-loading merely in additional processing time, or can nhibernate lazy-loading also increase the data-access time (for instance, by making additional round-trips to the database)?

谢谢!

推荐答案

从数据库中快速加载和延迟加载对象之间存在明显的性能折衷.

There are clear performance tradeoffs between eager and lazy loading objects from a database.

如果使用急切加载,则会在单个查询中吸取大量数据,然后可以对其进行缓存.这在应用程序启动时最常见.您正在用内存消耗来进行数据库往返.

If you use eager loading, you suck a ton of data in a single query, which you can then cache. This is most common on application startup. You are trading memory consumption for database round trips.

如果使用延迟加载,则在单个查询中吸取的数据量最少,但是每当需要与该初始数据相关的更多信息时,它都需要对数据库进行更多查询,而数据库性能命中率通常是主要性能大多数应用程序中的瓶颈.

If you use lazy loading, you suck a minimal amount of data in a single query, but any time you need more information related to that initial data it requires more queries to the database and database performance hits are very often the major performance bottleneck in most applications.

因此,通常,您总是希望准确检索整个"单位所需要的数据.工作",不多也不少.在某些情况下,您可能不知道确切需要什么(因为用户正在通过向导或类似工具进行操作),在这种情况下,随便进行延迟加载可能很有意义.

So, in general, you always want to retrieve exactly the data you will need for the entire "unit of work", no more, no less. In some cases, you may not know exactly what you need (because the user is working through a wizard or something similar) and in that case it probably makes sense to lazy load as you go.

如果您正在使用ORM并专注于快速添加功能,并且稍后会回来并优化性能(这是非常普遍的做法,这是一种很好的处理方式),则默认为延迟加载是正确的选择.如果以后(通过性能分析/分析)发现您有一个查询来获取一个对象,然后有N个查询来获取与该原始对象相关的N个对象,则可以更改该代码段以使用急切加载仅击中数据库一次而不是N + 1次(N + 1问题是使用延迟加载的众所周知的缺点).

If you are using an ORM and focused on adding features quickly and will come back and optimize performance later (which is extremely common and a good way to do things), having lazy loading being the default is the correct way to go. If you later find (through performance profiling/analysis) that you have one query to get an object and then N queries to get the N objects related to that original object, you can change that piece of code to use eager loading to only hit the database once instead of N+1 times (the N+1 problem is a well known downside of using lazy loading).

这篇关于什么时候应该避免使用NHibernate的延迟加载功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆