我在项目中有以下查询,执行时耗费了大量时间。我正在尝试优化它,但无法成功完成它。任何建议都将受到高度赞赏。
_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
。相反,你打破查询并逐个应用Include
。我的意思是你应用Include, get the result and then apply the
Includeagain and so..By having more than two
Include`影响性能。
我没有看到您查询的任何性能问题。
由于你有一个单独的默认,我会看一下uptimizing数据库调用。如果您有可用的分析工具,那么在SQL Server Management Studio中选择工具> Sql Server Profiler。获取对SQL Server Management Studio的查询,标记查询并选择“在数据库引擎优化顾问中分析查询”