Question

I am doing a sql server profiler on deadlocks because users are getting query time outs. In the profiler the eventClass column shows Lock:Escalation and Lock:Cancel. How do I find out what would cause a query to be canceled? Basically the same queries are being run by bunch of users and things zoom right through, but off and on throughout the day users are timing out. I am run sqldiag also; however, unfortunately I am not DBA and muddling my way through to discover the problem. Any suggestions?

thanks community nick

Was it helpful?

Solution

Query timeouts and deadlocks are pretty much mutually exclusive.

A deadlock situation will be discovered very quickly by deadlock monitor background thread and dealt with in a manner that one of the deadlocked processes (usually the one with lower cost of rolling back) will be chosen as a deadlock victim and it's work up to the point rolled back.

A query timeout could happen with livelocks, with high number of cuncurrent processes trying to access the same resource and thus blocking one another. When the time elapsed exceeds the timeout value (set by the client) the query will be canceled (and this is the reason you're seeing the Lock:Canceled events in the trace).

It is very important that client handles this condition, because all the resorces taken inside a transaction which timed out will remain taken as long as the connection is alive or the transaction is not rolled back.

To diagnose blocking situations, you can do several things.

If you happen to be monitoring at the time when a process is blocked, run the following query to find out the head of the blocking chain so you can investigate further:

select r.session_id, r.host_name, r.program_name, 
    r.login_name, r.nt_domain, r.nt_user_name, 
    r.total_elapsed_time/1000 as total_elapsed_time_sec, getdate() as vrijeme,
    (select text from sys.dm_exec_sql_text(c.most_recent_sql_handle)) as sql_text
from sys.dm_exec_connections c 
inner join sys.dm_exec_sessions r on r.session_id = c.session_id
where r.is_user_process = 1
and exists (
    select *
    from sys.dm_os_waiting_tasks r2
    where r2.blocking_session_id = r.session_id
)
and not exists (
    select *
    from sys.dm_os_waiting_tasks r3
    where r3.session_id = r.session_id
)
and r.total_elapsed_time/1000 > 10

This query has a 10 seconds treshold.

Firthermore you can use the Profiler to capture the blocking process event and then analyze it later on. Check this link for detailed explanation:

https://www.simple-talk.com/sql/sql-tools/how-to-identify-blocking-problems-with-sql-profiler/

There will usually be a handful of queries responsible for large majority of blocking. Identify those and try to optimize them (rewrite, indexing..). Besides, you can set up read committed snapshot isolation level for the database to avoid readers waiting on writers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top