在MS SQL中查找阻塞/锁定查询(mssql)

时间:2010-12-06 11:36:50

标签: sql-server sql-server-2005 sql-server-2008 query-optimization

使用sys.dm_os_wait_stats我已经确定了我认为是锁定问题

  wait type    waittime  pct     running ptc
  LCK_M_RS_S   2238.54   22.14   22.14
  LCK_M_S      1980.59   19.59   41.73

有没有办法可以找到顶级阻塞/锁定查询?我试过没有太多运气来查询sys.dm_db_index_operational_stats

3 个答案:

答案 0 :(得分:23)

您可能会发现此查询很有用:

SELECT * 
FROM sys.dm_exec_requests
WHERE DB_NAME(database_id) = 'YourDBName' 
AND blocking_session_id <> 0

答案 1 :(得分:4)

使用脚本:sp_blocker_pss08或SQL Trace / Profiler以及阻止进程报告事件类。

答案 2 :(得分:1)

我发现了这个查询,它帮助我找到了锁定的表并查询了导致问题的原因。

SELECT  L.request_session_id AS SPID, 
        DB_NAME(L.resource_database_id) AS DatabaseName,
        O.Name AS LockedObjectName, 
        P.object_id AS LockedObjectId, 
        L.resource_type AS LockedResource, 
        L.request_mode AS LockType,
        ST.text AS SqlStatementText,        
        ES.login_name AS LoginName,
        ES.host_name AS HostName,
        TST.is_user_transaction as IsUserTransaction,
        AT.name as TransactionName,
        CN.auth_scheme as AuthenticationMethod
FROM    sys.dm_tran_locks L
        JOIN sys.partitions P ON P.hobt_id = L.resource_associated_entity_id
        JOIN sys.objects O ON O.object_id = P.object_id
        JOIN sys.dm_exec_sessions ES ON ES.session_id = L.request_session_id
        JOIN sys.dm_tran_session_transactions TST ON ES.session_id = TST.session_id
        JOIN sys.dm_tran_active_transactions AT ON TST.transaction_id = AT.transaction_id
        JOIN sys.dm_exec_connections CN ON CN.session_id = ES.session_id
        CROSS APPLY sys.dm_exec_sql_text(CN.most_recent_sql_handle) AS ST
WHERE   resource_database_id = db_id()
ORDER BY L.request_session_id