I would like to do an left outer join to get some data. However, despite some index created on useful column, I get really poor performance using Entity Framework and C# LINQ join. I tried an another way using multiple and consecutive FROM with a WHERE clause, and saw a huge boost in performance.
I wanted to know why SQL Server does not pick the faster type of join? Did I do something wrong with my LINQ query join to get so bad performance?
Here the first version that take more than 3s:
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 };
and the version that takes 200ms:
from calendarEvent in calendarEvents
from participants in ctx.CalendarEventParticipants.Where(cep => cep.CalendarEventId == calendarEvent.Id).DefaultIfEmpty()
select new { calendarEvent , participants };
I have an useful index on CalendarEventParticipants.calendarEventId
.
My main concern is that the query with the join should be in theory much faster than the second one with a where. Using SSMS, I can see that the query do not JOIN before doing a NESTED LOOPS (Left Outer join) that cost me so much on this query.
Thanks