SQL Server存储过程中的死锁

时间:2018-06-04 07:31:37

标签: sql-server tsql

我遇到了一个僵局,我无法用我非常有限的SQL Server知识来解决这个问题。 SP看起来像这样(实际的列/表/ SP名称编辑):

ALTER PROCEDURE MySP
    @id BIGINT = NULL
AS
BEGIN
    SET NOCOUNT ON;
    SET TRAN ISOLATION LEVEL READ UNCOMMITTED;

    DECLARE @ids TABLE ([ID] BIGINT, [Col1] TINYINT);
    DECLARE @idsUpdated [dbo].[BigIntList];

    UPDATE  Table1
    SET     [Col1] = CASE WHEN ([Col2] IS NOT NULL AND [Col2] <= GETUTCDATE()) THEN 5
                            WHEN ([Col3] IS NOT NULL AND [Col3] > GETUTCDATE()) THEN 4
                            ELSE 0
                      END
    OUTPUT  Inserted.[ID], Inserted.[Col1]
    INTO    @ids
    WHERE   ([ID] = @id AND [Col1] IN (0, 1, 4, 5))
            OR (@id IS NULL AND [Col1] IN (0, 4))

    INSERT INTO @idsUpdated (ID)
    SELECT      [id] FROM @ids 
    WHERE       [Col1] IN (0, 4)
    EXEC        SomeOtherSP @ids = @idsUpdated
END

Table1ID作为主键,上面有聚簇索引和一些数据列。 Col1上有一个非聚集索引。

我收集的死锁跟踪如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<deadlock>
   <victim-list>
      <victimProcess id="processbf11305848" />
   </victim-list>
   <process-list>
      <process id="processbf11305848" taskpriority="0" logused="6916" waitresource="KEY: 5:72057594153533440 (83cb2cafbb82)" waittime="1251" ownerId="177020808" transactionname="ParentSP" lasttranstarted="2018-05-29T21:03:26.013" XDES="0xc004810490" lockMode="U" schedulerid="2" kpid="13824" status="suspended" spid="66" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2018-05-29T21:03:26.087" lastbatchcompleted="2018-05-29T21:03:26.010" lastattention="1900-01-01T00:00:00.010" clientapp="Core .Net SqlClient Data Provider" hostname="webcrawler-1527627780-jq8fs" hostpid="479064550" loginname="api" isolationlevel="read uncommitted (1)" xactid="177020808" currentdb="5" lockTimeout="4294967295" clientoption1="673185824" clientoption2="128056">
         <executionStack>
            <frame procname="MySP" line="22" stmtstart="1452" stmtend="2496" sqlhandle="0x03000500843e711dfcb3dc00eaa8000001000000000000000000000000000000000000000000000000000000">UPDATE Table1...</frame>
            <frame procname="ParentSP" line="209" stmtstart="12288" stmtend="12356" sqlhandle="0x0300050068cf4121b7ddb600efa8000001000000000000000000000000000000000000000000000000000000">EXEC MySP @i</frame>
         </executionStack>
         <inputbuf>Proc [Database Id = 5 Object Id = 557961064]</inputbuf>
      </process>
      <process id="processbffc630108" taskpriority="0" logused="21312" waitresource="KEY: 5:72057594153533440 (9459fe930757)" waittime="1244" ownerId="177020807" transactionname="ParentSP" lasttranstarted="2018-05-29T21:03:25.983" XDES="0xbf34a24490" lockMode="U" schedulerid="3" kpid="7088" status="suspended" spid="65" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2018-05-29T21:03:26.087" lastbatchcompleted="2018-05-29T21:03:25.983" lastattention="1900-01-01T00:00:00.983" clientapp="Core .Net SqlClient Data Provider" hostname="webcrawler-1527627780-jq8fs" hostpid="479064550" loginname="api" isolationlevel="read uncommitted (1)" xactid="177020807" currentdb="5" lockTimeout="4294967295" clientoption1="673185824" clientoption2="128056">
         <executionStack>
            <frame procname="MySP" line="22" stmtstart="1452" stmtend="2496" sqlhandle="0x03000500843e711dfcb3dc00eaa8000001000000000000000000000000000000000000000000000000000000">UPDATE Table1...</frame>
            <frame procname="ParentSP" line="209" stmtstart="12288" stmtend="12356" sqlhandle="0x0300050068cf4121b7ddb600efa8000001000000000000000000000000000000000000000000000000000000">EXEC MySP @i</frame>
         </executionStack>
         <inputbuf>Proc [Database Id = 5 Object Id = 557961064]</inputbuf>
      </process>
   </process-list>
   <resource-list>
      <keylock hobtid="72057594153533440" dbid="5" objectname="Table1" indexname="PK_Table1" id="lockbf5ec94100" mode="X" associatedObjectId="72057594153533440">
         <owner-list>
            <owner id="processbffc630108" mode="X" />
         </owner-list>
         <waiter-list>
            <waiter id="processbf11305848" mode="U" requestType="wait" />
         </waiter-list>
      </keylock>
      <keylock hobtid="72057594153533440" dbid="5" objectname="Table1" indexname="PK_Table1" id="lockbf5ede1500" mode="X" associatedObjectId="72057594153533440">
         <owner-list>
            <owner id="processbf11305848" mode="X" />
         </owner-list>
         <waiter-list>
            <waiter id="processbffc630108" mode="U" requestType="wait" />
         </waiter-list>
      </keylock>
   </resource-list>
</deadlock>

0 个答案:

没有答案