HiveBrain v1.2.0
Get Started
← Back to all entries
patternModerate

Notification on long-running query or deadlock in SQL Server 2008 R2?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
2008deadlocksqlquerylongrunningservernotification

Problem

I'd like to know if there is a way to send a notification on deadlock? If so what queries would be required. I understand that SQL Server takes care of deadlocks, I simply would like information on the queries involved.

I found the following to determine long-running queries:

SELECT 
    creation_time
    ,last_execution_time
    ,total_physical_reads
    ,total_logical_reads
    ,total_logical_writes
    , execution_count
    , total_worker_time
    , total_elapsed_time
    , total_elapsed_time / execution_count avg_elapsed_time
    ,SUBSTRING(st.text, (qs.statement_start_offset/2) + 1,
    ((CASE statement_end_offset
        WHEN -1 THEN DATALENGTH(st.text)
        ELSE qs.statement_end_offset END
    - qs.statement_start_offset)/2) + 1) AS statement_text
FROM sys.dm_exec_query_stats AS qs
    CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st  
where total_elapsed_time >= 300000000 --5 min
ORDER BY total_elapsed_time / execution_count DESC;


I'd like to know if the above is the right way to go, or is there a better way to determine if any query takes longer than a specific interval say 5 min as shown?

Thanks

Solution

You can configure alerts for both of these with SQL Agent. Create a new alert and select type "SQL Server performance condition alert"

For long running queries, choose Object "MSSQL$InstanceName:Transactions" and Counter: Longest Transaction Running Time. Configure the values, and the alert notification options, and you're good to go.

For deadlocks, the Object is "MSSQL$InstanceName:Locks" and the Counter is "Number of Deadlocks/sec"

If you want more fine grained control of the deadlock notification, check this out:
http://www.sqlservercentral.com/articles/Administration/3243/

Context

StackExchange Database Administrators Q#5107, answer score: 10

Revisions (0)

No revisions yet.