Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server C#-Entityframework和SQL Server linq嵌套循环联接和hashjoin性能_Sql Server_Performance_Linq - Fatal编程技术网

Sql server C#-Entityframework和SQL Server linq嵌套循环联接和hashjoin性能

Sql server C#-Entityframework和SQL Server linq嵌套循环联接和hashjoin性能,sql-server,performance,linq,Sql Server,Performance,Linq,我想做一个左外连接来获取一些数据。然而,尽管在有用的列上创建了一些索引,但使用实体框架和C#LINQ连接时,我的性能非常差。我尝试了另一种方法,在WHERE子句中使用multiple和continuousfrom,并看到了性能的巨大提升 我想知道为什么SQL Server不选择更快的连接类型?我的LINQ查询联接是否做错了什么,导致性能如此糟糕 这里是需要3秒钟以上时间的第一个版本: from calendarEvent in calendarEvents join participants i

我想做一个左外连接来获取一些数据。然而,尽管在有用的列上创建了一些索引,但使用实体框架和C#LINQ连接时,我的性能非常差。我尝试了另一种方法,在WHERE子句中使用multiple和continuousfrom,并看到了性能的巨大提升

我想知道为什么SQL Server不选择更快的连接类型?我的LINQ查询联接是否做错了什么,导致性能如此糟糕

这里是需要3秒钟以上时间的第一个版本:

from calendarEvent in calendarEvents
join participants in ctx.CalendarEventParticipants on calendarEvent.Id equals participants.CalendarEventId into calendarEventWithParticipants
from subCalendarEventWithParticipant in calendarEventWithParticipants.DefaultIfEmpty()
select new { calendarEvent , subCalendarEventWithParticipant };
以及需要200毫秒的版本:

from calendarEvent in calendarEvents
from participants in ctx.CalendarEventParticipants.Where(cep => cep.CalendarEventId == calendarEvent.Id).DefaultIfEmpty()
select new { calendarEvent , participants };
我有一个关于
CalendarEventParticipants.calendarEventId
的有用索引

我主要关心的是,带有连接的查询在理论上应该比带有where的第二个查询快得多。使用SSMS,我可以看到在执行嵌套循环(左外连接)之前查询没有连接,这在这个查询上花费了我很多


谢谢

首先,您的问题不清楚。比较CalendarEventId与calendarEvent.Id的方式

from calendarEvent in calendarEvents

from participants in ctx.CalendarEventParticipants.Where(cep => cep.CalendarEventId == calendarEvent.Id).DefaultIfEmpty()
select new { calendarEvents, participants };
其次,上面的查询看起来像一个嵌套查询,它始终是一个性能食客。另一个花费较少时间的查询是使用join,它为您提供了要处理的联合目标集。性能取决于索引、大小和其他因素。但如果表很大,则嵌套查询不是正确的选择。而是使用连接

from calendarEvent in calendarEvents
join participants in ctx.CalendarEventParticipants on calendarEvent.Id equals participants.CalendarEventId into calendarEventWithParticipants

from subCalendarEventWithParticipant in calendarEventWithParticipants.DefaultIfEmpty()
select new { calendarEvents, subCalendarEventWithParticipant };

请试着想象一下读者回答一个问题需要什么。您不知道生成了哪个SQL,所以我们不知道您所说的“更快的连接类型”是什么意思。此外,了解您使用的ORM(+版本!)也很重要。通常,类定义和映射也是相关的,因为必需的和非必需的关联在联接中的行为不同。
hashjoin
是什么?@GertArnold:你好!这是C代码,因此此处不显示SQL。我确实可以转储发送到SQL SERVER的查询,但这真的很难理解。我们使用实体框架。@NetMage:您好!请参阅,您似乎没有意识到C代码只是生成SQL的工具。无论在C#中写了什么关于LINQ的内容,也就是说,LINQ到对象,这里都不适用。如果要优化什么,那就是SQL代码。这就是为什么SQL和映射至少与LINQ查询一样相关的原因。我的主要问题是,与使用where子句的查询相比,使用join的查询运行了3秒钟。我更新了我的帖子以添加更多细节。