使用Include()优化EF Core查询

问题描述 投票:0回答:2

我在项目中有以下查询,执行时耗费了大量时间。我正在尝试优化它,但无法成功完成它。任何建议都将受到高度赞赏。

_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);
entity-framework optimization entity-framework-core
2个回答
1
投票

Include()转换为加入,并且您在代码中使用了太多的连接。您可以借助数据库引擎执行计划优化索引。

我建议你不要一次性使用所有的Include。相反,你打破查询并逐个应用Include。我的意思是你应用Include, get the result and then apply theIncludeagain and so..By having more than twoInclude`影响性能。


0
投票

我没有看到您查询的任何性能问题。

由于你有一个单独的默认,我会看一下uptimizing数据库调用。如果您有可用的分析工具,那么在SQL Server Management Studio中选择工具> Sql Server Profiler。获取对SQL Server Management Studio的查询,标记查询并选择“在数据库引擎优化顾问中分析查询”

© www.soinside.com 2019 - 2024. All rights reserved.