gpt4 book ai didi

sql-server - 了解特定类型的死锁

转载 作者:行者123 更新时间:2023-12-02 05:59:32 25 4
gpt4 key购买 nike

我试图了解特定类型的死锁是如何发生的。

我有一个非常简单的死锁图,其中包含两个资源和两个进程。一个运行 SELECT 另一个运行 UPDATE 语句。我知道在某些情况下这可能会导致死锁,但我不明白在这种特定情况下它是如何发生的。

受害者的询问:

SELECT id_particle, id_event, dt_created, dt_rcvd, tlx_no, from_routename, re, deleted 
FROM msg_list
WHERE to_routename = @P0
AND ((dt_answ IS NULL AND b_complete = 0 AND id_event=6) OR (id_event = 10 AND deleted = 0)
OR (from_id_post = 9705 AND deleted = 0))
ORDER BY dt_created ASC

获奖者的询问:

UPDATE msg_list 
SET
ID_EVENT=7,
STATUS='Answered',
DT_ANSW={ts '2017-02-12 05:34:14'
WHERE ID_PARTICLE = 46211816

完整的死锁图:

<deadlock-list>
<deadlock victim="process30aa42d468">
<process-list>
<process id="process30aa42d468" taskpriority="0" logused="0" waitresource="PAGE: 6:1:155679 " waittime="921" ownerId="427175775" transactionname="SELECT" lasttranstarted="2017-02-12T05:34:48.293" XDES="0x3126fbba40" lockMode="S" schedulerid="4" kpid="10648" status="suspended" spid="184" sbid="0" ecid="0" priority="0" trancount="0" lastbatchstarted="2017-02-12T05:34:48.293" lastbatchcompleted="2017-02-12T05:34:48.280" lastattention="1900-01-01T00:00:00.280" clientapp="jTDS" hostname="LNS" hostpid="123" loginname="MsgStore" isolationlevel="read committed (2)" xactid="427175775" currentdb="6" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="tempdb.dbo.#jtds000003_____________________________________________________________________________________________________________0042FD9D" line="1" stmtstart="92" stmtend="668" sqlhandle="0x030002005819e1b4efce580018a7000001000000000000000000000000000000000000000000000000000000">
SELECT id_particle, id_event, dt_created, dt_rcvd, tlx_no, from_routename, re, deleted FROM msg_list WHERE to_routename = @P0 AND ((dt_answ IS NULL AND b_complete = 0 AND id_event=6) OR (id_event = 10 AND deleted = 0) OR (from_id_post = 9705 AND deleted = 0)) ORDER BY dt_created AS </frame>
</executionStack>
<inputbuf>
Proc [Database Id = 2 Object Id = -1260316328] </inputbuf>
</process>
<process id="process319282aca8" taskpriority="0" logused="21256" waitresource="PAGE: 6:1:396658 " waittime="869" ownerId="427175573" transactionname="implicit_transaction" lasttranstarted="2017-02-12T05:34:48.210" XDES="0x30c2dbc408" lockMode="IX" schedulerid="4" kpid="11980" status="suspended" spid="99" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2017-02-12T05:34:48.430" lastbatchcompleted="2017-02-12T05:34:48.427" lastattention="2017-02-12T03:34:28.130" clientapp="NotesMover" hostname="LNS" hostpid="8828" loginname="Notesmover" isolationlevel="read committed (2)" xactid="427175573" currentdb="6" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128058">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="364" stmtend="812" sqlhandle="0x020000006d523b316d15563a50b97ce8d56da3cf6d8fc4450000000000000000000000000000000000000000">
unknown </frame>
<frame procname="adhoc" line="1" stmtend="554" sqlhandle="0x02000000d8b78713c51b1588947edf24f2d7b69031f4f1d60000000000000000000000000000000000000000">
unknown </frame>
<frame procname="unknown" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
UPDATE msg_list SET ID_EVENT=7, STATUS='Answered', DT_ANSW={ts '2017-02-12 05:34:14'WHERE ID_PARTICLE = 46211816
</inputbuf>
</process>
</process-list>
<resource-list>
<pagelock fileid="1" pageid="155679" dbid="6" subresource="FULL" objectname="MsgStore.dbo.msg_list" id="lock31683f2400" mode="IX" associatedObjectId="72057594523811840">
<owner-list>
<owner id="process319282aca8" mode="IX"/>
</owner-list>
<waiter-list>
<waiter id="process30aa42d468" mode="S" requestType="wait"/>
</waiter-list>
</pagelock>
<pagelock fileid="1" pageid="396658" dbid="6" subresource="FULL" objectname="MsgStore.dbo.msg_list" id="lock2f80c2e300" mode="S" associatedObjectId="72057594523811840">
<owner-list>
<owner id="process30aa42d468" mode="S"/>
</owner-list>
<waiter-list>
<waiter id="process319282aca8" mode="IX" requestType="wait"/>
</waiter-list>
</pagelock>
</resource-list>
</deadlock>
</deadlock-list>

以及相同的图形表示:

Deadlock Graph

以下是运行这两个查询时生成的查询计划:

受害者:

Execution Plan Victim

获胜者:

Execution Plan Winner

受害者使用的ncci_rcvd_list索引是一个非聚集覆盖索引,具有单个键列和多个包含列。其中包含的专栏之一由获胜者更新。键列未更新。

mgs_list 表大约有 500,000 条记录。 SELECT 查询返回的结果集通常只有几行,例如十几行(尽管有时可能会更大,最多可能有几千行)。

有人可以解释一下这种情况如何导致死锁吗?

这是从生产系统中捕获的,这种情况经常发生。我已经通过更改事务隔离级别以读取已提交的快照解决了这个问题,这不再是问题,但我想了解那里发生了什么,然后看看是否有不同的解决方案来解决该问题。

最佳答案

页面 155679 在 IX 模式下由 process319282aca8(UPDATE)拥有,在 S 模式下由 process30aa42d468(SELECT)拥有。页 396658 由 S 模式下的 SELECT 拥有,并由 IX 模式下的 UPDATE 需要。

问题是两个查询都缺少索引。 UPDATE 使用索引但未覆盖,因此需要在聚集索引中查找 RID。 SELECT 是一次扫描(由页级 S 锁泄露...)。这在并发情况下是有保证的死锁。

这是滥用表作为队列的另一个例子。阅读 Using tables as Queues 。将您的状态与您的事件分开。仅对事件进行入队和出队。

PS。 “受害者”将始终是需要回滚的工作较少的事务,并且在读取与写入中,这将始终是读取。另外,我怀疑您发布的 SELECT 执行计划是真正陷入僵局的计划。

关于sql-server - 了解特定类型的死锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42223676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com