We have a .NET application that on some occasions throws an exception, which the stack trace reveals to have happened when its trying to update a particular table.
Any suggestions to find the culprit would be appreciated.
I ran sp_who2 at about the same time, maybe a few seconds afterwards, but did not see any indication of blocking, which i still suspect might be the case - somewhere.
I have another query which hits sys.dm_exec_query_stats, but when I run it, its taking a very long time to return (over 1 min and then I stopped it). It wasnt causing any blocks or running into any that I could see.
Here is the query to attempt to locate long running queries
SELECT TOP 20
total_worker_time, total_elapsed_time,
total_worker_time/execution_count AS avg_cpu_cost, execution_count,
(SELECT DB_NAME(dbid) + ISNULL('..' + OBJECT_NAME(objectid), '')
FROM sys.dm_exec_sql_text([sql_handle])) AS query_database,
(SELECT SUBSTRING(est.[text], statement_start_offset/2 + 1,
(CASE WHEN statement_end_offset = -1
THEN LEN(CONVERT(nvarchar(max), est.[text])) * 2
ELSE statement_end_offset
END - statement_start_offset) / 2
)
FROM sys.dm_exec_sql_text([sql_handle]) AS est) AS query_text,
total_logical_reads/execution_count AS avg_logical_reads,
total_logical_writes/execution_count AS avg_logical_writes,
last_worker_time, min_worker_time, max_worker_time,
last_elapsed_time, min_elapsed_time, max_elapsed_time,
plan_generation_num, qp.query_plan
FROM sys.dm_exec_query_stats
OUTER APPLY sys.dm_exec_query_plan([plan_handle]) AS qp
WHERE [dbid] >= 5 AND DB_NAME(dbid) IS NOT NULL
AND (total_worker_time/execution_count) > 100
--ORDER BY avg_cpu_cost DESC;
--ORDER BY execution_count DESC;
ORDER BY total_worker_time DESC;