【问题标题】:How to find out what table a page lock belongs to如何找出页锁属于哪个表
【发布时间】:2011-10-19 11:56:33
【问题描述】:
当我们遇到性能问题时,我正在使用 sys.dm_tran_locks 视图检查我的数据库的哪些区域有锁。
使用此视图....
但是,如果 resource_type 是 Page 或 Key 是否有任何方法可以将其追溯到其父表,以便我可以很好地了解哪些表被锁定?
【问题讨论】:
标签:
sql-server
sql-server-2008
locking
【解决方案1】:
这就是 resource_associated_entity_id 列的用途 (Example query)。
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
【解决方案2】:
您必须找到与该资源关联的 object_id,这可能涉及加入另一个表。例如,
SELECT *, OBJECT_NAME(p.object_id)
FROM sys.dm_tran_locks l
JOIN sys.partitions p
ON l.resource_associated_entity_id = p.hobt_id
WHERE resource_type = 'KEY'
在联机丛书中查找 sys.dm_tran_locks 以确定每个资源的连接表应该是什么。