我在一个相对简单的查询中遇到了僵局:
select coalesce((select max(id) from table where year < 2016),0)
错误: 事务在使用另一个进程的通用可等待对象资源上死锁,并被选为死锁牺牲品。重新运行该交易。
详细说明:
如果我使用isnull而不是coalesce,则不会发生错误。
死锁图:
<?xml version="1.0" encoding="UTF-8"?>
<deadlock>
<victim-list>
<victimProcess id="process697ca6c108" />
</victim-list>
<process-list>
<process id="process697ca6c108" taskpriority="0" logused="20000" waittime="2630" schedulerid="4" kpid="648" status="suspended" spid="62" sbid="0" ecid="3" priority="0" trancount="0" lastbatchstarted="2017-04-10T09:30:00.030" lastbatchcompleted="2017-04-10T09:30:00.030" lastattention="1900-01-01T00:00:00.030" clientapp="Microsoft SQL Server Management Studio - Query" hostname="MyHost" hostpid="6328" isolationlevel="read committed (2)" xactid="90846084" currentdb="9" lockTimeout="4294967295" clientoption1="671090784" clientoption2="390200">
<executionStack>
<frame procname="adhoc" line="1" stmtend="160" sqlhandle="0x0200000000342d3438c95d7d95220ffb20aa484d79bf55d20000000000000000000000000000000000000000">unknown</frame>
</executionStack>
<inputbuf>select coalesce( (select max(id) from MyTable where year &lt; 2016),0)</inputbuf>
</process>
<process id="process697c9c1088" taskpriority="0" logused="20000" waittime="3923" schedulerid="6" kpid="9016" status="suspended" spid="62" sbid="0" ecid="2" priority="0" trancount="0" lastbatchstarted="2017-04-10T09:30:00.030" lastbatchcompleted="2017-04-10T09:30:00.030" lastattention="1900-01-01T00:00:00.030" clientapp="Microsoft SQL Server Management Studio - Query" hostname="MyHost" hostpid="6328" isolationlevel="read committed (2)" xactid="90846084" currentdb="9" lockTimeout="4294967295" clientoption1="671090784" clientoption2="390200">
<executionStack>
<frame procname="adhoc" line="1" stmtend="160" sqlhandle="0x0200000000342d3438c95d7d95220ffb20aa484d79bf55d20000000000000000000000000000000000000000">unknown</frame>
</executionStack>
<inputbuf>select coalesce( (select max(id) from MyTable where year &lt; 2016),0)</inputbuf>
</process>
<process id="process697ca4f848" taskpriority="0" logused="20000" waittime="3923" schedulerid="5" kpid="11792" status="suspended" spid="62" sbid="0" ecid="4" priority="0" trancount="0" lastbatchstarted="2017-04-10T09:30:00.030" lastbatchcompleted="2017-04-10T09:30:00.030" lastattention="1900-01-01T00:00:00.030" clientapp="Microsoft SQL Server Management Studio - Query" hostname="MyHost" hostpid="6328" isolationlevel="read committed (2)" xactid="90846084" currentdb="9" lockTimeout="4294967295" clientoption1="671090784" clientoption2="390200">
<executionStack>
<frame procname="adhoc" line="1" stmtend="160" sqlhandle="0x0200000000342d3438c95d7d95220ffb20aa484d79bf55d20000000000000000000000000000000000000000">unknown</frame>
</executionStack>
<inputbuf>select coalesce( (select max(id) from MyTable where year &lt; 2016),0)</inputbuf>
</process>
<process id="process697ca38ca8" taskpriority="0" logused="20000" waittime="3923" schedulerid="2" kpid="16468" status="suspended" spid="62" sbid="0" ecid="1" priority="0" trancount="0" lastbatchstarted="2017-04-10T09:30:00.030" lastbatchcompleted="2017-04-10T09:30:00.030" lastattention="1900-01-01T00:00:00.030" clientapp="Microsoft SQL Server Management Studio - Query" hostname="MyHost" hostpid="6328" isolationlevel="read committed (2)" xactid="90846084" currentdb="9" lockTimeout="4294967295" clientoption1="671090784" clientoption2="390200">
<executionStack>
<frame procname="adhoc" line="1" stmtend="160" sqlhandle="0x0200000000342d3438c95d7d95220ffb20aa484d79bf55d20000000000000000000000000000000000000000">unknown</frame>
</executionStack>
<inputbuf>select coalesce( (select max(id) from MyTable where year &lt; 2016),0)</inputbuf>
</process>
</process-list>
<resource-list>
<SyncPoint>
<owner-list>
<owner id="process697c9c1088" />
<owner id="process697ca38ca8" />
<owner id="process697ca4f848" />
</owner-list>
<waiter-list>
<waiter id="process697ca6c108" />
</waiter-list>
</SyncPoint>
<SyncPoint>
<owner-list>
<owner id="process697ca6c108" />
</owner-list>
<waiter-list>
<waiter id="process697c9c1088" />
</waiter-list>
</SyncPoint>
<SyncPoint>
<owner-list>
<owner id="process697ca6c108" />
</owner-list>
<waiter-list>
<waiter id="process697ca4f848" />
</waiter-list>
</SyncPoint>
<SyncPoint>
<owner-list>
<owner id="process697ca6c108" />
</owner-list>
<waiter-list>
<waiter id="process697ca38ca8" />
</waiter-list>
</SyncPoint>
</resource-list>
</deadlock>