如何解决如何解决由于 SQL Server 中的 PageLock 导致的死锁
我在 sql Server 中有一个表 adjustment_details_backup
。我在这里附上了表格的架构:
+---------------------------+---------+------------+-------------------------+
| Name | Owner | Type | Created_datetime |
|---------------------------+---------+------------+-------------------------|
| adjustment_details_backup | dbo | user table | 2020-02-24 13:14:20.370 |
+---------------------------+---------+------------+-------------------------+
+---------------+----------+------------+----------+--------+---------+------------+----------------------+
| Column_name | Type | Computed | Length | Prec | Scale | Nullable | TrimTrailingBlanks |
|---------------+----------+------------+----------+--------+---------+------------+----------------------+
| id | int | no | 4 | 10 | 0 | no | (n/a) |
| cid | int | no | 4 | 10 | 0 | no | (n/a) |
| promotion_id | int | no | 4 | 10 | 0 | no | (n/a) |
| adjustment_id | int | no | 4 | 10 | 0 | no | (n/a) |
| target_type | varchar | no | 10 | | | no | no |
| target_ids | nvarchar | no | -1 | | | no | (n/a) |
| bf_ids | nvarchar | no | -1 | | | yes | (n/a) |
+---------------+----------+------------+----------+--------+---------+------------+----------------------
+--------------------------------+-------------------------------------------------------+-------------------+
| index_name | index_description | index_keys |
|--------------------------------+-------------------------------------------------------+-------------------|
| adj_details_backup_cid_pid_idx | clustered | cid,promotion_id |
| PK__adjustme__3213E83EAF725CC9 | nonclustered,unique,primary key,stats no recompute | id |
| UQ__adjustme__32324848CA673053 | nonclustered,unique key | adjustment_id |
+--------------------------------+-------------------------------------------------------+-------------------+
schema for adjustment_details_backup
此表包含促销 ID 的多条记录。我在并发执行的查询之间遇到了死锁。
查询 1:
delete from adjustment_details_backup
where promotion_id = 28492
查询 2:
delete from adjustment_details_backup
where promotion_id = 28260
死锁图的 XML 是:
<deadlock>
<victim-list>
<victimProcess id="process26ce2575848" />
</victim-list>
<process-list>
<process id="process26ce2575848" taskpriority="0" logused="0" waitresource="PAGE: 5:1:1643475 " waittime="19306" ownerId="5395124916" transactionname="implicit_transaction" lasttranstarted="2021-04-12T22:57:20.240" XDES="0x26cbb4f7aa0" lockMode="U" schedulerid="6" kpid="138164" status="suspended" spid="367" sbid="0" ecid="7" priority="0" trancount="0" lastbatchstarted="2021-04-12T22:57:20.240" lastbatchcompleted="2021-04-12T22:57:20.177" lastattention="1900-01-01T00:00:00.177" clientapp="Microsoft JDBC Driver for sql Server" hostname="forecast-mgmt-747b85c647-twgm5" hostpid="0" isolationlevel="read committed (2)" xactid="5395124916" currentdb="5" currentdbname="forecast-management" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128058">
<executionStack>
<frame procname="unkNown" queryhash="0x9c39d2d962cc226f" queryplanhash="0x3a3f2196dc34ec92" line="1" stmtend="126" sqlhandle="0x02000000e3619d1890952212a37333bffa6d0ca92cc3684c0000000000000000000000000000000000000000">
unkNown </frame>
<frame procname="unkNown" queryhash="0x9c39d2d962cc226f" queryplanhash="0x3a3f2196dc34ec92" line="1" stmtend="126" sqlhandle="0x0200000005301203bd2a6100166b96a8b076cf61b9cf61140000000000000000000000000000000000000000">
unkNown </frame>
<frame procname="unkNown" queryhash="0x0000000000000000" queryplanhash="0x0000000000000000" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unkNown </frame>
</executionStack>
<inputbuf>
delete from adjustment_details_backup where promotion_id = 28260 </inputbuf>
</process>
<process id="process26ce2577c28" taskpriority="0" logused="0" waitresource="PAGE: 5:1:1666487 " waittime="19302" ownerId="5395124916" transactionname="implicit_transaction" lasttranstarted="2021-04-12T22:57:20.240" XDES="0x26077e2daa0" lockMode="U" schedulerid="8" kpid="97276" status="suspended" spid="367" sbid="0" ecid="6" priority="0" trancount="0" lastbatchstarted="2021-04-12T22:57:20.240" lastbatchcompleted="2021-04-12T22:57:20.177" lastattention="1900-01-01T00:00:00.177" clientapp="Microsoft JDBC Driver for sql Server" hostname="forecast-mgmt-747b85c647-twgm5" hostpid="0" isolationlevel="read committed (2)" xactid="5395124916" currentdb="5" currentdbname="forecast-management" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128058">
<executionStack>
<frame procname="unkNown" queryhash="0x9c39d2d962cc226f" queryplanhash="0x3a3f2196dc34ec92" line="1" stmtend="126" sqlhandle="0x02000000e3619d1890952212a37333bffa6d0ca92cc3684c0000000000000000000000000000000000000000">
unkNown </frame>
<frame procname="unkNown" queryhash="0x9c39d2d962cc226f" queryplanhash="0x3a3f2196dc34ec92" line="1" stmtend="126" sqlhandle="0x0200000005301203bd2a6100166b96a8b076cf61b9cf61140000000000000000000000000000000000000000">
unkNown </frame>
<frame procname="unkNown" queryhash="0x0000000000000000" queryplanhash="0x0000000000000000" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unkNown </frame>
</executionStack>
<inputbuf>
delete from adjustment_details_backup where promotion_id = 28260 </inputbuf>
</process>
<process id="process26cc0b25848" taskpriority="0" logused="0" waitresource="PAGE: 5:1:1992058 " waittime="19296" ownerId="5395124916" transactionname="implicit_transaction" lasttranstarted="2021-04-12T22:57:20.240" XDES="0x25e78a15aa0" lockMode="U" schedulerid="10" kpid="86452" status="suspended" spid="367" sbid="0" ecid="5" priority="0" trancount="0" lastbatchstarted="2021-04-12T22:57:20.240" lastbatchcompleted="2021-04-12T22:57:20.177" lastattention="1900-01-01T00:00:00.177" clientapp="Microsoft JDBC Driver for sql Server" hostname="forecast-mgmt-747b85c647-twgm5" hostpid="0" isolationlevel="read committed (2)" xactid="5395124916" currentdb="5" currentdbname="forecast-management" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128058">
<executionStack>
<frame procname="unkNown" queryhash="0x9c39d2d962cc226f" queryplanhash="0x3a3f2196dc34ec92" line="1" stmtend="126" sqlhandle="0x02000000e3619d1890952212a37333bffa6d0ca92cc3684c0000000000000000000000000000000000000000">
unkNown </frame>
<frame procname="unkNown" queryhash="0x9c39d2d962cc226f" queryplanhash="0x3a3f2196dc34ec92" line="1" stmtend="126" sqlhandle="0x0200000005301203bd2a6100166b96a8b076cf61b9cf61140000000000000000000000000000000000000000">
unkNown </frame>
<frame procname="unkNown" queryhash="0x0000000000000000" queryplanhash="0x0000000000000000" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unkNown </frame>
</executionStack>
<inputbuf>
delete from adjustment_details_backup where promotion_id = 28260 </inputbuf>
</process>
<process id="process26220db44e8" taskpriority="0" logused="0" waitresource="PAGE: 5:1:1644155 " waittime="19306" ownerId="5395124916" transactionname="implicit_transaction" lasttranstarted="2021-04-12T22:57:20.240" XDES="0x2629d0ddaa0" lockMode="U" schedulerid="11" kpid="93408" status="suspended" spid="367" sbid="0" ecid="2" priority="0" trancount="0" lastbatchstarted="2021-04-12T22:57:20.240" lastbatchcompleted="2021-04-12T22:57:20.177" lastattention="1900-01-01T00:00:00.177" clientapp="Microsoft JDBC Driver for sql Server" hostname="forecast-mgmt-747b85c647-twgm5" hostpid="0" isolationlevel="read committed (2)" xactid="5395124916" currentdb="5" currentdbname="forecast-management" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128058">
<executionStack>
<frame procname="unkNown" queryhash="0x9c39d2d962cc226f" queryplanhash="0x3a3f2196dc34ec92" line="1" stmtend="126" sqlhandle="0x02000000e3619d1890952212a37333bffa6d0ca92cc3684c0000000000000000000000000000000000000000">
unkNown </frame>
<frame procname="unkNown" queryhash="0x9c39d2d962cc226f" queryplanhash="0x3a3f2196dc34ec92" line="1" stmtend="126" sqlhandle="0x0200000005301203bd2a6100166b96a8b076cf61b9cf61140000000000000000000000000000000000000000">
unkNown </frame>
<frame procname="unkNown" queryhash="0x0000000000000000" queryplanhash="0x0000000000000000" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unkNown </frame>
</executionStack>
<inputbuf>
delete from adjustment_details_backup where promotion_id = 28260 </inputbuf>
</process>
<process id="process263301e48c8" taskpriority="0" logused="10000" waittime="391" schedulerid="8" kpid="24256" status="suspended" spid="208" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2021-04-12T22:55:00.873" lastbatchcompleted="2021-04-12T22:55:00.873" lastattention="1900-01-01T00:00:00.873" clientapp="Microsoft JDBC Driver for sql Server" hostname="forecast-mgmt-747b85c647-bck7r" hostpid="0" loginname="sfadmin" isolationlevel="read committed (2)" xactid="5394851450" currentdb="5" currentdbname="forecast-management" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128058">
<executionStack>
<frame procname="unkNown" queryhash="0x9c39d2d962cc226f" queryplanhash="0x3a3f2196dc34ec92" line="1" stmtend="126" sqlhandle="0x02000000e3619d1890952212a37333bffa6d0ca92cc3684c0000000000000000000000000000000000000000">
unkNown </frame>
<frame procname="unkNown" queryhash="0x9c39d2d962cc226f" queryplanhash="0x3a3f2196dc34ec92" line="1" stmtend="126" sqlhandle="0x02000000add9d836abdc925b7e8512a0ad07c77bad20f63f0000000000000000000000000000000000000000">
unkNown </frame>
<frame procname="unkNown" queryhash="0x0000000000000000" queryplanhash="0x0000000000000000" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unkNown </frame>
</executionStack>
<inputbuf>
delete from adjustment_details_backup where promotion_id = 28492 </inputbuf>
</process>
<process id="process2613e78b468" taskpriority="0" logused="63080" waitresource="PAGE: 5:1:1666487 " waittime="12485" ownerId="5394851450" transactionname="implicit_transaction" lasttranstarted="2021-04-12T22:55:00.873" XDES="0x26ccec03aa0" lockMode="U" schedulerid="2" kpid="82200" status="suspended" spid="208" sbid="0" ecid="4" priority="0" trancount="0" lastbatchstarted="2021-04-12T22:55:00.873" lastbatchcompleted="2021-04-12T22:55:00.873" lastattention="1900-01-01T00:00:00.873" clientapp="Microsoft JDBC Driver for sql Server" hostname="forecast-mgmt-747b85c647-bck7r" hostpid="0" isolationlevel="read committed (2)" xactid="5394851450" currentdb="5" currentdbname="forecast-management" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128058">
<executionStack>
<frame procname="unkNown" queryhash="0x9c39d2d962cc226f" queryplanhash="0x3a3f2196dc34ec92" line="1" stmtend="126" sqlhandle="0x02000000e3619d1890952212a37333bffa6d0ca92cc3684c0000000000000000000000000000000000000000">
unkNown </frame>
<frame procname="unkNown" queryhash="0x9c39d2d962cc226f" queryplanhash="0x3a3f2196dc34ec92" line="1" stmtend="126" sqlhandle="0x02000000add9d836abdc925b7e8512a0ad07c77bad20f63f0000000000000000000000000000000000000000">
unkNown </frame>
<frame procname="unkNown" queryhash="0x0000000000000000" queryplanhash="0x0000000000000000" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unkNown </frame>
</executionStack>
<inputbuf>
delete from adjustment_details_backup where promotion_id = 28492 </inputbuf>
</process>
<process id="process26cc9927468" taskpriority="0" logused="63080" waitresource="PAGE: 5:1:1992058 " waittime="12580" ownerId="5394851450" transactionname="implicit_transaction" lasttranstarted="2021-04-12T22:55:00.873" XDES="0x26ca371daa0" lockMode="U" schedulerid="9" kpid="67304" status="suspended" spid="208" sbid="0" ecid="6" priority="0" trancount="0" lastbatchstarted="2021-04-12T22:55:00.873" lastbatchcompleted="2021-04-12T22:55:00.873" lastattention="1900-01-01T00:00:00.873" clientapp="Microsoft JDBC Driver for sql Server" hostname="forecast-mgmt-747b85c647-bck7r" hostpid="0" isolationlevel="read committed (2)" xactid="5394851450" currentdb="5" currentdbname="forecast-management" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128058">
<executionStack>
<frame procname="unkNown" queryhash="0x9c39d2d962cc226f" queryplanhash="0x3a3f2196dc34ec92" line="1" stmtend="126" sqlhandle="0x02000000e3619d1890952212a37333bffa6d0ca92cc3684c0000000000000000000000000000000000000000">
unkNown </frame>
<frame procname="unkNown" queryhash="0x9c39d2d962cc226f" queryplanhash="0x3a3f2196dc34ec92" line="1" stmtend="126" sqlhandle="0x02000000add9d836abdc925b7e8512a0ad07c77bad20f63f0000000000000000000000000000000000000000">
unkNown </frame>
<frame procname="unkNown" queryhash="0x0000000000000000" queryplanhash="0x0000000000000000" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unkNown </frame>
</executionStack>
<inputbuf>
delete from adjustment_details_backup where promotion_id = 28492 </inputbuf>
</process>
<process id="process26ce204a8c8" taskpriority="0" logused="63080" waitresource="PAGE: 5:1:1643475 " waittime="12946" ownerId="5394851450" transactionname="implicit_transaction" lasttranstarted="2021-04-12T22:55:00.873" XDES="0x26cc3125aa0" lockMode="U" schedulerid="1" kpid="95516" status="suspended" spid="208" sbid="0" ecid="1" priority="0" trancount="0" lastbatchstarted="2021-04-12T22:55:00.873" lastbatchcompleted="2021-04-12T22:55:00.873" lastattention="1900-01-01T00:00:00.873" clientapp="Microsoft JDBC Driver for sql Server" hostname="forecast-mgmt-747b85c647-bck7r" hostpid="0" isolationlevel="read committed (2)" xactid="5394851450" currentdb="5" currentdbname="forecast-management" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128058">
<executionStack>
<frame procname="unkNown" queryhash="0x9c39d2d962cc226f" queryplanhash="0x3a3f2196dc34ec92" line="1" stmtend="126" sqlhandle="0x02000000e3619d1890952212a37333bffa6d0ca92cc3684c0000000000000000000000000000000000000000">
unkNown </frame>
<frame procname="unkNown" queryhash="0x9c39d2d962cc226f" queryplanhash="0x3a3f2196dc34ec92" line="1" stmtend="126" sqlhandle="0x02000000add9d836abdc925b7e8512a0ad07c77bad20f63f0000000000000000000000000000000000000000">
unkNown </frame>
<frame procname="unkNown" queryhash="0x0000000000000000" queryplanhash="0x0000000000000000" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unkNown </frame>
</executionStack>
<inputbuf>
delete from adjustment_details_backup where promotion_id = 28492 </inputbuf>
</process>
<process id="process26cc9933848" taskpriority="0" logused="63080" waitresource="PAGE: 5:1:1644155 " waittime="12856" ownerId="5394851450" transactionname="implicit_transaction" lasttranstarted="2021-04-12T22:55:00.873" XDES="0x26b35c0baa0" lockMode="U" schedulerid="7" kpid="72952" status="suspended" spid="208" sbid="0" ecid="2" priority="0" trancount="0" lastbatchstarted="2021-04-12T22:55:00.873" lastbatchcompleted="2021-04-12T22:55:00.873" lastattention="1900-01-01T00:00:00.873" clientapp="Microsoft JDBC Driver for sql Server" hostname="forecast-mgmt-747b85c647-bck7r" hostpid="0" isolationlevel="read committed (2)" xactid="5394851450" currentdb="5" currentdbname="forecast-management" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128058">
<executionStack>
<frame procname="unkNown" queryhash="0x9c39d2d962cc226f" queryplanhash="0x3a3f2196dc34ec92" line="1" stmtend="126" sqlhandle="0x02000000e3619d1890952212a37333bffa6d0ca92cc3684c0000000000000000000000000000000000000000">
unkNown </frame>
<frame procname="unkNown" queryhash="0x9c39d2d962cc226f" queryplanhash="0x3a3f2196dc34ec92" line="1" stmtend="126" sqlhandle="0x02000000add9d836abdc925b7e8512a0ad07c77bad20f63f0000000000000000000000000000000000000000">
unkNown </frame>
<frame procname="unkNown" queryhash="0x0000000000000000" queryplanhash="0x0000000000000000" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unkNown </frame>
</executionStack>
<inputbuf>
delete from adjustment_details_backup where promotion_id = 28492 </inputbuf>
</process>
</process-list>
<resource-list>
<pagelock fileid="1" pageid="1643475" dbid="5" subresource="FULL" objectname="66b54972-a069-4426-a3c9-393f898368f6.dbo.adjustment_details_backup" id="lock26a2ff11d80" mode="U" associatedobjectId="72057594139574272">
<owner-list>
<owner id="process26cc9933848" mode="U" />
</owner-list>
<waiter-list>
<waiter id="process26ce2575848" mode="U" requestType="wait" />
</waiter-list>
</pagelock>
<pagelock fileid="1" pageid="1666487" dbid="5" subresource="FULL" objectname="66b54972-a069-4426-a3c9-393f898368f6.dbo.adjustment_details_backup" id="lock26a3208a300" mode="U" associatedobjectId="72057594139574272">
<owner-list>
<owner id="process263301e48c8" mode="U" />
</owner-list>
<waiter-list>
<waiter id="process26ce2577c28" mode="U" requestType="wait" />
</waiter-list>
</pagelock>
<pagelock fileid="1" pageid="1992058" dbid="5" subresource="FULL" objectname="66b54972-a069-4426-a3c9-393f898368f6.dbo.adjustment_details_backup" id="lock266cdfce800" mode="U" associatedobjectId="72057594139574272">
<owner-list>
<owner id="process263301e48c8" mode="U" />
</owner-list>
<waiter-list>
<waiter id="process26cc0b25848" mode="U" requestType="wait" />
</waiter-list>
</pagelock>
<pagelock fileid="1" pageid="1644155" dbid="5" subresource="FULL" objectname="66b54972-a069-4426-a3c9-393f898368f6.dbo.adjustment_details_backup" id="lock26a316db580" mode="U" associatedobjectId="72057594139574272">
<owner-list>
<owner id="process2613e78b468" mode="U" />
</owner-list>
<waiter-list>
<waiter id="process26220db44e8" mode="U" requestType="wait" />
</waiter-list>
</pagelock>
<exchangeEvent id="Pipe26268814400" WaitType="e_waitPipeGetRow" waiterType="Coordinator" nodeId="6" tid="0" ownerActivity="sentData" waiteractivity="needMoreData" merging="false" spilling="false" waitingToClose="false">
<owner-list>
<owner id="process26ce204a8c8" />
<owner id="process26cc9933848" />
<owner id="process2613e78b468" />
<owner id="process26cc9927468" />
</owner-list>
<waiter-list>
<waiter id="process263301e48c8" />
</waiter-list>
</exchangeEvent>
<pagelock fileid="1" pageid="1666487" dbid="5" subresource="FULL" objectname="66b54972-a069-4426-a3c9-393f898368f6.dbo.adjustment_details_backup" id="lock26a3208a300" mode="U" associatedobjectId="72057594139574272">
<owner-list>
<owner id="process26ce2577c28" mode="U" requestType="wait" />
</owner-list>
<waiter-list>
<waiter id="process2613e78b468" mode="U" requestType="wait" />
</waiter-list>
</pagelock>
<pagelock fileid="1" pageid="1992058" dbid="5" subresource="FULL" objectname="66b54972-a069-4426-a3c9-393f898368f6.dbo.adjustment_details_backup" id="lock266cdfce800" mode="U" associatedobjectId="72057594139574272">
<owner-list>
<owner id="process26cc0b25848" mode="U" requestType="wait" />
</owner-list>
<waiter-list>
<waiter id="process26cc9927468" mode="U" requestType="wait" />
</waiter-list>
</pagelock>
<pagelock fileid="1" pageid="1643475" dbid="5" subresource="FULL" objectname="66b54972-a069-4426-a3c9-393f898368f6.dbo.adjustment_details_backup" id="lock26a2ff11d80" mode="U" associatedobjectId="72057594139574272">
<owner-list>
<owner id="process26ce2575848" mode="U" requestType="wait" />
</owner-list>
<waiter-list>
<waiter id="process26ce204a8c8" mode="U" requestType="wait" />
</waiter-list>
</pagelock>
<pagelock fileid="1" pageid="1644155" dbid="5" subresource="FULL" objectname="66b54972-a069-4426-a3c9-393f898368f6.dbo.adjustment_details_backup" id="lock26a316db580" mode="U" associatedobjectId="72057594139574272">
<owner-list>
<owner id="process26220db44e8" mode="U" requestType="wait" />
</owner-list>
<waiter-list>
<waiter id="process26cc9933848" mode="U" requestType="wait" />
</waiter-list>
</pagelock>
</resource-list>
</deadlock>
查询执行计划
死锁图的屏幕截图:
根据我的分析,死锁是由于同一页面上不同促销 ID 的多条记录。我如何防止这种僵局?
此外,由于两个查询都使用相同的过滤器参数 .v.i.z。 promotion_id
。我假设查询#1 将获得一个锁,如果查询#2 的记录包含在同一页面上,那么它将等待查询#1 的锁被释放。为什么不是这样?
如果页面锁已被一个资源获取,为什么另一个资源不等待该页面锁被释放? PageLock 是否不能有效地处理并发 DML 查询?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。