SQL Server unexpected deadlock - sql-server

I guess most deadlocks are unexpected, but even looking at the deadlock graph I still find the deadlock unexpected.
Azure SQL Server.
Table is very simple. Clustered index on primary key (Id) and no other indexes.
All updates are on a single row identified by primary key.
Multiple servers may be updating the table at the same time (usually only two, though).
Deadlock graph image:
This one deadlocks on page lock. I since added WITH (rowlock) hint, but I still get deadlocks, just on rows instead of pages. I used this example because it was the simplest graph I found.
It seems like the query is running in parallel (Exchange Event) and the different parallel processors are deadlocking each other, but I really wouldn't expect that to happen on something so simple.
Deadlock graph XML:
<deadlock>
<victim-list>
<victimProcess id="process228fd04e108" />
</victim-list>
<process-list>
<process id="process228fd04e108" taskpriority="0" logused="0" waitresource="PAGE: 5:1:2288 " waittime="9103" ownerId="2582803" transactionname="UPDATE" lasttranstarted="2020-06-12T12:53:02.807" XDES="0x22776384df0" lockMode="U" schedulerid="2" kpid="45296" status="suspended" spid="123" sbid="0" ecid="1" priority="0" trancount="0" lastbatchstarted="2020-06-12T12:53:02.807" lastbatchcompleted="2020-06-12T12:53:02.803" lastattention="1900-01-01T00:00:00.803" clientapp="Core Microsoft SqlClient Data Provider" hostname="RD501AC5B6ACC6" hostpid="16672" isolationlevel="read committed (2)" xactid="2582803" currentdb="5" currentdbname="fp-pinfo-prod-db" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="unknown" queryhash="0x16e67a1e6613ccaa" queryplanhash="0xd91db86605a474c5" line="1" stmtstart="150" stmtend="308" sqlhandle="0x02000000035f942bf8a99f27adc04a1730c088079e14a1a90000000000000000000000000000000000000000">
unknown </frame>
<frame procname="unknown" queryhash="0x0000000000000000" queryplanhash="0x0000000000000000" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
(#p0 nvarchar(4000),#p1 datetime2(7),#p2 nvarchar(4000),#p3 nvarchar(4000))UPDATE KeyValue SET Value = #p0, TimestampUtc = #p1, Origin = #p2 WHERE Id = #p3 </inputbuf>
</process>
<process id="process228fd04fc28" taskpriority="0" logused="328" waitresource="PAGE: 5:1:2288 " waittime="6999" ownerId="2582770" transactionname="UPDATE" lasttranstarted="2020-06-12T12:53:02.573" XDES="0x22908ef4960" lockMode="U" schedulerid="2" kpid="54024" status="suspended" spid="122" sbid="0" ecid="2" priority="0" trancount="0" lastbatchstarted="2020-06-12T12:53:02.573" lastbatchcompleted="2020-06-12T12:53:02.570" lastattention="1900-01-01T00:00:00.570" clientapp="Core Microsoft SqlClient Data Provider" hostname="RD501AC5B6ACC6" hostpid="14476" isolationlevel="read committed (2)" xactid="2582770" currentdb="5" currentdbname="fp-pinfo-prod-db" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="unknown" queryhash="0x16e67a1e6613ccaa" queryplanhash="0xd91db86605a474c5" line="1" stmtstart="150" stmtend="308" sqlhandle="0x02000000035f942bf8a99f27adc04a1730c088079e14a1a90000000000000000000000000000000000000000">
unknown </frame>
<frame procname="unknown" queryhash="0x0000000000000000" queryplanhash="0x0000000000000000" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
(#p0 nvarchar(4000),#p1 datetime2(7),#p2 nvarchar(4000),#p3 nvarchar(4000))UPDATE KeyValue SET Value = #p0, TimestampUtc = #p1, Origin = #p2 WHERE Id = #p3 </inputbuf>
</process>
<process id="process228f451d088" taskpriority="0" logused="10000" waittime="4139" schedulerid="2" kpid="43452" status="suspended" spid="122" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2020-06-12T12:53:02.573" lastbatchcompleted="2020-06-12T12:53:02.570" lastattention="1900-01-01T00:00:00.570" clientapp="Core Microsoft SqlClient Data Provider" hostname="RD501AC5B6ACC6" hostpid="14476" loginname="superuser" isolationlevel="read committed (2)" xactid="2582770" currentdb="5" currentdbname="fp-pinfo-prod-db" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="unknown" queryhash="0x16e67a1e6613ccaa" queryplanhash="0xd91db86605a474c5" line="1" stmtstart="150" stmtend="308" sqlhandle="0x02000000035f942bf8a99f27adc04a1730c088079e14a1a90000000000000000000000000000000000000000">
unknown </frame>
<frame procname="unknown" queryhash="0x0000000000000000" queryplanhash="0x0000000000000000" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
(#p0 nvarchar(4000),#p1 datetime2(7),#p2 nvarchar(4000),#p3 nvarchar(4000))UPDATE KeyValue SET Value = #p0, TimestampUtc = #p1, Origin = #p2 WHERE Id = #p3 </inputbuf>
</process>
</process-list>
<resource-list>
<pagelock fileid="1" pageid="2288" dbid="5" subresource="FULL" objectname="9332623d-7c4f-4da3-b054-9be315e6f0a4.dbo.KeyValue" id="lock228c3bed800" mode="U" associatedObjectId="72057594043826176">
<owner-list>
<owner id="process228f451d088" mode="U" />
</owner-list>
<waiter-list>
<waiter id="process228fd04e108" mode="U" requestType="wait" />
</waiter-list>
</pagelock>
<pagelock fileid="1" pageid="2288" dbid="5" subresource="FULL" objectname="9332623d-7c4f-4da3-b054-9be315e6f0a4.dbo.KeyValue" id="lock228c3bed800" mode="U" associatedObjectId="72057594043826176">
<owner-list>
<owner id="process228fd04e108" mode="U" requestType="wait" />
</owner-list>
<waiter-list>
<waiter id="process228fd04fc28" mode="U" requestType="wait" />
</waiter-list>
</pagelock>
<exchangeEvent id="Pipe2277614e600" WaitType="e_waitPipeGetRow" waiterType="Coordinator" nodeId="2" tid="0" ownerActivity="sentData" waiterActivity="needMoreData" merging="false" spilling="false" waitingToClose="false">
<owner-list>
<owner id="process228fd04fc28" />
</owner-list>
<waiter-list>
<waiter id="process228f451d088" />
</waiter-list>
</exchangeEvent>
</resource-list>
</deadlock>
Edit:
DDL for table:
/****** Object: Table [dbo].[KeyValue] Script Date: 15-06-2020 21:41:15 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[KeyValue](
[Id] [varchar](900) NOT NULL,
[Value] [nvarchar](max) NULL,
[TimestampUtc] [datetime2](7) NOT NULL,
[Origin] [nvarchar](max) NOT NULL,
CONSTRAINT [PK_KeyValue] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
Actual query plan for an edit:
Another deadlock graph, this one after adding rowlock hint:
Third graph:
XML for third graph:
<deadlock>
<victim-list>
<victimProcess id="process28b6db54108" />
</victim-list>
<process-list>
<process id="process28b6db54108" taskpriority="0" logused="396" waitresource="KEY: 5:72057594043826176 (36d9edc841fc)" waittime="7137" ownerId="1491320" transactionname="UPDATE" lasttranstarted="2020-06-15T09:55:29.570" XDES="0x28b698c9280" lockMode="U" schedulerid="2" kpid="34896" status="suspended" spid="113" sbid="0" ecid="3" priority="0" trancount="0" lastbatchstarted="2020-06-15T09:55:29.570" lastbatchcompleted="2020-06-15T09:55:29.563" lastattention="1900-01-01T00:00:00.563" clientapp="Core Microsoft SqlClient Data Provider" hostname="RD501AC5B6ACC6" hostpid="26324" isolationlevel="read committed (2)" xactid="1491320" currentdb="5" currentdbname="fp-pinfo-prod-db" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="unknown" queryhash="0x01068720fbb54c87" queryplanhash="0xd91db86605a474c5" line="1" stmtstart="150" stmtend="338" sqlhandle="0x02000000916f1f032e9e459183cba2f8f5cf5fabfffb31b40000000000000000000000000000000000000000">
unknown </frame>
<frame procname="unknown" queryhash="0x0000000000000000" queryplanhash="0x0000000000000000" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
(#p0 nvarchar(4000),#p1 datetime2(7),#p2 nvarchar(4000),#p3 nvarchar(4000))UPDATE KeyValue WITH (rowlock) SET Value = #p0, TimestampUtc = #p1, Origin = #p2 WHERE Id = #p3 </inputbuf>
</process>
<process id="process28b6db544e8" taskpriority="0" logused="796" waitresource="KEY: 5:72057594043826176 (f8db9e67957b)" waittime="14507" ownerId="1491488" transactionname="UPDATE" lasttranstarted="2020-06-15T09:55:31.380" XDES="0x28b698dedf0" lockMode="U" schedulerid="2" kpid="74440" status="suspended" spid="129" sbid="0" ecid="2" priority="0" trancount="0" lastbatchstarted="2020-06-15T09:55:31.380" lastbatchcompleted="2020-06-15T09:55:31.377" lastattention="1900-01-01T00:00:00.377" clientapp="Core Microsoft SqlClient Data Provider" hostname="RD501AC552A9B5" hostpid="4176" isolationlevel="read committed (2)" xactid="1491488" currentdb="5" currentdbname="fp-pinfo-prod-db" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="unknown" queryhash="0x01068720fbb54c87" queryplanhash="0xd91db86605a474c5" line="1" stmtstart="150" stmtend="338" sqlhandle="0x02000000916f1f032e9e459183cba2f8f5cf5fabfffb31b40000000000000000000000000000000000000000">
unknown </frame>
<frame procname="unknown" queryhash="0x0000000000000000" queryplanhash="0x0000000000000000" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
(#p0 nvarchar(4000),#p1 datetime2(7),#p2 nvarchar(4000),#p3 nvarchar(4000))UPDATE KeyValue WITH (rowlock) SET Value = #p0, TimestampUtc = #p1, Origin = #p2 WHERE Id = #p3 </inputbuf>
</process>
<process id="process28c66eff848" taskpriority="0" logused="796" waitresource="KEY: 5:72057594043826176 (5f732a583b1e)" waittime="7820" ownerId="1491488" transactionname="UPDATE" lasttranstarted="2020-06-15T09:55:31.380" XDES="0x28b69a2fba0" lockMode="U" schedulerid="1" kpid="67308" status="suspended" spid="129" sbid="0" ecid="1" priority="0" trancount="0" lastbatchstarted="2020-06-15T09:55:31.380" lastbatchcompleted="2020-06-15T09:55:31.377" lastattention="1900-01-01T00:00:00.377" clientapp="Core Microsoft SqlClient Data Provider" hostname="RD501AC552A9B5" hostpid="4176" isolationlevel="read committed (2)" xactid="1491488" currentdb="5" currentdbname="fp-pinfo-prod-db" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="unknown" queryhash="0x01068720fbb54c87" queryplanhash="0xd91db86605a474c5" line="1" stmtstart="150" stmtend="338" sqlhandle="0x02000000916f1f032e9e459183cba2f8f5cf5fabfffb31b40000000000000000000000000000000000000000">
unknown </frame>
<frame procname="unknown" queryhash="0x0000000000000000" queryplanhash="0x0000000000000000" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
(#p0 nvarchar(4000),#p1 datetime2(7),#p2 nvarchar(4000),#p3 nvarchar(4000))UPDATE KeyValue WITH (rowlock) SET Value = #p0, TimestampUtc = #p1, Origin = #p2 WHERE Id = #p3 </inputbuf>
</process>
<process id="process28cf7edd468" taskpriority="0" logused="10000" waittime="4232" schedulerid="1" kpid="50744" status="suspended" spid="129" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2020-06-15T09:55:31.380" lastbatchcompleted="2020-06-15T09:55:31.377" lastattention="1900-01-01T00:00:00.377" clientapp="Core Microsoft SqlClient Data Provider" hostname="RD501AC552A9B5" hostpid="4176" loginname="superuser" isolationlevel="read committed (2)" xactid="1491488" currentdb="5" currentdbname="fp-pinfo-prod-db" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="unknown" queryhash="0x01068720fbb54c87" queryplanhash="0xd91db86605a474c5" line="1" stmtstart="150" stmtend="338" sqlhandle="0x02000000916f1f032e9e459183cba2f8f5cf5fabfffb31b40000000000000000000000000000000000000000">
unknown </frame>
<frame procname="unknown" queryhash="0x0000000000000000" queryplanhash="0x0000000000000000" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
(#p0 nvarchar(4000),#p1 datetime2(7),#p2 nvarchar(4000),#p3 nvarchar(4000))UPDATE KeyValue WITH (rowlock) SET Value = #p0, TimestampUtc = #p1, Origin = #p2 WHERE Id = #p3 </inputbuf>
</process>
<process id="process28cf7edc8c8" taskpriority="0" logused="10000" waittime="4864" schedulerid="1" kpid="59208" status="suspended" spid="113" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2020-06-15T09:55:29.570" lastbatchcompleted="2020-06-15T09:55:29.563" lastattention="1900-01-01T00:00:00.563" clientapp="Core Microsoft SqlClient Data Provider" hostname="RD501AC5B6ACC6" hostpid="26324" loginname="superuser" isolationlevel="read committed (2)" xactid="1491320" currentdb="5" currentdbname="fp-pinfo-prod-db" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="unknown" queryhash="0x01068720fbb54c87" queryplanhash="0xd91db86605a474c5" line="1" stmtstart="150" stmtend="338" sqlhandle="0x02000000916f1f032e9e459183cba2f8f5cf5fabfffb31b40000000000000000000000000000000000000000">
unknown </frame>
<frame procname="unknown" queryhash="0x0000000000000000" queryplanhash="0x0000000000000000" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
(#p0 nvarchar(4000),#p1 datetime2(7),#p2 nvarchar(4000),#p3 nvarchar(4000))UPDATE KeyValue WITH (rowlock) SET Value = #p0, TimestampUtc = #p1, Origin = #p2 WHERE Id = #p3 </inputbuf>
</process>
</process-list>
<resource-list>
<keylock hobtid="72057594043826176" dbid="5" objectname="050344b0-8326-468d-9337-48fedc86da64.dbo.KeyValue" indexname="PK_KeyValue" id="lock28c65e13800" mode="U" associatedObjectId="72057594043826176">
<owner-list>
<owner id="process28cf7edd468" mode="U" />
</owner-list>
<waiter-list>
<waiter id="process28b6db54108" mode="U" requestType="wait" />
</waiter-list>
</keylock>
<keylock hobtid="72057594043826176" dbid="5" objectname="050344b0-8326-468d-9337-48fedc86da64.dbo.KeyValue" indexname="PK_KeyValue" id="lock28c68183800" mode="X" associatedObjectId="72057594043826176">
<owner-list>
<owner id="process28cf7edc8c8" mode="X" />
<owner id="process28cf7edc8c8" mode="U" />
</owner-list>
<waiter-list>
<waiter id="process28b6db544e8" mode="U" requestType="wait" />
</waiter-list>
</keylock>
<keylock hobtid="72057594043826176" dbid="5" objectname="050344b0-8326-468d-9337-48fedc86da64.dbo.KeyValue" indexname="PK_KeyValue" id="lock28c683a5700" mode="U" associatedObjectId="72057594043826176">
<owner-list>
<owner id="process28cf7edc8c8" mode="U" />
</owner-list>
<waiter-list>
<waiter id="process28c66eff848" mode="U" requestType="wait" />
</waiter-list>
</keylock>
<exchangeEvent id="Pipe28b69a26680" WaitType="e_waitPipeGetRow" waiterType="Coordinator" nodeId="2" tid="0" ownerActivity="sentData" waiterActivity="needMoreData" merging="false" spilling="false" waitingToClose="false">
<owner-list>
<owner id="process28b6db544e8" />
<owner id="process28c66eff848" />
</owner-list>
<waiter-list>
<waiter id="process28cf7edd468" />
</waiter-list>
</exchangeEvent>
<exchangeEvent id="Pipe28b699e0680" WaitType="e_waitPipeGetRow" waiterType="Coordinator" nodeId="2" tid="0" ownerActivity="sentData" waiterActivity="needMoreData" merging="false" spilling="false" waitingToClose="false">
<owner-list>
<owner id="process28b6db54108" />
</owner-list>
<waiter-list>
<waiter id="process28cf7edc8c8" />
</waiter-list>
</exchangeEvent>
</resource-list>
</deadlock>

Found it. Simple mistake. Your PK is varchar(900). Your UPDATE sends a parameter of type nvarchar(4000). nvarchar has a higher data type precedence than varchar, so varchar values must be converted to nvarchar for comparison. So what should be a trivial clustered index seek becomes a scan. And it's the extra cost of the scan that's causing parallel query plans, and the possibility of a deadlock.
To fix this simply use the correct parameter type in .NET or force a conversion in your query. EG
UPDATE KeyValue SET Value = #p0, TimestampUtc = #p1, Origin = #p2
WHERE Id = cast(#p3 as varchar(900))
This will eliminate the parallel plans, the deadlocks, and make this query much, much cheaper.

Related

Does after insert trigger fire on update?

I am trying to understand what is causing this deadlock.
The update command in the first process is the result of the trigger from the insert command. So I am trying to understand if the insert trigger fire on update?
<deadlock>
<victim-list>
<victimProcess id="process1fb169468" />
</victim-list>
<process-list>
<process id="process1fb169468" taskpriority="0" logused="2044" waitresource="KEY: 5:72057598307401728 (c73677424644)" waittime="61674" ownerId="34455794" transactionname="user_transaction" lasttranstarted="2022-07-11T23:24:01.387" XDES="0x56bee18e0" lockMode="U" schedulerid="6" kpid="9800" status="suspended" spid="94" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2022-07-11T23:24:01.387" lastbatchcompleted="2022-07-11T23:24:01.387" lastattention="2022-07-11T22:02:36.350" clientapp="Vizion WMS" hostname="AGROXAWORK02" hostpid="10484" loginname="vizion" isolationlevel="read committed (2)" xactid="34455794" currentdb="5" currentdbname="WMSData" lockTimeout="4294967295" clientoption1="671219744" clientoption2="128056">
<executionStack>
<frame procname="WMSData.dbo.UpdateDes" line="5" stmtstart="154" stmtend="298" sqlhandle="0x030005000caa6e4b5b56dd0089a6000000000000000000000000000000000000000000000000000000000000">
update inventory set designation = 'FROZEN' where designation <> 'FROZEN </frame>
<frame procname="WMSData.dbo.PostReceipt" line="101" stmtstart="7960" stmtend="10026" sqlhandle="0x0300050071b68341909a680037ae000001000000000000000000000000000000000000000000000000000000">
INSERT INTO [wmsdata].[dbo].[Inventory]
([Customer],[Product],[Row],[Rack],[Slot],[Pallets],[Cases],[Net],[ProductDate],[ReceiverNumber],[LotNumber],[ReceiveDate],[StorThru],[Status],[PalletNumber],[CustomerPallet],[StartTime],[StopTime],[BornOnDate],[EnteredBy],[ImportOrManual],[FullPalletQty],[PalletQtyReceived],[CustomerReference],lpnumber,fcgpallet,designation,mark,invoptional1,invoptional2,invoptional3,invoptional4,f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,rsrate)
select [Customer],[Product],[Row],[Rack],[Slot],[Pallets],[Cases],[Net],[ProductDate],[Receiver],[LotNumber],convert(char(12),getdate(),101) as [ReceiveDate],[StorThru],[Status],[PalletNumber],[CustomerPallet],[StartTime],[StopTime],convert(char(12),getdate(),101) as [BornOnDate],[EnteredBy],[ImportOrManual],[FullPalletQty],cases as [PalletQtyReceived], #r, lpnumber, fcgpallet,designation,mark,invoptional1,invoptional2,invoptional3,invoptional4,f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,rsrate
from [wmsdata].[dbo].receipts with (nolock) where palletnum </frame>
<frame procname="adhoc" line="1" sqlhandle="0x0100050098f50238607f6f0b0500000000000000000000000000000000000000000000000000000000000000">
exec dbo.postreceipt '149496','RDAY' </frame>
</executionStack>
<inputbuf>
exec dbo.postreceipt '149496','RDAY' </inputbuf>
</process>
<process id="process46811bc28" taskpriority="0" logused="22480" waitresource="KEY: 5:72057598307401728 (771c8a402149)" waittime="4364" ownerId="34439550" transactionname="user_transaction" lasttranstarted="2022-07-11T23:22:34.483" XDES="0x5a043d770" lockMode="U" schedulerid="1" kpid="10752" status="suspended" spid="59" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2022-07-11T23:22:34.497" lastbatchcompleted="2022-07-11T23:22:33.330" lastattention="2022-07-11T23:17:04.210" clientapp="Vizion WMS" hostname="AGROXAWORK02" hostpid="12144" loginname="vizion" isolationlevel="read committed (2)" xactid="34439550" currentdb="5" currentdbname="WMSData" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="WMSData.dbo.PostShipment" line="161" stmtstart="16962" stmtend="17036" sqlhandle="0x0300050069a66001dd5e640021ac000001000000000000000000000000000000000000000000000000000000">
delete from inventory where cases <= </frame>
<frame procname="adhoc" line="1" sqlhandle="0x01000500f2278100b05d31790100000000000000000000000000000000000000000000000000000000000000">
exec Postshipment '257208', 'JJOHNSON' </frame>
</executionStack>
<inputbuf>
exec Postshipment '257208', 'JJOHNSON' </inputbuf>
</process>
</process-list>
<resource-list>
<keylock hobtid="72057598307401728" dbid="5" objectname="WMSData.dbo.Inventory" indexname="PK_Inventory" id="lock4e671e880" mode="X" associatedObjectId="72057598307401728">
<owner-list>
<owner id="process46811bc28" mode="X" />
</owner-list>
<waiter-list>
<waiter id="process1fb169468" mode="U" requestType="wait" />
</waiter-list>
</keylock>
<keylock hobtid="72057598307401728" dbid="5" objectname="WMSData.dbo.Inventory" indexname="PK_Inventory" id="lock52ec3d280" mode="X" associatedObjectId="72057598307401728">
<owner-list>
<owner id="process1fb169468" mode="X" />
</owner-list>
<waiter-list>
<waiter id="process46811bc28" mode="U" requestType="wait" />
</waiter-list>
</keylock>
</resource-list>
</deadlock>

How to solve deadlock detected by sql server?

I'm facing a deadlock problem, the sql server detects it and return an exception because it chose a process as a victim. I would like to avoid this problem, I tried to change the isolation level to read uncommitted and also add indexes but this did not resolve the problems, here is report that describe the deadlock problem:
<deadlock>
<victim-list>
<victimProcess id="process133b1e188" />
</victim-list>
<process-list>
<process id="process133b1e188" taskpriority="0" logused="5236" waitresource="RID: 18:1:3545:209" waittime="1939" ownerId="13564317" transactionname="implicit_transaction" lasttranstarted="2020-12-18T18:15:14.250" XDES="0x1a064a3a8" lockMode="U" schedulerid="2" kpid="23832" status="suspended" spid="71" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2020-12-18T18:15:14.280" lastbatchcompleted="2020-12-18T18:15:14.280" lastattention="1900-01-01T00:00:00.280" clientapp="jTDS" hostname="med" hostpid="123" loginname="med" isolationlevel="read uncommitted (1)" xactid="13564317" currentdb="18" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128058">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="462" stmtend="592" sqlhandle="0x030012003d326d4a3bf62b0195ac000001000000000000000000000000000000000000000000000000000000"> UPDATE DEP SET name WHERE id = 2 </frame>
</executionStack>
</process>
<process id="process133b1e558" taskpriority="0" logused="30956" waitresource="KEY: 18:72057766865403904 (8c1a6a11df48)" waittime="1957" ownerId="13563835" transactionname="implicit_transaction" lasttranstarted="2020-12-18T18:15:14.113" XDES="0x1a064b668" lockMode="S" schedulerid="2" kpid="32596" status="suspended" spid="68" sbid="0" ecid="0" priority="0" trancount="1" lastbatchstarted="2020-12-18T18:15:14.263" lastbatchcompleted="2020-12-18T18:15:14.263" lastattention="1900-01-01T00:00:00.263" clientapp="jTDS" hostname="med" hostpid="123" loginname="med" isolationlevel="read committed (2)" xactid="13563835" currentdb="18" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128058">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="24" sqlhandle="0x0200000064dbc4257a329ecd9ef9f0056fe48ce548b17d2e0000000000000000000000000000000000000000">
SELECT * FROM Worker w WHERE (w.id = #P0 ) </frame>
</executionStack>
</process>
</process-list>
<resource-list>
<ridlock fileid="1" pageid="3545" dbid="18" objectname="DEP" id="lock185869680" mode="X" associatedObjectId="72057766864551936">
<owner-list>
<owner id="process133b1e558" mode="X" />
</owner-list>
<waiter-list>
<waiter id="process133b1e188" mode="U" requestType="wait" />
</waiter-list>
</ridlock>
<keylock hobtid="72057766865403904" dbid="18" objectname="Worker" indexname="worker_id" id="lock17d9fdb80" mode="X" associatedObjectId="72057766865403904">
<owner-list>
<owner id="process133b1e188" mode="X" />
</owner-list>
<waiter-list>
<waiter id="process133b1e558" mode="S" requestType="wait" />
</waiter-list>
</keylock>
</resource-list>
</deadlock>
Try reading about:
set deadlock priority high
You should use high or 10 for the session you don't want to be the victim.
And I would say isolation level read uncommitted is not a good choice.

SQL Server: Transaction deadlock on SQL with PK in WHERE clause

We have a Java application that uses a table AU_PROCESSQUEUE for following operations:
INSERT new job
UPDATE job status to READY | RUNNING
UPDATE associated workflow information for that job
DELETE the job once it is completed
Table DDL
CREATE TABLE AU_PROCESSQUEUE (
AU_JOB_ID varchar(80) NOT NULL ,
AU_PROCESSFLOW_ID varchar (50) NULL ,
AU_PROCESSFLOW_NAME varchar (64) NULL ,
AU_STATUS varchar (30) NULL ,
AU_SUBJECT varbinary (Max) NULL ,
AU_PROCESSFLOW_OBJECTADDRESS varchar (350) NULL ,
AU_JOB_METADATA varbinary(Max) NULL ,
AU_QUEUE_TIME datetime NULL ,
AU_START_TIME datetime NULL ,
AU_END_TIME datetime NULL ,
AU_INSTANCE_ID varchar (100) NULL ,
AU_PRIORITY numeric(18, 2) NULL ,
AU_NODE_NAME varchar (50) NULL ,
AU_MESSAGE varchar (2000) NULL ,
CONSTRAINT PK_AU_PROCESSQUEUE PRIMARY KEY(AU_JOB_ID)) ;
INDEXES
CREATE NONCLUSTERED INDEX [IX_AU_PROCESSQUEUE_AU_STATUS_AU_NODE_NAME]
ON [AdeptiaBackend].[dbo].[AU_PROCESSQUEUE] (
[AU_STATUS] ASC,
[AU_NODE_NAME] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = ON,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
CREATE NONCLUSTERED INDEX [IX_AU_PROCESSQUEUE_AUQUEUETIME]
ON [AdeptiaBackend].[dbo].[AU_PROCESSQUEUE] (
[AU_QUEUE_TIME] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = ON,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
There is a SELECT query that keeps on executing on this table periodically to load the QUEUED jobs ordered by queued time
Select * from AU_PROCESSQUEUE (NOLOCK)
WHERE (AU_STATUS = 'Queued' OR AU_STATUS = 'Ready To Resume')
and
( AU_NODE_NAME='localhost' OR AU_NODE_NAME IS NULL )
ORDER BY AU_QUEUE_TIME LIMIT 200
The Java application only process a fixed number of jobs at a time (defined as job concurrency say 60), so essentially, there are 60 different threads where following queries are being executed in parallel:
Update AU_PROCESSQUEUE
SET AU_NODE_NAME=#P0 ,
AU_INSTANCE_ID=#P1,
AU_PROCESSFLOW_OBJECTADDRESS=#P2,
AU_STATUS=#P3,
AU_START_TIME=#P4
WHERE AU_JOB_ID=#P5
DELETE from AU_PROCESSQUEUE WHERE AU_JOB_ID=#P0
Issue
Under high job concurrency, say 60, we are seeing transactions being deadlocked in SQL Server. Here is the deadlock trace:
<deadlock-list>
<deadlock victim="process48d914ca8">
<process-list>
<process id="process4cdda84e8" taskpriority="0" logused="0" waitresource="KEY: 18:72057594055098368 (666003c8ddef)" waittime="34201" ownerId="1603282" transactionname="DELETE" lasttranstarted="2018-01-09T10:12:31.663" XDES="0x4cdff7770" lockMode="U" schedulerid="1" kpid="3892" status="suspended" spid="122" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2018-01-09T10:12:31.663" lastbatchcompleted="2018-01-09T10:12:31.663" lastattention="1900-01-01T00:00:00.663" clientapp="Microsoft JDBC Driver for SQL Server" hostname="WIN-UOEQMBQP08C" hostpid="0" loginname="sa" isolationlevel="read committed (2)" xactid="1603282" currentdb="18" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="40" stmtend="132" sqlhandle="0x0200000063980b37a86688199d73e7920178af18c95d90030000000000000000000000000000000000000000">
unknown </frame>
<frame procname="unknown" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
(#P0 nvarchar(4000))DELETE from AU_PROCESSQUEUE WHERE AU_JOB_ID=#P0 </inputbuf>
</process>
<process id="process48d914ca8" taskpriority="0" logused="0" waitresource="KEY: 18:72057594055098368 (666003c8ddef)" waittime="35028" ownerId="1603137" transactionname="DELETE" lasttranstarted="2018-01-09T10:12:31.107" XDES="0x488c743b0" lockMode="U" schedulerid="4" kpid="2232" status="suspended" spid="286" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2018-01-09T10:12:31.107" lastbatchcompleted="2018-01-09T10:12:31.107" lastattention="1900-01-01T00:00:00.107" clientapp="Microsoft JDBC Driver for SQL Server" hostname="WIN-UOEQMBQP08C" hostpid="0" loginname="sa" isolationlevel="read committed (2)" xactid="1603137" currentdb="18" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="40" stmtend="132" sqlhandle="0x0200000063980b37a86688199d73e7920178af18c95d90030000000000000000000000000000000000000000">
unknown </frame>
<frame procname="unknown" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
(#P0 nvarchar(4000))DELETE from AU_PROCESSQUEUE WHERE AU_JOB_ID=#P0 </inputbuf>
</process>
<process id="process4c809dc28" taskpriority="0" logused="0" waitresource="PAGE: 18:1:3620 " waittime="33435" ownerId="1609771" transactionname="DELETE" lasttranstarted="2018-01-09T10:12:57.623" XDES="0x48d905770" lockMode="IU" schedulerid="4" kpid="1712" status="suspended" spid="289" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2018-01-09T10:12:57.623" lastbatchcompleted="2018-01-09T10:12:57.620" lastattention="1900-01-01T00:00:00.620" clientapp="Microsoft JDBC Driver for SQL Server" hostname="WIN-UOEQMBQP08C" hostpid="0" loginname="sa" isolationlevel="read committed (2)" xactid="1609771" currentdb="18" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="40" stmtend="132" sqlhandle="0x0200000063980b37a86688199d73e7920178af18c95d90030000000000000000000000000000000000000000">
unknown </frame>
<frame procname="unknown" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
(#P0 nvarchar(4000))DELETE from AU_PROCESSQUEUE WHERE AU_JOB_ID=#P0 </inputbuf>
</process>
<process id="process4c7853468" taskpriority="0" logused="0" waitresource="PAGE: 18:1:4688 " waittime="33460" ownerId="1609641" transactionname="UPDATE" lasttranstarted="2018-01-09T10:12:57.237" XDES="0x4c77783b0" lockMode="U" schedulerid="2" kpid="5660" status="suspended" spid="326" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2018-01-09T10:12:57.237" lastbatchcompleted="2018-01-09T10:10:20.090" lastattention="1900-01-01T00:00:00.090" clientapp="Microsoft JDBC Driver for SQL Server" hostname="WIN-UOEQMBQP08C" hostpid="0" loginname="sa" isolationlevel="read committed (2)" xactid="1609641" currentdb="18" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="220" stmtend="522" sqlhandle="0x0200000086a2ea1594021304e59bc20f346ab6be8a632b340000000000000000000000000000000000000000">
unknown </frame>
<frame procname="unknown" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
(#P0 nvarchar(4000),#P1 nvarchar(4000),#P2 nvarchar(4000),#P3 nvarchar(4000),#P4 datetime2,#P5 nvarchar(4000))Update AU_PROCESSQUEUE SET AU_NODE_NAME=#P0 , AU_INSTANCE_ID=#P1, AU_PROCESSFLOW_OBJECTADDRESS=#P2, AU_STATUS=#P3, AU_START_TIME=#P4 WHERE AU_JOB_ID=#P5 </inputbuf>
</process>
<process id="process4bd316ca8" taskpriority="0" logused="0" waitresource="KEY: 18:72057594055098368 (666003c8ddef)" waittime="35028" ownerId="1609113" transactionname="DELETE" lasttranstarted="2018-01-09T10:12:55.737" XDES="0x48d66a3b0" lockMode="U" schedulerid="4" kpid="6312" status="suspended" spid="336" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2018-01-09T10:12:55.733" lastbatchcompleted="2018-01-09T10:12:55.733" lastattention="1900-01-01T00:00:00.733" clientapp="Microsoft JDBC Driver for SQL Server" hostname="WIN-7EFMLLQ3PKJ" hostpid="0" loginname="sa" isolationlevel="read committed (2)" xactid="1609113" currentdb="18" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="40" stmtend="132" sqlhandle="0x0200000063980b37a86688199d73e7920178af18c95d90030000000000000000000000000000000000000000">
unknown </frame>
<frame procname="unknown" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
(#P0 nvarchar(4000))DELETE from AU_PROCESSQUEUE WHERE AU_JOB_ID=#P0 </inputbuf>
</process>
<process id="process4bd316108" taskpriority="0" logused="0" waitresource="KEY: 18:72057594055098368 (666003c8ddef)" waittime="35028" ownerId="1607808" transactionname="DELETE" lasttranstarted="2018-01-09T10:12:51.910" XDES="0x4d1e4a6d0" lockMode="U" schedulerid="4" kpid="9144" status="suspended" spid="266" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2018-01-09T10:12:51.910" lastbatchcompleted="2018-01-09T10:12:51.910" lastattention="1900-01-01T00:00:00.910" clientapp="Microsoft JDBC Driver for SQL Server" hostname="WIN-UOEQMBQP08C" hostpid="0" loginname="sa" isolationlevel="read committed (2)" xactid="1607808" currentdb="18" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="40" stmtend="132" sqlhandle="0x0200000063980b37a86688199d73e7920178af18c95d90030000000000000000000000000000000000000000">
unknown </frame>
<frame procname="unknown" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
(#P0 nvarchar(4000))DELETE from AU_PROCESSQUEUE WHERE AU_JOB_ID=#P0 </inputbuf>
</process>
<process id="process4bfc37468" taskpriority="0" logused="0" waitresource="KEY: 18:72057594055098368 (666003c8ddef)" waittime="35029" ownerId="1604201" transactionname="DELETE" lasttranstarted="2018-01-09T10:12:36.360" XDES="0x4b89623b0" lockMode="U" schedulerid="4" kpid="2704" status="suspended" spid="535" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2018-01-09T10:12:36.360" lastbatchcompleted="2018-01-09T10:12:36.360" lastattention="1900-01-01T00:00:00.360" clientapp="Microsoft JDBC Driver for SQL Server" hostname="WIN-7EFMLLQ3PKJ" hostpid="0" loginname="sa" isolationlevel="read committed (2)" xactid="1604201" currentdb="18" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="40" stmtend="132" sqlhandle="0x0200000063980b37a86688199d73e7920178af18c95d90030000000000000000000000000000000000000000">
unknown </frame>
<frame procname="unknown" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
(#P0 nvarchar(4000))DELETE from AU_PROCESSQUEUE WHERE AU_JOB_ID=#P0 </inputbuf>
</process>
<process id="process4bd317468" taskpriority="0" logused="0" waitresource="KEY: 18:72057594055098368 (666003c8ddef)" waittime="35029" ownerId="1603983" transactionname="DELETE" lasttranstarted="2018-01-09T10:12:35.400" XDES="0x47ff6c540" lockMode="U" schedulerid="4" kpid="3508" status="suspended" spid="160" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2018-01-09T10:12:35.400" lastbatchcompleted="2018-01-09T10:12:35.400" lastattention="1900-01-01T00:00:00.400" clientapp="Microsoft JDBC Driver for SQL Server" hostname="WIN-UOEQMBQP08C" hostpid="0" loginname="sa" isolationlevel="read committed (2)" xactid="1603983" currentdb="18" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="40" stmtend="132" sqlhandle="0x0200000063980b37a86688199d73e7920178af18c95d90030000000000000000000000000000000000000000">
unknown </frame>
<frame procname="unknown" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
(#P0 nvarchar(4000))DELETE from AU_PROCESSQUEUE WHERE AU_JOB_ID=#P0 </inputbuf>
</process>
<process id="process4bd3168c8" taskpriority="0" logused="0" waitresource="PAGE: 18:1:3620 " waittime="8339" ownerId="1598759" transactionname="DELETE" lasttranstarted="2018-01-09T10:12:16.410" XDES="0x491786d90" lockMode="IU" schedulerid="4" kpid="6692" status="suspended" spid="350" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2018-01-09T10:12:16.410" lastbatchcompleted="2018-01-09T10:12:16.407" lastattention="1900-01-01T00:00:00.407" clientapp="Microsoft JDBC Driver for SQL Server" hostname="WIN-7EFMLLQ3PKJ" hostpid="0" loginname="sa" isolationlevel="read committed (2)" xactid="1598759" currentdb="18" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="40" stmtend="132" sqlhandle="0x0200000063980b37a86688199d73e7920178af18c95d90030000000000000000000000000000000000000000">
unknown </frame>
<frame procname="unknown" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
(#P0 nvarchar(4000))DELETE from AU_PROCESSQUEUE WHERE AU_JOB_ID=#P0 </inputbuf>
</process>
</process-list>
<resource-list>
<keylock hobtid="72057594055098368" dbid="18" objectname="topco_cluster_bk.dbo.AU_PROCESSQUEUE" indexname="IX_AU_PROCESSQUEUE_AUQUEUETIME" id="lock4cfec4f80" mode="U" associatedObjectId="72057594055098368">
<owner-list>
<owner id="process48d914ca8" mode="U" requestType="wait"/>
</owner-list>
<waiter-list>
<waiter id="process4cdda84e8" mode="U" requestType="wait"/>
</waiter-list>
</keylock>
<keylock hobtid="72057594055098368" dbid="18" objectname="topco_cluster_bk.dbo.AU_PROCESSQUEUE" indexname="IX_AU_PROCESSQUEUE_AUQUEUETIME" id="lock4cfec4f80" mode="U" associatedObjectId="72057594055098368">
<owner-list>
<owner id="process4bd3168c8" mode="U"/>
</owner-list>
<waiter-list>
<waiter id="process48d914ca8" mode="U" requestType="wait"/>
</waiter-list>
</keylock>
<pagelock fileid="1" pageid="3620" dbid="18" subresource="FULL" objectname="topco_cluster_bk.dbo.AU_PROCESSQUEUE" id="lock4af764580" mode="U" associatedObjectId="72057594055098368">
<owner-list>
<owner id="process4c7853468" mode="U"/>
</owner-list>
<waiter-list>
<waiter id="process4c809dc28" mode="IU" requestType="wait"/>
</waiter-list>
</pagelock>
<pagelock fileid="1" pageid="4688" dbid="18" subresource="FULL" objectname="topco_cluster_bk.dbo.AU_PROCESSQUEUE" id="lock4cfe98600" mode="IU" associatedObjectId="72057594055098368">
<owner-list>
<owner id="process4cdda84e8" mode="IU"/>
<owner id="process4bfc37468" mode="IU"/>
<owner id="process4bd317468" mode="IU"/>
<owner id="process48d914ca8" mode="IU"/>
</owner-list>
<waiter-list>
<waiter id="process4c7853468" mode="U" requestType="wait"/>
</waiter-list>
</pagelock>
<keylock hobtid="72057594055098368" dbid="18" objectname="topco_cluster_bk.dbo.AU_PROCESSQUEUE" indexname="IX_AU_PROCESSQUEUE_AUQUEUETIME" id="lock4cfec4f80" mode="U" associatedObjectId="72057594055098368">
<owner-list>
<owner id="process48d914ca8" mode="U" requestType="wait"/>
</owner-list>
<waiter-list>
<waiter id="process4bd316ca8" mode="U" requestType="wait"/>
</waiter-list>
</keylock>
<keylock hobtid="72057594055098368" dbid="18" objectname="topco_cluster_bk.dbo.AU_PROCESSQUEUE" indexname="IX_AU_PROCESSQUEUE_AUQUEUETIME" id="lock4cfec4f80" mode="U" associatedObjectId="72057594055098368">
<owner-list>
<owner id="process48d914ca8" mode="U" requestType="wait"/>
</owner-list>
<waiter-list>
<waiter id="process4bd316108" mode="U" requestType="wait"/>
</waiter-list>
</keylock>
<keylock hobtid="72057594055098368" dbid="18" objectname="topco_cluster_bk.dbo.AU_PROCESSQUEUE" indexname="IX_AU_PROCESSQUEUE_AUQUEUETIME" id="lock4cfec4f80" mode="U" associatedObjectId="72057594055098368">
<owner-list>
<owner id="process48d914ca8" mode="U" requestType="wait"/>
</owner-list>
<waiter-list>
<waiter id="process4bfc37468" mode="U" requestType="wait"/>
</waiter-list>
</keylock>
<keylock hobtid="72057594055098368" dbid="18" objectname="topco_cluster_bk.dbo.AU_PROCESSQUEUE" indexname="IX_AU_PROCESSQUEUE_AUQUEUETIME" id="lock4cfec4f80" mode="U" associatedObjectId="72057594055098368">
<owner-list>
<owner id="process48d914ca8" mode="U" requestType="wait"/>
</owner-list>
<waiter-list>
<waiter id="process4bd317468" mode="U" requestType="wait"/>
</waiter-list>
</keylock>
<pagelock fileid="1" pageid="3620" dbid="18" subresource="FULL" objectname="topco_cluster_bk.dbo.AU_PROCESSQUEUE" id="lock4af764580" mode="U" associatedObjectId="72057594055098368">
<owner-list>
<owner id="process4c809dc28" mode="IU" requestType="wait"/>
</owner-list>
<waiter-list>
<waiter id="process4bd3168c8" mode="IU" requestType="wait"/>
</waiter-list>
</pagelock>
</resource-list>
</deadlock>
</deadlock-list>
Questions
Why am I seeing a deadlock when both the UPDATE and DELETE query is being executed with PK in the WHERE clause? Isn't updating and deleting on PK considered the best scenario?
Will it help if I change my PK (column AU_JOB_ID) to INT (or BIGINT) instead of VARCHAR?
Will it help if I change status column (AU_STATUS) to refer lookup codes - that will change the column from VARCHAR to INT and help in faster comparison and search?
I am not an expert on database and SQL Server, so any help shall be appreciated. Thanks in advance!
Stepping back, it's going to be tough to tweak and troubleshoot your way through this problem, even if you were "an expert on database and SQL Server". The basic problem the three different data structures that need to be updated whenever you update or delete: the clustered index and two non-clustered indexes.
When using a table as a queue, you either need to keep the table structure extremely simplistic, or use Service Broker Queues, which is SQL Server's native in-database queuing feature.
So do what you would do in Java, synchronize access to your shared data. In SQL Server you use a transaction and an Application Lock to ensure that a session has brief, exclusive access to run a bit of code.
You seem to be focused on where the locks are occurring rather that what is causing them.
If the select was not taking locks then the update and delete should not have a problem.

Deadlock in SQL SERVER 2014

I am experiencing sporadic deadlock behaviour when my application tries to execute any DROP TABLE statement against my database.
I tried capturing the deadlock graph and this is the output from deadlock xml
<deadlock>
<victim-list>
<victimProcess id="process2bddc28" />
</victim-list>
<process-list>
<process id="process2bddc28" taskpriority="0" logused="0" waitresource="KEY: 6:281474978938880 (85262e079cd2)" waittime="1900" ownerId="76493296" transactionname="user_transaction" lasttranstarted="2016-02-22T05:58:15.867" XDES="0x621cb38e0" lockMode="S" schedulerid="6" kpid="10748" status="suspended" spid="119" sbid="0" ecid="0" priority="0" trancount="1" lastbatchstarted="2016-02-22T05:58:16.080" lastbatchcompleted="2016-02-22T05:58:16.080" lastattention="1900-01-01T00:00:00.080" hostname="DEV01" hostpid="13420" isolationlevel="read committed (2)" xactid="76493296" currentdb="6" lockTimeout="4294967295" clientoption1="671090784" clientoption2="128056">
<executionStack>
<frame procname="mssqlsystemresource.sys.sp_columns_100" line="138" stmtstart="12020" stmtend="17532" sqlhandle="0x0300ff7f78a964e13656e600e7a5000001000000000000000000000000000000000000000000000000000000">
</frame>
</executionStack>
<inputbuf>Proc [Database Id = 32767 Object Id = -513496712]</inputbuf>
</process>
<process id="process45c8ca8" taskpriority="0" logused="6748" waitresource="KEY: 6:562949956108288 (5db581fe7a0b)" waittime="1900" ownerId="76493663" transactionname="user_transaction" lasttranstarted="2016-02-22T05:58:16.087" XDES="0x16aeded90" lockMode="X" schedulerid="1" kpid="18268" status="suspended" spid="129" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2016-02-22T05:58:16.090" lastbatchcompleted="2016-02-22T05:58:16.090" lastattention="1900-01-01T00:00:00.090" hostname="DEV01" hostpid="20128" isolationlevel="read committed (2)" xactid="76493663" currentdb="6" lockTimeout="4294967295" clientoption1="671090784" clientoption2="128056">
<executionStack>
<frame procname="adhoc" line="1" sqlhandle="0x01000600702bfd0bd083047f0600000000000000000000000000000000000000000000000000000000000000">
DROP TABLE "dbo"."USER_TABLE_1" </frame>
</executionStack>
<inputbuf>
DROP TABLE "dbo"."USER_TABLE_1" </inputbuf>
</process>
</process-list>
<resource-list>
<keylock hobtid="281474978938880" dbid="6" objectname="DEV_01.sys.sysschobjs" indexname="clst" id="lock35af27b80" mode="X" associatedObjectId="281474978938880">
<owner-list><owner id="process45c8ca8" mode="X" /></owner-list>
<waiter-list><waiter id="process2bddc28" mode="S" requestType="wait" /></waiter-list>
</keylock>
<keylock hobtid="562949956108288" dbid="6" objectname="DEV_01.sys.syscolpars" indexname="nc" id="lock453c34600" mode="U" associatedObjectId="562949956108288">
<owner-list><owner id="process2bddc28" mode="S" /></owner-list>
<waiter-list><waiter id="process45c8ca8" mode="X" requestType="convert" /></waiter-list>
</keylock>
</resource-list>
</deadlock>
This behaviour was not seen in earlier versions of SQL Server. Any thoughts on this?

SQL Server deadlock - with S and IX lock mode

Recently we encountered deadlock issues when more users using the system, below is the log details, appreciate if someone can provide guides on how to resolve this:
How S or IX lock mode causing the deadlock?
I am thinking probably to improve the query time for the select statement, so that it could release the lock sooner.
<TextData>
<deadlock-list>
<deadlock victim="process6956d8">
<process-list>
<process id="process6956d8" taskpriority="0" logused="0" waitresource="PAGE: 8:1:6923" waittime="1840" ownerId="67336413" transactionname="implicit_transaction" lasttranstarted="2013-07-08T16:22:34.897" XDES="0x124272700" lockMode="S" schedulerid="2" kpid="5472" status="suspended" spid="64" sbid="0" ecid="0" priority="0" transcount="1" lastbatchstarted="2013-07-08T16:22:34.900" lastbatchcompleted="2013-07-08T16:22:34.900" clientapp="Microsoft SQL Server JDBC Driver" hostname="VIRTIPH-MAS001" hostpid="0" loginname="prtcmt_user" isolationlevel="read committed (2)" xactid="67336413" currentdb="8" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128058">
<executionStack>
<frame procname="adhoc" line="1" sqlhandle="0x02000000ec69351d69911b1227018da1e1e67e60256cd8be">
select top 290 workplan0_.id as id112_, workplan0_.request_mode as request2_112_, workplan0_.cmt_status_id as cmt3_112_, workplan0_.requestType_rd_id as requestT4_112_, workplan0_.requested_dt as requested5_112_, workplan0_.generic_desc as generic6_112_, workplan0_.brand_desc as brand7_112_, workplan0_.strength as strength112_, workplan0_.prod_form_name as prod9_112_, workplan0_.pack_desc as pack10_112_, workplan0_.inv_uom_desc as inv11_112_, workplan0_.pack_uom_desc as pack12_112_, workplan0_.requestType_name as request13_112_, workplan0_.site_name as site14_112_, workplan0_.priority as priority112_, workplan0_.source as source112_, workplan0_.cmt_status_code as cmt17_112_, workplan0_.prev_cmt_status_code as prev18_112_, workplan0_.swmd_prod_id as swmd19_112_, workplan0_.hppl_lookslike_id as hppl20_112_, workplan0_.amt_id as amt21_112_, workplan0_.locked_dt as locked22_112_ from ipharm_workPlanView workplan0_ where workplan0_.requestType_rd_id=6 and workplan0_.cmt_status_id=33 order by workplan0_.generic_des </frame>
<frame procname="unknown" line="1" sqlhandle="0x000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
select top 290 workplan0_.id as id112_, workplan0_.request_mode as request2_112_, workplan0_.cmt_status_id as cmt3_112_, workplan0_.requestType_rd_id as requestT4_112_, workplan0_.requested_dt as requested5_112_, workplan0_.generic_desc as generic6_112_, workplan0_.brand_desc as brand7_112_, workplan0_.strength as strength112_, workplan0_.prod_form_name as prod9_112_, workplan0_.pack_desc as pack10_112_, workplan0_.inv_uom_desc as inv11_112_, workplan0_.pack_uom_desc as pack12_112_, workplan0_.requestType_name as request13_112_, workplan0_.site_name as site14_112_, workplan0_.priority as priority112_, workplan0_.source as source112_, workplan0_.cmt_status_code as cmt17_112_, workplan0_.prev_cmt_status_code as prev18_112_, workplan0_.swmd_prod_id as swmd19_112_, workplan0_.hppl_lookslike_id as hppl20_112_, workplan0_.amt_id as amt21_112_, workplan0_.locked_dt as locked22_112_ from ipharm_workPlanView workplan0_ where workplan0_.requestType_rd_id=6 and workplan0_.cmt_status_id=33 order by workplan0_.generic_de </inputbuf>
</process>
<process id="processc7c5ec58" taskpriority="0" logused="9904" waitresource="PAGE: 8:1:6922" waittime="936" ownerId="67336189" transactionname="implicit_transaction" lasttranstarted="2013-07-08T16:22:33.930" XDES="0x123635380" lockMode="IX" schedulerid="1" kpid="5232" status="suspended" spid="61" sbid="0" ecid="0" priority="0" transcount="2" lastbatchstarted="2013-07-08T16:22:35.930" lastbatchcompleted="2013-07-08T16:22:35.927" clientapp="Microsoft SQL Server JDBC Driver" hostname="VIRTIPH-MAS001" hostpid="0" loginname="prtcmt_user" isolationlevel="read committed (2)" xactid="67336189" currentdb="8" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128058">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="890" sqlhandle="0x02000000a89737254800790c60bb4d0fa72b9640012d8592">
update requestedProduct set version=#P0, create_user=#P1, create_dt=#P2, last_update_user=#P3, last_update_dt=#P4, requested_by=#P5, requested_dt=#P6, requested_byName=#P7, requested_byTitle=#P8, status_id=#P9, status_by=#P10, status_date=#P11, cmt_status_id=#P12, prev_cmt_status_id=#P13, prod_id=#P14, source_prod_id=#P15, hppl_lookslike_id=#P16, amt_id=#P17, fred_code=#P18, source=#P19, swmd_prod_id=#P20, locked_dt=#P21, locked_by=#P22, request_mode=#P23, original_request_mode=#P24, site_rd_id=#P25, priority_rd_id=#P26, submitted_priority_rd_id=#P27, requestType_rd_id=#P28, comment=#P29 where id=#P30 and version=#P31 </frame>
<frame procname="unknown" line="1" sqlhandle="0x000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
(#P0 int,#P1 nvarchar(4000),#P2 datetime,#P3 nvarchar(4000),#P4 datetime,#P5 bigint,#P6 datetime,#P7 nvarchar(4000),#P8 nvarchar(4000),#P9 bigint,#P10 bigint,#P11 datetime,#P12 bigint,#P13 bigint,#P14 int,#P15 int,#P16 nvarchar(4000),#P17 nvarchar(4000),#P18 nvarchar(4000),#P19 nvarchar(4000),#P20 varchar(8000),#P21 datetime,#P22 bigint,#P23 int,#P24 int,#P25 bigint,#P26 bigint,#P27 bigint,#P28 bigint,#P29 varchar(8000),#P30 bigint,#P31 int)update requestedProduct set version=#P0, create_user=#P1, create_dt=#P2, last_update_user=#P3, last_update_dt=#P4, requested_by=#P5, requested_dt=#P6, requested_byName=#P7, requested_byTitle=#P8, status_id=#P9, status_by=#P10, status_date=#P11, cmt_status_id=#P12, prev_cmt_status_id=#P13, prod_id=#P14, source_prod_id=#P15, hppl_lookslike_id=#P16, amt_id=#P17, fred_code=#P18, source=#P19, swmd_prod_id=#P20, locked_dt=#P21, locked_by=#P22, request_mode=#P23, original_request_mode=#P24, site_rd_id=#P25, priority_rd_id=#P26, submitted_priority_rd_id=#P27, requestType_r </inputbuf>
</process>
</process-list>
<resource-list>
<pagelock fileid="1" pageid="6923" dbid="8" objectname="MYDB.dbo.requestedProduct" id="lockedec4680" mode="IX" associatedObjectId="72057594227261440">
<owner-list>
<owner id="processc7c5ec58" mode="IX" />
</owner-list>
<waiter-list>
<waiter id="process6956d8" mode="S" requestType="wait" />
</waiter-list>
</pagelock>
<pagelock fileid="1" pageid="6922" dbid="8" objectname="MYDB.dbo.requestedProduct" id="lockfa82e880" mode="SIU" associatedObjectId="72057594227261440">
<owner-list>
<owner id="process6956d8" mode="S" />
</owner-list>
<waiter-list>
<waiter id="processc7c5ec58" mode="IX" requestType="convert" />
</waiter-list>
</pagelock>
</resource-list>
</deadlock>
</deadlock-list>
</TextData>)

Resources