如何检查SQL表是否被锁定以进行查询?

时间:2014-01-13 14:25:29

标签: sql-server sql-server-2008 tsql

无论出于何种原因,数据库中的某个特定表开始不在特定表上完成任何查询结果。它说'执行查询......'而不是完成。

像:

select * from foo

其他表正在返回行。

我如何/在哪里检查桌子是否被锁定?

2 个答案:

答案 0 :(得分:3)

有几种方法可以检查阻止查询的内容。最简单的方法是启动内置Activity Monitor

同样简单的方法是查看sys.dm_exec_requests并查看被阻止的查询的wait_typewait_timewait_resourceblocking_session_id列(使用session_id来识别它。)

对于更复杂但更具信息性的方式,请观看How to Use sp_WhoIsActive to Find Slow SQL Server Queries

答案 1 :(得分:2)

检查此样本

    select  
    object_name(P.object_id) as TableName
    , resource_type
    , resource_description
    , request_mode
    , CASE REQUEST_MODE
                    WHEN 'S'        THEN 'Shared'
                    WHEN 'U'        THEN 'Update'
                    WHEN 'X'        THEN 'Exclusive'
                    WHEN 'IS'       THEN 'Intent Shared'
                    WHEN 'IU'       THEN 'Intent Update'
                    WHEN 'IX'       THEN 'Intent Exclusive'
                    WHEN 'SIU'      THEN 'Shared Intent Update'
                    WHEN 'SIX'      THEN 'Shared Intent Exclusive'
                    WHEN 'UIX'      THEN 'Update Intent Exclusive'
                    WHEN 'BU'       THEN 'Bulk Update'
                    WHEN 'RangeS_S' THEN 'Shared Range S'
                    WHEN 'RangeS_U' THEN 'Shared Range U'
                    WHEN 'RangeI_N' THEN 'Insert Range'
                    WHEN 'RangeI_S' THEN 'Insert Range S'
                    WHEN 'RangeI_U' THEN 'Insert Range U'
                    WHEN 'RangeI_X' THEN 'Insert Range X'
                    WHEN 'RangeX_S' THEN 'Exclusive range S'
                    WHEN 'RangeX_U' THEN 'Exclusive range U'
                    WHEN 'RangeX_X' THEN 'Exclusive range X'
                    WHEN 'SCH-M'    THEN 'Schema-Modification'
                    WHEN 'SCH-S'    THEN 'Schema-Stability'

        ELSE NULL
        END AS REQUEST_LOCK_MODE

FROM   sys.dm_tran_locks   AS L
       join sys.partitions AS P 
        on L.resource_associated_entity_id = p.hobt_id

参考:http://sqlblog.foxraven.com/2012/01/check-to-see-if-table-is-locked.html