分享一次U9 MSSQL死锁排查过程

2023/09/08

起因

MSSQL 2016数据库基本每天死锁,用户反馈U9生产订单操作卡死。

分析

SELECT sqltext.text,
       req.session_id,
       req.status,
       req.command,
       req.cpu_time,
       req.total_elapsed_time,
       req.blocking_session_id
FROM sys.dm_exec_requests req (NOLOCK)
    CROSS APPLY sys.dm_exec_sql_text(req.sql_handle) AS sqltext

image-20230822114819768

果然200多条SQL阻塞

定位根源阻塞session

SELECT  blocking_session_id,COUNT(*) AS 阻塞数量 FROM

(SELECT sqltext.text,
       req.session_id,
       req.status,
       req.command,
       req.cpu_time,
       req.total_elapsed_time,
       req.blocking_session_id
FROM sys.dm_exec_requests req (NOLOCK)
    CROSS APPLY sys.dm_exec_sql_text(req.sql_handle) AS sqltext)
    T1 
GROUP BY blocking_session_id
ORDER BY  COUNT(*) DESC

image-20230822115622367

  • 分析结果:阻塞session最终矛头都指向是session134

484 -> 121 -> 398 -> 267 ->134

388 ->267 -> 134

431 -> 400 ->388 -> 267 -> 134

272 -> 406 -> 388 ->267 ->134

400 -> 388 -> 267 -> 134

果然session134对应SQL是BE插件对表MO_MO高消耗UPDATE语句导致生产订单表死锁。

解决问题

优化BE插件SQL,经过观察20多天,数据库没有再次出现死锁。