Check SQL Server Deadlock

Sometimes a script keeps running for a long time and can't stop, then a db blocking is occurring.

We can do below things to kill the db process which is blocking others.

  1. Run “sp_who2“ in db, we can get the sql process list like below pic.

Take below pic as example, we can see process 6 is blocked by process 3.

  1. Run below script to see the detail of the block.
SELECT TOP 10 @@SERVERNAME ,@@SERVERNAME + CHAR(10) + CHAR(13) 
+ 'Wait: ' + CAST(DATEDIFF(ss, Start_Time, GETDATE()) AS VARCHAR(20)) + ' SS' + CHAR(10) + CHAR(13) 
+ 'Host_name: ' + ISNULL(host_name, '') + CHAR(10) + CHAR(13) 
+ 'Login_name: ' + ISNULL(b.Login_name, '') + CHAR(10) + CHAR(13) 
+ 'program_name: ' + ISNULL(program_name, '') + CHAR(10) + CHAR(13)
+ 'Database: ' + ISNULL(db_name(a.database_id), '') + CHAR(10) + CHAR(13) 
+ ISNULL(c.text, '') + CHAR(10) + CHAR(13) 
+ 'wait_type: ' + wait_type + CHAR(10) + CHAR(13) 
+ 'Session_ID: ' + CAST(a.Session_ID AS VARCHAR(10)) , GETDATE()
FROM sys.dm_exec_requests a
INNER JOIN sys.dm_exec_sessions b ON a.session_id = b.session_id CROSS APPLY sys.dm_exec_sql_text(a.sql_handle) c
WHERE a.session_id > 50
  AND DATEDIFF(ss, Start_Time, GETDATE()) BETWEEN 10 AND 200
  AND wait_type NOT IN ('BROKER_RECEIVE_WAITFOR')
  AND blocking_session_id > 0
ORDER BY DATEDIFF(ss, Start_Time, GETDATE()) DESC
  1. Run script "Kill 3' in db (3 is the db process id), and we can stop the process which blocks others.

猜你喜欢

转载自www.cnblogs.com/swlin/p/10581999.html