了解SQL Server死锁图

时间:2015-01-01 14:08:59

标签: sql-server sql-server-2008

我有一个应用程序将被选为"死锁受害者"在SQL Server中。尝试在查询下面执行多个线程。

查询

merge Table_X as target  
using (values ('14410')) as source (CUST_ID) on target.CUST_ID = '14410'
when matched then  
    update 
       SET CUST_NAME = 'xyz', CLOSE_DATE = NULL,
           xx = 2, COMPLETE = 'No', 
           qwert = CASE  WHEN qwert is null and 'Low' = 'High' THEN getDate() ELSE null END, 
           ACTIVE = 1, xcount = '913af80db3f424e34a9055e0ea9bc391'  

when not matched then  
   INSERT (CUST_ID, CUST_NAME, OPEN_DATE, CLOSE_DATE, xx, COMPLETE, qwert, ddd, ACTIVE, xcount )  
   VALUES ('14410', 'U.S. Robotics and Mechanical Men', '2007-08-31 15:14:23.0', NULL, 2, 'No', NULL, 0, 1, '913af80db3f424e34a9055e0ea9bc391') ;

DECLARE @54229 numeric(19,0)
SET @54229 = ( SELECT id from Table_X  where CUST_ID = '14410' )

insert into Table_Y (xyz, abc, ax, ay, az, bx, bxz, bz, UPDATED, abcd) 
   select 
       xyz, abc, ax, ay, az, bx, bxz, bz, UPDATED, abcd 
   from 
       Table_Z 
   where 
       abcd = @54229

merge Table_Z as target 
using (values (@54229)) as source (abcd) on target.abcd = @54229 
when matched then  
    UPDATE  
       SET xyz = 1.1, abc = 1.1, ax = 1.1, ay = 1.1, az = 1.1, 
           bx = 1.1, bxz = 'Low', bz = 1.1, UPDATED = getdate()

when not matched then 
    INSERT (xyz, abc, ax, ay, az, IS_FORCE_EDD, bx, bxz, bz, UPDATED, abcd) 
    VALUES(1.1, 1.1, 1.1, 1.1, 1.1, 0, 1.1,'Low', 1.1, getdate(), @54229);

INSERT INTO Table_A(ACTION, DATE_TIME, PROFILE_ID, USER_ID)
VALUES('Profile Change', getdate(), @54229, 1)  

Deadlock diagram

任何人都可以解释这个死锁图吗?

并为我提供防止这种僵局的解决方案。

我已阅读多个内容并尝试了with (nolock)isolation level,但我没有得到正确的解决方案。

死锁受害者是否存在与索引相关的问题?

2 个答案:

答案 0 :(得分:1)

我创建了非聚集索引   之后我还没有观察到僵局受害者的例外情况。

CREATE NONCLUSTERED INDEX [T_abcd] ON [dbo].[Table_Z]
    (
        [abcd] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO

答案 1 :(得分:0)

我认为在代码的早期获取TABLE_Z的读锁将解决问题。

以下是关于此的问题: Obtain Update Table Lock at start of Stored Procedure in SQL Server

编辑:在您写入之前,您正在阅读TABLE_Z。假设你有两个线程/连接(A& B)运行:

A需要读锁

B采用读锁

A尝试进行写锁定但必须等待B释放读锁定。

B尝试进行写锁定但必须等待A释放读锁定。

现在出现了僵局。