Quantcast
Channel: ฟอรัม Getting started with SQL Server
Viewing all articles
Browse latest Browse all 8428

Deadlock on Simple Merge Statement

$
0
0

Hello All,

  I am running a simple MERGE Statement when parallel threads running this Stored Procedure , we see deadlock events.

MERGE test.Target AS t
USING
(
SELECT ItemID, Score, TargetText
FROM @SrcTable
) AS S
ON
t.TargetUserID = @TargetUserID
AND t.TargetUserFormPartID = @TargetUserFormPartID
AND t.ItemID = s.ItemID
WHEN NOT MATCHED THEN
INSERT (TargetUserID, TargetUserFormPartID, TenantID, ItemID, TargetText, Score, TimeSpent)
VALUES (@TargetUserID, @TargetUserFormPartID, @TenantID, s.ItemID, s.TargetText, s.Score, @TimeSpent)
WHEN MATCHED THEN
UPDATE SET t.TargetText = s.TargetText, t.Score = s.Score, t.TimeSpent = @TimeSpent

Deadlock Events noticed :

    Proc [Database Id = 8 Object Id = 1893581784]    
      resource-list
        keylock hobtid=72057594608746496 dbid=8 objectname=XXXXXXX indexname=nc_response_testuserid_itemid_testuserformpartid id=lock6c15880 mode=U associatedObjectId=72057594608746496
         owner-list
          owner id=process62274c8 mode=U
         waiter-list
          waiter id=processf21d4c8 mode=U requestType=wait
        keylock hobtid=72057594608746496 dbid=8 objectname=XXXXXXX indexname=nc_response_testuserid_itemid_testuserformpartid id=lock1001d300 mode=U associatedObjectId=72057594608746496
         owner-list
          owner id=processf21d4c8 mode=U
         waiter-list
          waiter id=process62274c8 mode=U requestType=wait

Couple of Options I am evaluating :

Add HOLDLOCK in the MergeStatement ( 

MERGE test.Target WITH (HOLDLOCK) AS t

Other Option I am thinking of adding is the query hint : OPTION (ORDER GROUP)

Has anyone experienced deadlocks while using Simple MERGE Statement?.Love to know about the fixes applied .


I90Runner


Viewing all articles
Browse latest Browse all 8428

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>