使用Include()优化EF Core查询

编程入门 行业动态 更新时间:2024-10-09 12:27:03
本文介绍了使用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查询

本文发布于:2023-11-15 06:24:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1591933.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Include   EF   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!