当我们遇到性能问题时,我正在使用sys.dm_tran_locks
视图来检查数据库的哪些区域有锁定。
使用此视图....
如果resource_type是数据库,我可以使用DB_NAME函数找出具有锁定的数据库。
如果是对象,我通常可以加入sys.tables来检查它是什么表。
但是,如果resource_type是页面或键,有没有办法将其追溯到其父表,以便我可以很好地了解哪些表是锁定的?
答案 0 :(得分:38)
这是(Example query)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
答案 1 :(得分:1)
您必须找到与该资源关联的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,以确定每个资源的连接表应该是什么。