懒惰VS对实体框架急于加载性能 [英] Lazy vs eager loading performance on Entity Framework

查看:87
本文介绍了懒惰VS对实体框架急于加载性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有我的DbContext以下模型类:

So I have the following model classes on my DbContext:

每当我渲染LoanApplication对象的列表我做这样的事情:

Everytime I render a list of LoanApplication objects I do something like this:

var context = new MyContext();
var applications = context.LoanApplications.Where(d => d.PropertyThatIWantToFilter = localVariable);

这将返回然后我转换为一个视图模型像这样在我的控制器方法调用一个IQueryable:

This returns an IQueryable that then I convert to a ViewModel like this on my controller method call:

var vm = applications.Select(d => new LoanApplicationViewModel(d));

LoanApplicationViewModel 构造我接受实体对象,并做相应的映射。问题是,既然律师收集是一个导航属性​​,调用到数据库每一个新的视图模型实例化时提出的。每个应用程序的律师的平均数是2,那么这意味着,如果我使一个表格,列出了过去10应用程序然后应用程序正在约18-20〜人次到数据库。

Inside the LoanApplicationViewModel constructor I accept the entity object and do the corresponding mapping. The thing is that, since the Solicitors collection is a navigational property, a call is made to the database each time a new view model is instanced. The average number of solicitors per application is two, so that means that if I render a table listing the last 10 applications then the app is making about ~18-20 trips to the database.

我觉得必须有一个更好的方式来获得这个集合,所以我改变了我原来的查询热切加载集合,像这样:

I thought there had to be a better way to get this collection, so I changed my original query to eagerly load the collection like so:

var applications = context.LoanApplications.Include("Solicitors").Where...

虽然此降低调用数据库的数目只有一个,的查询是慢得多,约50%的更慢。

数据库托管的SQL Azure以及我们实现瞬时故障处理,但我想,以减少不降低响应时间的性能对数据库进行调用的数量。

The database is hosted on SQL Azure, and we've implemented Transient Fault Handling, but I want to reduce the quantity of calls made to the database without reducing response-time performance.

什么是最好的做法吗?

What is the best practice here?

推荐答案

什么是最好的做法在这里吗?

"What is the best practice here?"

最好的做法是将


  1. 设置!应用广泛!性能指标

  2. 轮廓,基准,并找到瓶颈

  3. 审查和微调的瓶颈,让你的工作,最少的最大性能取胜。 (从我的经验,90%的时间它不是TSQL)

现在,可能看起来有点风马牛不相及,但是从这一点来看,它装过,你要型材是你的应用程序域内最佳的模式是要走的正确途径。

Now that may seem a bit irrelevant, but from that point of view, which ever loading pattern you PROFILED to be optimal within your application domain is the correct way to go.

还有跃跃欲试/懒惰没有最佳实践。这就是为什么这两个选项都可用。此外,如果TSQL急于/懒惰之间的瓶颈和开关仍然不打你的绩效目标,你需要走下来的其他工具,如在SSMS查询分析器和查询分析器计划整体太多了。

There's no "best practice" of eager/lazy. That's why both options are both available. Also if the tsql is your bottle neck and switching between eager/lazy still isn't hitting your performance target, you will need to go down a whole plethora of other tools such as query analyzer and query plan analyser in SSMS.

有关的一些背景:

我在谷歌搜索预先加载缓慢,并来到了这里。这里是我的结果是:

I was googling "eager loading slow" and came here. Here's my result:

var foo = _context.Foos
    //.Include("Answers")
    //.Include("Attachments")
    .FirstOrDefault(q => q.Id == key);

预先加载:106ms

Eager loading: 106ms

延迟加载:11毫秒+ 5毫秒+ 5ms的

Lazy loading: 11ms + 5ms + 5ms

延迟加载胜,故事的结尾。

Lazy loading wins, end of story.

这篇关于懒惰VS对实体框架急于加载性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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