29

Using sys.dm_os_wait_stats I have identified what I believe is a locking problem

  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

Is there a way I can find the top blocking/locking queries? I've tried querying sys.dm_db_index_operational_stats without much luck.

Carlo V. Dango
  • 13,322
  • 16
  • 71
  • 114

3 Answers3

41

You may find this query useful:

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

To get the query itself use this one:

SELECT text,* 
  FROM sys.dm_exec_requests
  CROSS APPLY sys.dm_exec_sql_text(sql_handle)
  WHERE DB_NAME(database_id) = 'YourDBName' 
    AND blocking_session_id <> 0
schlebe
  • 3,387
  • 5
  • 37
  • 50
Nomad
  • 536
  • 5
  • 3
  • 2
    Yes I ended up manually and continually hitting F5 with the following query "SELECT * FROM sys.dm_exec_requests CROSS APPLY sys.dm_exec_sql_text(sql_handle) where blocking_session_id <> 0" – Carlo V. Dango Dec 06 '10 at 13:50
  • 11
    I edited the statement further, so that you always get the 'true' culprit: `SELECT * FROM sys.dm_exec_requests CROSS APPLY sys.dm_exec_sql_text(sql_handle) where session_id IN (SELECT blocking_session_id FROM sys.dm_exec_requests WHERE DB_NAME(database_id)='SDCS20' and blocking_session_id <>0)` Some statements are blocking because they're blocked by something else, in that case the blocking_session_id will be different from the session_id. – Steffen Winkler Mar 11 '14 at 08:00
  • A small variation; so you can view both the blocking and blocked queries together: SELECT DB_NAME(blocking.database_id) as database_name, blocked.text as blocked_query, blocking.text as blocking_query, blocked.command, blocking.command, blocked.*, blocking.* FROM (select * from sys.dm_exec_requests CROSS APPLY sys.dm_exec_sql_text(sql_handle) ) as blocked inner join (select * from sys.dm_exec_requests CROSS APPLY sys.dm_exec_sql_text(sql_handle) ) as blocking on blocking.session_id = blocked.blocking_session_id – Sam Aug 26 '22 at 08:43
14

I found this query which helped me find my locked table and query causing the issue.

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
ashman786
  • 342
  • 3
  • 9
5

Use the script: sp_blocker_pss08 or SQL Trace/Profiler and the Blocked Process Report event class.

Martin Smith
  • 438,706
  • 87
  • 741
  • 845