-- set for 3 minutes below, do not run on production
-- ideal for testing CPU usage alerts
Declare @T DateTime,
@F Bigint;
Set @T=GetDate();
While DATEADD(Second,180,@T)>GETDATE()
Set @F=POWER(2,30);
| # joined DMV view useful for looking at current blocks # step 1 create view CREATE VIEW WhatIsGoingOn |
AS |
| SELECT |
| OBJECT_NAME(objectid) as ObjectName |
| , SUBSTRING (stateText.text, (statement_start_offset/2)+1, |
| (( CASE statement_end_offset |
| WHEN -1 THEN DATALENGTH(stateText.text) |
| ELSE statement_end_offset |
| END - statement_start_offset)/2) + 1) AS statement_text |
| ,DB_Name(database_id) as DatabaseName |
| ,req.cpu_time AS CPU_Time |
| ,DATEDIFF( minute , last_request_start_time, getdate()) AS RunningMinutes |
| ,req.Percent_Complete |
| ,sess.HOST_NAME as RunningFrom |
| , LEFT (CLIENT_INTERFACE_NAME, 25) AS RunningBy |
| ,sess.session_id AS SessionID |
| ,req.blocking_session_id AS BlockingWith |
| ,req.reads |
| ,req.writes |
| ,sess.[program_name] |
| ,sess.login_name |
| ,sess.status |
| ,sess.last_request_start_time |
| ,req.logical_reads |
FROM |
| sys.dm_exec_requests req |
| INNER JOIN sys.dm_exec_sessions sess ON sess.session_id = req.session_id |
| AND sess.is_user_process = 1 |
| CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS stateText |
# step 2 create logging table
# step3 create procedure to insert rows into the logging table
e.g. RunningMinutes >
10 minutes). In this way its possible for a DBA to be proactive in resolving blocking issues. Also useful for performance tuning with some minor tweaks.