如何检查桌上持有哪些锁


Answers:


118

除其他响应外,sp_lock还可以用于在所有正在运行的进程上转储完整的锁定信息。输出结果可能会让人不知所措,但是如果您想确切地知道被锁定的内容,那么运行它就很有价值。我通常将其与sp_who2锁定问题快速归零。

sp_lock联机有多种不同版本的“更友好” 过程,具体取决于所讨论的SQL Server版本。

对于您的情况,对于SQL Server 2005 sp_lock仍然可用,但已弃用,因此现在建议将sys.dm_tran_locks视图用于此类操作。您可以在此处找到有关如何“滚动自己的” sp_lock函数的示例。


116

这并不能完全向您显示哪些行被锁定,但这可能对您有所帮助。

您可以通过运行以下命令检查哪些语句被阻止:

select cmd,* from sys.sysprocesses
where blocked > 0

它还会告诉您每个块正在等待什么。因此,您可以一直跟踪,以查看哪个语句导致了第一个块,而另一个块又引起了另一个块。

编辑以添加@MikeBlandford的评论:

被阻止的列指示阻止过程的spid。您可以运行kill {spid}进行修复。


7
被阻止的列指示阻止过程的spid。您可以运行kill {spid}进行修复。
Mike Blandford'3

52

您可以通过以下查询在表上找到当前

USE yourdatabase;
GO

SELECT * FROM sys.dm_tran_locks
  WHERE resource_database_id = DB_ID()
  AND resource_associated_entity_id = OBJECT_ID(N'dbo.yourtablename');

请参阅sys.dm_tran_locks

如果存在同一request_owner_type的多个实例,则request_owner_id列用于区分每个实例。对于分布式事务,request_owner_typerequest_owner_guid列将显示不同的实体信息。

例如,会话S1在Table1上拥有一个共享锁。在会话S1下运行的事务T1也对表1拥有共享锁。在这种情况下,sys.dm_tran_locks返回的resource_description列将显示同一资源的两个实例。该request_owner_type栏会显示一个实例作为会话和其他的交易。同样,resource_owner_id列将具有不同的值。


36

我使用动态管理视图(DMV)来捕获锁定以及被锁定项目的object_id或partition_id。

(必须切换到要观察的数据库以获得object_id)

SELECT 
     TL.resource_type,
     TL.resource_database_id,
     TL.resource_associated_entity_id,
     TL.request_mode,
     TL.request_session_id,
     WT.blocking_session_id,
     O.name AS [object name],
     O.type_desc AS [object descr],
     P.partition_id AS [partition id],
     P.rows AS [partition/page rows],
     AU.type_desc AS [index descr],
     AU.container_id AS [index/page container_id]
FROM sys.dm_tran_locks AS TL
INNER JOIN sys.dm_os_waiting_tasks AS WT 
 ON TL.lock_owner_address = WT.resource_address
LEFT OUTER JOIN sys.objects AS O 
 ON O.object_id = TL.resource_associated_entity_id
LEFT OUTER JOIN sys.partitions AS P 
 ON P.hobt_id = TL.resource_associated_entity_id
LEFT OUTER JOIN sys.allocation_units AS AU 
 ON AU.allocation_unit_id = TL.resource_associated_entity_id;

我正在尝试使用此语句来找出进程正在等待的对象。我可以清楚地看到一个会话正在等待使用sp_who2和in的另一个会话sys.dm_os_waiting_task(都试图更新同一张表)。但是您的语句不返回任何行。有任何想法吗?
a_horse_with_no_name 2014年

17

您还可以使用内置的sp_who2存储过程来获取SQL Server实例上的当前被阻止和被阻止的进程。通常,您可以将其与SQL事件探查器实例一起运行,以查找阻塞进程,并查看spid在事件探查器中发出的最新命令。


5

您可以通过以下脚本找到详细信息。

-- List all Locks of the Current Database 
SELECT TL.resource_type AS ResType 
      ,TL.resource_description AS ResDescr 
      ,TL.request_mode AS ReqMode 
      ,TL.request_type AS ReqType 
      ,TL.request_status AS ReqStatus 
      ,TL.request_owner_type AS ReqOwnerType 
      ,TAT.[name] AS TransName 
      ,TAT.transaction_begin_time AS TransBegin 
      ,DATEDIFF(ss, TAT.transaction_begin_time, GETDATE()) AS TransDura 
      ,ES.session_id AS S_Id 
      ,ES.login_name AS LoginName 
      ,COALESCE(OBJ.name, PAROBJ.name) AS ObjectName 
      ,PARIDX.name AS IndexName 
      ,ES.host_name AS HostName 
      ,ES.program_name AS ProgramName 
FROM sys.dm_tran_locks AS TL 
     INNER JOIN sys.dm_exec_sessions AS ES 
         ON TL.request_session_id = ES.session_id 
     LEFT JOIN sys.dm_tran_active_transactions AS TAT 
         ON TL.request_owner_id = TAT.transaction_id 
            AND TL.request_owner_type = 'TRANSACTION' 
     LEFT JOIN sys.objects AS OBJ 
         ON TL.resource_associated_entity_id = OBJ.object_id 
            AND TL.resource_type = 'OBJECT' 
     LEFT JOIN sys.partitions AS PAR 
         ON TL.resource_associated_entity_id = PAR.hobt_id 
            AND TL.resource_type IN ('PAGE', 'KEY', 'RID', 'HOBT') 
     LEFT JOIN sys.objects AS PAROBJ 
         ON PAR.object_id = PAROBJ.object_id 
     LEFT JOIN sys.indexes AS PARIDX 
         ON PAR.object_id = PARIDX.object_id 
            AND PAR.index_id = PARIDX.index_id 
WHERE TL.resource_database_id  = DB_ID() 
      AND ES.session_id <> @@Spid -- Exclude "my" session 
      -- optional filter  
      AND TL.request_mode <> 'S' -- Exclude simple shared locks 
ORDER BY TL.resource_type 
        ,TL.request_mode 
        ,TL.request_type 
        ,TL.request_status 
        ,ObjectName 
        ,ES.login_name;



--TSQL commands
SELECT 
       db_name(rsc_dbid) AS 'DATABASE_NAME',
       case rsc_type when 1 then 'null'
                             when 2 then 'DATABASE' 
                             WHEN 3 THEN 'FILE'
                             WHEN 4 THEN 'INDEX'
                             WHEN 5 THEN 'TABLE'
                             WHEN 6 THEN 'PAGE'
                             WHEN 7 THEN 'KEY'
                             WHEN 8 THEN 'EXTEND'
                             WHEN 9 THEN 'RID ( ROW ID)'
                             WHEN 10 THEN 'APPLICATION' end  AS 'REQUEST_TYPE',

       CASE req_ownertype WHEN 1 THEN 'TRANSACTION'
                                     WHEN 2 THEN 'CURSOR'
                                     WHEN 3 THEN 'SESSION'
                                     WHEN 4 THEN 'ExSESSION' END AS 'REQUEST_OWNERTYPE',

       OBJECT_NAME(rsc_objid ,rsc_dbid) AS 'OBJECT_NAME', 
       PROCESS.HOSTNAME , 
       PROCESS.program_name , 
       PROCESS.nt_domain , 
       PROCESS.nt_username , 
       PROCESS.program_name ,
       SQLTEXT.text 
FROM sys.syslockinfo LOCK JOIN 
     sys.sysprocesses PROCESS
  ON LOCK.req_spid = PROCESS.spid
CROSS APPLY sys.dm_exec_sql_text(PROCESS.SQL_HANDLE) SQLTEXT
where 1=1
and db_name(rsc_dbid) = db_name()



--Lock on a specific object
SELECT * 
FROM sys.dm_tran_locks
WHERE resource_database_id = DB_ID()
AND resource_associated_entity_id = object_id('Specific Table');
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.