I cannot give you enough data to reproduce the errors, so I am going to give you as much data as I can get away with.
I have a select statement executing from EF core.
var bookings = context.Booking
.Where(booking => booking.ConsigneeNumber == customer.GetCustomerTarget().Code
&& booking.CreatedAt >= from
&& booking.CreatedAt < to
&& booking.BookingLine.Any(b => b.BookingLineSpecification
.Any(c => c.CurrencyCode == code))
)
.Include(booking => booking.BookingLine)
.ThenInclude(bl => bl.BookingLineSpecification)
.ThenInclude(bls => bls.UnitType)
.Include(booking => booking.BookingLine)
.ThenInclude(bl => bl.BookingLineAddress)
.ThenInclude(bla => bla.Country)
.Include(booking => booking.BookingLine)
.ThenInclude(bl => bl.BookingLineAddress)
.ThenInclude(bla => bla.PostalCode)
.Include(booking => booking.BookingLine)
.ThenInclude(bl => bl.BookingLineSpecification)
.ThenInclude(bls => bls.RelBookingLineSpecificationSalesInvoiceDetail)
.ThenInclude(Rel => Rel.SalesInvoiceDetail);
The SQL query itself being suspended on the MSSQL server becomes:
(@__GetCustomerTarget_Code_0 bigint,@__from_1 datetime2(7),@__to_2 datetime2(7),@__code_3 varchar(255))
SELECT [booking].[Id],
[booking].[booking_provider_id],
[booking].[booking_status_id],
[booking].[consignee_name],
[booking].[consignee_number],
[booking].[created_at],
[booking].[created_by],
[booking].[currency_code],
[booking].[deliveryNumber],
[booking].[description],
[booking].[destroyed_at],
[booking].[destroyed_by],
[booking].[inter_company_number],
[booking].[invoicee_name],
[booking].[invoicee_number],
[booking].[is_create],
[booking].[location_id],
[booking].[location_name],
[booking].[maturity_level_id],
[booking].[number],
[booking].[order_number],
[booking].[provider_key],
[booking].[shipment_id],
[booking].[system_responsible_id],
[booking].[updated_at],
[booking].[updated_by]
FROM [Integration].[booking] AS [booking]
WHERE ((([booking].[consignee_number] = @__GetCustomerTarget_Code_0)
AND ([booking].[created_at] >= @__from_1))
AND ([booking].[created_at] < @__to_2))
AND EXISTS (
SELECT 1
FROM [Integration].[booking_line] AS [b]
WHERE EXISTS (
SELECT 1
FROM [Integration].[booking_line_specification] AS [c]
WHERE ([c].[currency_code] = @__code_3) AND ([b].[Id] = [c].[booking_line_id])) AND ([booking].[Id] = [b].[booking_id]))
This statement executes in zero seconds (but some miliseconds) when executed in MSSQL management studio. However the C# application experiences a timeout.
When I use the internal tools on MSSQL I can see that the spid is suspended, and permanently waiting. However the reason seems to be altering. In the beginning its due to IO_COMPLETION. Then its SOS_YIELD_~something and finally PAGEIOLATCH_SH This final state it stays in
I, for the life of me, cannot figure out why MSSQL can execute the query with no issue at all. But EF seemingly fails to utilize Indexes. Or something else I am missing entirely.
I am simply out of ideas. Can anyone point me in a direction that might help?
I have tried:
Running it in Visual stuido 2017. Running in release mode. I have tried Enabling lazy mode, and not use includes. I have tried removing lazy loads and includes, just to see if I could get the bookings back.
Nope. MSSQL seems to refuse EF Core from utilizing the Indexes.
The thing is, the query only hangs when I provide certain parameters. Other parameters work just fine. Specifically, if I provide different currency codes, this seems to make all the difference for MSSQL if the query gets suspended or not.
I have completely rebuilt the indexes required to execute this query efficiently according to the execution plan in MSSQL management studio.
Any further information that might be required please let me know, and I will see what I can do, to the best of my efforts.
UPDATE Actual execution plan:
UPDATE 2: I would like to point out, that this is currently being used for development, and thus this DB, my software and anything in between, is under my "control".
In so far as my apparently inexperienced mind can control anything :)
So any suggestions on how to better debug the problem, or requests for more data will be met with vigor and appreciation. And will likely be possible, especially if hinted at how to provide it to you! (And me)
SQL Profiler: opening connection to the DB:
set quoted_identifier on
set arithabort on
set numeric_roundabort off
set ansi_warnings on
set ansi_padding on
set ansi_nulls on
set concat_null_yields_null on
set cursor_close_on_commit off
set implicit_transactions off
set language us_english
set dateformat mdy
set datefirst 7
set transaction isolation level read committed