INSERT 和 SELECT SP 之间的死锁

Posted

技术标签:

【中文标题】INSERT 和 SELECT SP 之间的死锁【英文标题】:Deadlock between INSERT and SELECT SPs 【发布时间】:2014-03-19 13:27:57 【问题描述】:

我遇到了一个应用程序问题,我遇到了我认为永远不会发生的死锁。我有两个存储过程。其中一个只是读取表的一部分,其中一个在表中添加一行,或者如果存在主键冲突,则更新现有行。

我已经阅读了有关存储过程可以获得共享读锁的信息,如果两个存储过程都升级为排他锁,则会出现死锁。我在这里看不到这种情况,因为 GetList 存储过程只是读取而从不写入。这些存储过程死锁还有其他可能的原因吗?

以下关于死锁的报告:

<?xml version="1.0" encoding="UTF-8"?>
<deadlock-list>
   <deadlock victim="process3e37f62c8">
      <process-list>
         <process id="process3e37f62c8" taskpriority="0" logused="0" waitresource="KEY: 7:72057594152681472 (de2613e7782e)" waittime="2535" ownerId="7732055781" transactionname="SELECT" lasttranstarted="2014-03-17T17:48:31.437" XDES="0x1298bc1c0" lockMode="S" schedulerid="11" kpid="21372" status="suspended" spid="121" sbid="0" ecid="0" priority="0" trancount="0" lastbatchstarted="2014-03-17T17:48:31.437" lastbatchcompleted="2014-03-17T17:48:31.437" clientapp=".Net SqlClient Data Provider" hostname="STO2AP07" hostpid="4908" loginname="(hidden)" isolationlevel="read committed (2)" xactid="7732055781" currentdb="7" lockTimeout="4294967295" clientoption1="673185824" clientoption2="128056">
            <executionStack>
               <frame procname="(hidden).dbo.GetList" line="18" stmtstart="1060" stmtend="1942" sqlhandle="0x03000700072cfd1b7b8be800cfa200000100000000000000">
                   SELECT 
                        TOP (@NumRows)
                        [T1].[Id] , 
                        [T1].[A] , 
                        [T1].[B] , 
                        [T1].[C] , 
                        [T1].[D] , 
                        [T1].[E] , 
                        [T1].[F] , 
                        [T1].[G] , 
                        [T1].[H] , 
                        [T1].[I] , 
                        [T1].[J] 
                    FROM [Item] AS [T1] where [J] > @LastUpdatedDateTime ORDER BY [T1].[LastUpdatedField] ASC
                </frame>
            </executionStack>
            <inputbuf>Proc [Database Id = 7 Object Id = 469576711]</inputbuf>
         </process>
         <process id="process5a514c8" taskpriority="0" logused="244" waitresource="KEY: 7:72057594152615936 (16f70bd264f5)" waittime="2535" ownerId="7732055725" transactionname="user_transaction" lasttranstarted="2014-03-17T17:48:31.437" XDES="0x6602dfa00" lockMode="X" schedulerid="13" kpid="21196" status="suspended" spid="267" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2014-03-17T17:48:31.437" lastbatchcompleted="2014-03-17T17:48:31.437" clientapp=".Net SqlClient Data Provider" hostname="STO2AP07" hostpid="4908" loginname="(hidden)" isolationlevel="read committed (2)" xactid="7732055725" currentdb="7" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
            <executionStack>
               <frame procname="(hidden).dbo.AddToList" line="52" stmtstart="2484" stmtend="3294" sqlhandle="0x030007005cbf2019598be800cfa200000100000000000000">
                    UPDATE [Item] 
                    SET 
                        [A] = @A , 
                        [B] = @B , 
                        [C] = @C , 
                        [D] = @D , 
                        [E] = @E , 
                        [F] = @F , 
                        [G] = @G , 
                        [H] = @H , 
                        [I] = @I ,
                        [J] = @J 
                    WHERE [Id] = @Id
                </frame>
            </executionStack>
            <inputbuf>Proc [Database Id = 7 Object Id = 421576540]</inputbuf>
         </process>
      </process-list>
      <resource-list>
         <keylock hobtid="72057594152681472" dbid="7" objectname="(hidden).dbo.Item" indexname="PK_Item_1" id="lock47c381b80" mode="X" associatedObjectId="72057594152681472">
            <owner-list>
               <owner id="process5a514c8" mode="X" />
            </owner-list>
            <waiter-list>
               <waiter id="process3e37f62c8" mode="S" requestType="wait" />
            </waiter-list>
         </keylock>
         <keylock hobtid="72057594152615936" dbid="7" objectname="(hidden).dbo.Item" indexname="IX_LastUpdatedField" id="lock402a95800" mode="S" associatedObjectId="72057594152615936">
            <owner-list>
               <owner id="process3e37f62c8" mode="S" />
            </owner-list>
            <waiter-list>
               <waiter id="process5a514c8" mode="X" requestType="wait" />
            </waiter-list>
         </keylock>
      </resource-list>
   </deadlock>
</deadlock-list>

以下是名为GetList的存储过程:

ALTER PROCEDURE [dbo].[GetList]
    @LastUpdatedDateTime datetime,
    @NumRows int
WITH RECOMPILE
AS
BEGIN
    SET NOCOUNT ON;

    SELECT TOP (@NumRows)
           [T1].[Id]
         , [T1].[A]
         , [T1].[B]
         , [T1].[C]
         , [T1].[D]
         , [T1].[E]
         , [T1].[F]
         , [T1].[G]
         , [T1].[H]
         , [T1].[I]
         , [T1].[J]
      FROM [Item] AS [T1] where J >= @LastUpdatedDateTime
  ORDER BY [T1].[J] ASC
END

以下是名为 AddToList 的存储过程:

ALTER PROCEDURE [dbo].[AddToList]
    @Id int,
    @A varchar(30),
    @B decimal(18,2),
    @V varchar(30),
    @D varchar(512),
    @E datetime,
    @F datetime,
    @G bit,
    @H int,
    @I int
    @J datetime,
AS
IF NOT EXISTS(SELECT Id FROM Item WHERE [Id] = @Id)
    BEGIN
        SET NOCOUNT ON;
        INSERT INTO [Item] 
                  ( [Id] 
                  , [A]
                  , [B]
                  , [C]
                  , [D]
                  , [E]
                  , [F]
                  , [G]
                  , [H]
                  , [I]
                  , [J]
                  )
             VALUES
                  ( @Id
                  , @A
                  , @B
                  , @C
                  , @D
                  , @E
                  , @F
                  , @G
                  , @H
                  , @I
                  , @J
                  )
    END
ELSE
    BEGIN
        UPDATE [Item] SET
                   [A] = @A
                  , [B] = @B
                  , [C] = @C
                  , [D] = @D
                  , [E] = @E
                  , [F] = @F
                  , [G] = @G
                  , [H] = @H
                  , [I] = @I
                  , [J] = @J
        WHERE [Id] = @Id
    END

【问题讨论】:

【参考方案1】:

选择这样做:

    从 LastUpdatedDateTime 索引读取(S 锁) 查找对应的 CI 行(S 锁)

更新是这样的:

    写入 CI(X 锁) 写入 LastUpdatedDateTime 索引(X 锁定)

他们以相反的顺序访问两个不兼容的资源。死锁。

阅读器是否在高于READ COMMITTED 的隔离级别下运行? READ COMMITTED 应该在读取锁定的行后立即释放锁。

如果您可以使读取事务使用快照隔离模型,死锁将以简单、可靠和永久的方式消失。

如果不可能,请使用READ COMMITTED 隔离级别。

如果这不可行,您将不得不弄乱锁定提示(难以维护)或实现死锁重试逻辑。

【讨论】:

感谢您的快速回复!现在我想我明白他们是如何陷入僵局的。您不知道有一种方法可以跟踪获取锁的顺序吗? READ COMMITTED 已经是当前的隔离级别了,还能死锁吗? 如果 iso 级别真的是 READ COMMITTED 他们不应该死锁。也许这里正在发生其他事情。 SQL Profiler 可以跟踪锁。 这肯定是 READ COMMITTED,我想我得进一步调查一下

以上是关于INSERT 和 SELECT SP 之间的死锁的主要内容,如果未能解决你的问题,请参考以下文章

两个INSERT发生死锁原因剖析

insert …select …带来的死锁问题

mysql insert into select 语句为啥会造成死锁

InnoDB RR隔离级别下INSERT SELECT两种死锁案例剖析

mysql insert into select 语句为啥会造成死锁

mysql执行某种操作,卡死线程