I have a linq query that retrieves rows from a View based on an id column (where id=@id)
This query takes 4 seconds to run. I have used SQL Server Profiler to inspect the query that is executed by linq, and if i copy that query directly to management studio and execute, the query only takes 56ms.
This exponential time increase is consistent across all linq queries to views in my application. What could be causing this extended execution time in my (WPF) application when the same queries execute < 100ms ?
== EDIT ==
I've managed to isolate further, comments show profiler duration;
/* 3953ms, 111487 reads */
context.SkuView.Where(p => p.TermId == 35 && !p.IsDeleted).ToList();
/* 90ms, 173 reads */
context.SkuView.Where(p => p.TermId == 35).ToList();
If i paste the (sql rendered) linq queries directly into ssms i get;
/* 250ms, 173 reads */
SELECT * FROM SkuView WHERE TermId == 35 AND IsDeleted = 0
/* 250ms, 173 reads */
SELECT * FROM SkuView WHERE TermId == 35
So the problem has something to do with the read count through linq when using !p.IsDeleted...
Possible culprits are:
In any case, the tools to investigate are all at your disposal.
sys.dm_exec_requests
wait_type, wait_time and wait_resource columns sys.dm_exec_query_stats
. things to look for are large diferences between the two cases in logical_reads and physical_reads, indicative of wildly different plans (scan vs. seek), or wild differences in elapsed_time but similar worker_time (indicative of blocking, locks likely).updating statistics on the db fixed this issue.
exec sp_updatestats
Much thanks to Remus for the learning ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With