如何找出页锁所属的表

Gav*_*vin 17 sql-server locking sql-server-2008

sys.dm_tran_locks当我们遇到性能问题时,我正在使用视图来检查数据库的哪些区域有锁定.

使用这个视图....

  • 如果resource_type是数据库,我可以使用DB_NAME函数找出具有锁的数据库.

  • 如果它是一个对象,我通常可以加入sys.tables来检查它是什么表.

但是,如果resource_type是PageKey有什么方法可以将其追溯到其父表,那么我可以很好地了解哪些表是锁定的?

Mar*_*ith 41

这是该resource_associated_entity_id列的用途(示例查询).

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,
       partitions.index_id,
       indexes.name AS index_name,
       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 
Run Code Online (Sandbox Code Playgroud)