Deadlock on DELETE - DELETE for parallel execution of same stored procedure
3
votes
1
answer
626
views
I have a stored procedure which ran parallely by two processes.
Code for sp :
DECLARE @inputXML XML
DECLARE @tempTable TABLE (afid INT, ccid INT)
INSERT INTO @tempTable
SELECT ref.value('@afid', 'INT'),ref.value('@ccid', 'INT')
FROM @inputXML.nodes('/Ilist/aidA/afid') R ( ref )
DELETE FROM StylinInfoTemp
FROM @tempTable AS temp
WHERE temp.afid= StylinInfoTemp.afid
AND temp.ccid= StylinInfoTemp.ccid
this is the schema for StylinInfoTemp
Columns :
cisid int
afid int
ccid int
entity int
dtime int
guid uniqueidentifier
addFlags int
Index is present on
cisid, afid, ccid
here is the schema for tempTable
(afid INT, ccid INT)
This
However the stored procedure tempTable will always have unique afid.
When this stored proc executes I am getting a deadlock on the same page, I am having trouble understanding how exactly is this leading to a deadlock and why do we have same page Id below
Can someone help me understanding why deadlock, why same page id, how can two process hold and request for the same page id at the same time(image above) and how can I resolve this
DeadLock XML : https://codebeautify.org/xmlviewer/y228f34a3
Query Plan:
DELETE FROM StylinInfoTemp FROM @tempTable AS temp WHERE temp.afid = StylinInfoTemp.afid AND temp.ccid = StylinInfoTemp.ccid
|--Clustered Index Delete(OBJECT:([Magneto].[dbo].[StylinInfoTemp].[StylinInfoTemp_cis_afid_ccid_PK]))
|--Nested Loops(Left Semi Join, WHERE:(@tempTable.[afid] as [temp].[afid]=[Magneto].[dbo].[StylinInfoTemp].[afId] AND @temp.[ccid] as [temp].[ccid]=[Magneto].[dbo].[StylinInfoTemp].[ccid]))
|--Clustered Index Scan(OBJECT:([Magneto].[dbo].[StylinInfoTemp].[StylinInfoTemp_cis_afid_ccidd_PK]))
|--Table Scan(OBJECT:(@tempTable AS [temp]))
tempTable
is populated on the input received by the stored proc.
tempTable
in itself can never have duplicate entries. However StylinInfoTemp
table may have duplicate entries on afId
and ccid
. But is unique on cisid, afid, ccid
eg Check duplicate values on afid below


Asked by Himanshuman
(197 rep)
Jul 14, 2022, 07:20 AM
Last activity: May 4, 2025, 10:03 PM
Last activity: May 4, 2025, 10:03 PM