使用Include()优化EF Core查询 [英] Optimize EF Core query with Include()

查看:229
本文介绍了使用Include()优化EF Core查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有以下查询,它消耗大量时间来执行。我正在尝试对其进行优化,但无法成功实现。

I have following query within my project and it is consuming lot of time to execute. I am trying to optimize it, but not able to successfully do it. Any suggestions would be highly appreciated.

_context.MainTable
.Include(mt => mt.ChildTable1)
.Include(mt => mt.ChildTable1.ChildTable2)
.Include(mt => mt.ChildTable3)
.Include(mt => mt.ChildTable3.ChildTable4)
.SingleOrDefault(
        mt =>
        mt.ChildTable3.ChildTable4.Id == id 
        &&
        mt.ChildTable1.Operation == operation
        && 
        mt.ChildTable1.Method = method
        && 
        mt.StatusId == statusId);


推荐答案

Include()获取要转换的联接,而您在代码中使用了太多联接。您可以借助数据库引擎执行计划来优化索引。

Include() gets translates to join and you are using too many joins in the code. You can optimize indexes with the help of DB engine execution plan.

我建议您不要在其中使用所有 Include 一口气。相反,您断开查询并逐个应用 Include 。我的意思是您再次应用 Include,得到结果,然后再次应用 Include ,依此类推。通过具有两个以上的包含会影响性能。

I suggest you not to use all Include in one go. instead, you break the query and apply Include one by one. I meant you apply Include, get the result and then apply theIncludeagain and so..By having more than twoInclude` affect the performance.

这篇关于使用Include()优化EF Core查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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