--查看死锁
SELECT
t1.resource_type [资源锁定类型],
DB_NAME(resource_database_id) AS 数据库名,
t1.resource_associated_entity_id 锁定对象,
t1.request_mode AS 等待者请求的锁定模式,
t1.request_session_id 等待者SID,
t2.wait_duration_ms 等待时间,
(
SELECT
TEXT
FROM
sys.dm_exec_requests r CROSS apply sys.dm_exec_sql_text (r.sql_handle)
WHERE
r.session_id = t1.request_session_id
) AS 等待者要执行的SQL,
t2.blocking_session_id [锁定者SID],
(
SELECT
TEXT
FROM
sys.sysprocesses p CROSS apply sys.dm_exec_sql_text (p.sql_handle)
WHERE
p.spid = t2.blocking_session_id
) 锁定者执行语句
FROM
sys.dm_tran_locks t1,
sys.dm_os_waiting_tasks t2
WHERE
t1.lock_owner_address = t2.resource_address
order by t2.wait_duration_ms desc
--查看正在阻塞的SQL
SELECT TOP 20
[session_id],
[request_id],
[start_time] AS '开始时间',
[status] AS '状态',
[command] AS '命令',
dest.[text] AS 'sql语句',
DB_NAME([database_id]) AS '数据库名',
[blocking_session_id] AS '正在阻塞其他会话的会话ID',
[wait_type] AS '等待资源类型',
[wait_time] AS '等待时间',
[wait_resource] AS '等待的资源',
[reads] AS '物理读次数',
[writes] AS '写次数',
[logical_reads] AS '逻辑读次数',
[row_count] AS '返回结果行数'
FROM sys.[dm_exec_requests] AS der CROSS APPLY sys.[dm_exec_sql_text](der.[sql_handle]) AS dest
WHERE [session_id]>50 AND DB_NAME(der.[database_id])='SeedLandContractDB'
ORDER BY [cpu_time] DESC