3

我观察到我的数据库非常奇怪的行为。我有一个小表(大约 300 行),其中一个字段不断更新。

而且我在那里遇到了很多死锁 - 表的更新导致同一张表的类似更新死锁(U 锁与 X 锁)。

所以我决定删除聚集索引(所以表现在没有任何索引)来修复死锁。但这并没有帮助,现在我在 U 和 X 锁定模式之间陷入僵局。

所以一张表,没有索引和 2 个会话更新一张表

受害者

update dbo.MyNumber set
  @nextno = nextno = nextno + 1
where [type] = @type
  and yearid = @yearid


中奖查询:

update dbo.MyNumber set
  @nextno = nextno = nextno + 1
where [type] = @TYPE
  and yrclosedyn = 0

行肯定不同,但页面是相同的。

这怎么可能?也许它与锁升级有关,或者......?

我真的很感激任何建议。

在此先感谢迈克

死锁 XML:

<deadlock-list>
 <deadlock victim="process6c492e8">
  <process-list>
   <process id="processb6a988" taskpriority="0" logused="1848" waitresource="RID: 5:1:127478:16" waittime="3478" ownerId="17153439" transactionname="user_transaction" lasttranstarted="2012-12-18T12:31:40.147" XDES="0xffffffff89482258" lockMode="U" schedulerid="7" kpid="4248" status="suspended" spid="98" sbid="0" ecid="0" priority="0" transcount="2" lastbatchstarted="2012-12-18T12:31:49.913" lastbatchcompleted="2012-12-18T12:31:49.913" clientapp="PenAIR" hostname="S16047425" hostpid="9300" loginname="sa" isolationlevel="read committed (2)" xactid="17153439" currentdb="5" lockTimeout="4294967295" clientoption1="673185824" clientoption2="128056">
    <executionStack>
     <frame procname="MYDATABASE.dbo.MyStoredProcedure" line="92" stmtstart="9062" stmtend="9388" sqlhandle="0x030005002d15a05e58b5710016a100000100000000000000">
UPDATE dbo.MyNumber Set
  @NEXTNO = NEXTNO = NEXTNO + 1
WHERE  (TYPE = @TYPE) AND (YRCLOSEDYN = 0)     </frame>
    </executionStack>
    <inputbuf>
Proc [Database Id = 5 Object Id = 1587549485]    </inputbuf>
   </process>
   <process id="process6c492e8" taskpriority="0" logused="192" waitresource="RID: 5:1:127478:20" waittime="8252" ownerId="17153562" transactionname="user_transaction" lasttranstarted="2012-12-18T12:31:45.140" XDES="0x6583b1e0" lockMode="U" schedulerid="13" kpid="19824" status="suspended" spid="143" sbid="0" ecid="0" priority="0" transcount="2" lastbatchstarted="2012-12-18T12:31:45.140" lastbatchcompleted="2012-12-18T12:31:45.140" clientapp="PenAIR" hostname="S16047425" hostpid="4760" loginname="sa" isolationlevel="read committed (2)" xactid="17153562" currentdb="5" lockTimeout="4294967295" clientoption1="673185824" clientoption2="128056">
    <executionStack>
     <frame procname="MYDATABASE.dbo.MyStoredProcedure" line="92" stmtstart="9062" stmtend="9388" sqlhandle="0x030005002d15a05e58b5710016a100000100000000000000">
UPDATE dbo.MyNumber Set
  @NEXTNO = NEXTNO = NEXTNO + 1
WHERE  ([TYPE] = @TYPE) AND (YRCLOSEDYN = 0)     </frame>
    </executionStack>
    <inputbuf>
Proc [Database Id = 5 Object Id = 1587549485]    </inputbuf>
   </process>
  </process-list>
  <resource-list>
   <ridlock fileid="1" pageid="127478" dbid="5" objectname="MYDATABASE.dbo.MyNumber" id="lock464f2640" mode="X" associatedObjectId="72057594131120128">
    <owner-list>
     <owner id="processb6a988" mode="X"/>
    </owner-list>
    <waiter-list>
     <waiter id="process6c492e8" mode="U" requestType="wait"/>
    </waiter-list>
   </ridlock>
   <ridlock fileid="1" pageid="127478" dbid="5" objectname="MYDATABASE.dbo.MyNumber" id="lockfffffffff1974980" mode="X" associatedObjectId="72057594131120128">
    <owner-list>
     <owner id="process6c492e8" mode="X"/>
    </owner-list>
    <waiter-list>
     <waiter id="processb6a988" mode="U" requestType="wait"/>
    </waiter-list>
   </ridlock>
  </resource-list>
 </deadlock>
</deadlock-list>
4

1 回答 1

3

将您的死锁图分解为表格形式显示以下内容。

+----------+-------------------------+-----------+-----------+------------+----------+--------------------+--------------------+---------+
| LockMode |      LockedObject       | TranCount | LockEvent | LockedMode | WaitMode |    WaitResource    |   IsolationLevel   | LogUsed |
+----------+-------------------------+-----------+-----------+------------+----------+--------------------+--------------------+---------+
| U        | MYDATABASE.dbo.MyNumber | NULL      | rid       | X          | U        | RID: 5:1:127478:20 | read committed (2) |     192 |
| U        | MYDATABASE.dbo.MyNumber | NULL      | rid       | X          | U        | RID: 5:1:127478:16 | read committed (2) |    1848 |
+----------+-------------------------+-----------+-----------+------------+----------+--------------------+--------------------+---------+

您还没有在评论中回答我关于序列生成代码是否在每个事务中只调用一次的问题。

如果没有,很容易生成类似于您帖子中的死锁图。

设置

CREATE TABLE dbo.MyNumber
  (
     [TYPE]     CHAR(1),
     YRCLOSEDYN INT,
     NEXTNO     INT
  )

INSERT INTO dbo.MyNumber
VALUES      ('X', 0, 1),
            ('Y', 0, 1)

GO

CREATE PROC MyStoredProcedure @TYPE   CHAR(1),
                              @NEXTNO INT OUTPUT
AS
    UPDATE dbo.MyNumber
    SET    @NEXTNO = NEXTNO = NEXTNO + 1
    WHERE  ( [TYPE] = @TYPE )
           AND ( YRCLOSEDYN = 0 )

连接 1

BEGIN TRAN

DECLARE @NEXTNO INT

EXEC MyStoredProcedure 'Y', @NEXTNO OUTPUT

WAITFOR DELAY '00:00:05'

EXEC MyStoredProcedure 'X', @NEXTNO OUTPUT

ROLLBACK 

连接 2

(执行连接1中的代码后立即运行)

BEGIN TRAN
DECLARE @NEXTNO INT

EXEC MyStoredProcedure 'X', @NEXTNO OUTPUT

EXEC MyStoredProcedure 'Y', @NEXTNO OUTPUT

ROLLBACK

从中输出的死锁图与上面的非常相似

+----------+-------------------------+-----------+-----------+------------+----------+-----------------+--------------------+---------+
| LockMode |      LockedObject       | TranCount | LockEvent | LockedMode | WaitMode |  WaitResource   |   IsolationLevel   | LogUsed |
+----------+-------------------------+-----------+-----------+------------+----------+-----------------+--------------------+---------+
| U        | MYDATABASE.dbo.MyNumber |         2 | rid       | X          | U        | RID: 11:1:144:1 | read committed (2) |     248 |
| U        | MYDATABASE.dbo.MyNumber |         2 | rid       | X          | U        | RID: 11:1:144:0 | read committed (2) |     248 |
+----------+-------------------------+-----------+-----------+------------+----------+-----------------+--------------------+---------+

如果这是对您的问题的解释,您将需要确保在所有事务中以相同的顺序更新序列(我认为必须有一些充分的理由说明您不能只使用基于IDENTITY列的解决方案

于 2012-12-20T13:58:12.103 回答