检测 SQL 服务器更新锁定的原因

Detect the cause of SQL Server update lock

问题:

业务交易期间的 .NET 应用程序执行类似

的查询
UPDATE Order 
SET Description = 'some new description` 
WHERE OrderId = @p1 AND RowVersion = @p2

此查询挂起直到超时(几分钟),然后我得到一个异常:

SqlException: Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

在数据库负载很重的情况下(每天几次)重现。
我需要检测查询锁定的原因。

我试过的:

  1. 探索 activity 监视器 - 它显示查询被锁定挂起。通过 headblocker 过滤并不能提供太多信息,它经常变化。

  2. Analyze SQL script, that gives similar to activity monitor data - almost same result as looking to activity monitor. Chasing blocking_session_id results in some session, that awaits for command or executing some SQL, I can't reason a relation to Order table. Executing the same script in a second gives other session. I also tried a some other queries/stored procedures from this atritcle 没有结果。

  3. 构建标准 SQL locked/problem 事务的服务器报告导致错误,例如最大递归耗尽或本地 OutOfMemory 异常(我有 16 Gb RAM)。

数据库详细信息

我对这类问题绝对陌生,所以我肯定错过了很多。
任何帮助或指导都会很棒!

尝试使用 sys.dm_exec_requests 视图,并按列筛选 blocking_session_id, wait_time

如果有人感兴趣,我发现这个特别有用的查询:

SELECT tl.resource_type
 ,OBJECT_NAME(p.object_id) AS object_name
 ,tl.request_status
 ,tl.request_mode
 ,tl.request_session_id
 ,tl.resource_description
 ,(select text from sys.dm_exec_sql_text(r.sql_handle))
FROM sys.dm_tran_locks tl
    INNER JOIN sys.dm_exec_requests r ON tl.request_session_id=r.session_id
    LEFT JOIN sys.partitions p ON p.hobt_id = tl.resource_associated_entity_id
WHERE tl.resource_database_id = DB_ID()
    AND OBJECT_NAME(p.object_id) = '<YourTableName>'
ORDER BY tl.request_session_id

它显示了在 <YourTableName> 上获得锁的事务以及它们现在正在执行的查询。