查找对 Azure SQL/SQL Server 中的资源具有 Sch-S 锁定超过 5 分钟的查询

问题描述 投票:0回答:1

以下查询给出了不同类型的锁。我观察到一些查询长时间保持资源上的 Sch-S 锁定。

我提到了https://dba.stackexchange.com/questions/328283/why-does-a-select-query-sch-s-get-blocked- due-to-an-existing-sch-s-lock-持有-b 以了解这些锁的一些想法。

SELECT dm_tran_locks.request_session_id,
       dm_tran_locks.resource_database_id,
       DB_NAME(dm_tran_locks.resource_database_id) AS dbname,
       CASE
           WHEN resource_type = 'OBJECT'
               THEN OBJECT_NAME(dm_tran_locks.resource_associated_entity_id)
           ELSE OBJECT_NAME(partitions.OBJECT_ID)
       END AS ObjectName,
       dm_tran_locks.resource_type,
       dm_tran_locks.resource_description,
       dm_tran_locks.resource_associated_entity_id,
       dm_tran_locks.request_mode,
       dm_tran_locks.request_status
FROM sys.dm_tran_locks
LEFT JOIN sys.partitions ON partitions.hobt_id = dm_tran_locks.resource_associated_entity_id
LEFT JOIN sys.indexes ON indexes.OBJECT_ID = partitions.OBJECT_ID AND indexes.index_id = partitions.index_id
WHERE resource_associated_entity_id > 0
  AND resource_database_id = DB_ID()
ORDER BY request_session_id, resource_associated_entity_id 

问题

  1. 如何识别导致锁定时间超过 5 分钟的关联查询?
  2. 记录此类查询的最佳方式是什么(导致此锁定超过 5 分钟)
sql-server azure-sql-database
1个回答
0
投票

您需要的查询可能如下所示:

select conn.session_id as blockerSession,conn2.session_id as BlockedSession,req.wait_time as Waiting_Time_ms,cast((req.wait_time/1000.) as decimal(18,2)) as Waiting_Time_secs,
cast((req.wait_time/1000./60.) as decimal(18,2)) as Waiting_Time_mins,t.text as BlockerQuery,t2.text as BlockedQuery, req.wait_type from sys.dm_exec_requests as req
inner join sys.dm_exec_connections as conn on req.blocking_session_id=conn.session_id
inner join sys.dm_exec_connections as conn2 on req.session_id=conn2.session_id
inner join sys.dm_tran_locks as tr on tr.request_session_id = conn.session_id
cross apply sys.dm_exec_sql_text(conn.most_recent_sql_handle) as t
cross apply sys.dm_exec_sql_text(conn2.most_recent_sql_handle) as t2
where tr.request_mode = 'Sch-S' and cast((req.wait_time/1000./60.) > 5

您可能希望使用 Azure 自动化安排其每 X 分钟执行一次,并将结果记录到表中。

© www.soinside.com 2019 - 2024. All rights reserved.