How to find out what table a page lock belongs to

爱⌒轻易说出口 提交于 2019-12-29 04:36:09

问题


I'm using the sys.dm_tran_locks view to check what areas of my database have locks when we are having performance problems.

Using this view....

  • If the resource_type is database I can use the DB_NAME function to find out what database has the lock.

  • If its an object I can normally join to sys.tables to check what table it is.

However if the resource_type is Page or Key is there any way to trace this back to its parent table so I can get a good idea of which tables are locking?


回答1:


This is what the resource_associated_entity_id column is for (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:


You've got to find the object_id associated with that resource, and it may involve joining to another table. For example,

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'

Look up sys.dm_tran_locks in Books Online to figure out what the joining tables should be for each resource.



来源:https://stackoverflow.com/questions/7820907/how-to-find-out-what-table-a-page-lock-belongs-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!