HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Deadlock on two update statements on the same page

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
sametheupdatedeadlockstatementstwopage

Problem

I'm receiving multiple deadlocks in one SQL Server.
The deadlocks occur when the same query with different parameter values is being issued on the same database:

Example:

Query 1:

UPDATE Tabel1 
SET column1=8, column2=1 
WHERE ((column3=117902015625) OR (column3 IN(SELECT column4 
                                                        FROM Table2 
                                                        WHERE (column5=117902015625))))


Query 2:

UPDATE Tabel1 
SET column1=13, column2=1 
WHERE ((column3=300004584884) OR (column3 IN(SELECT column4 
                                                        FROM Table2 
                                                        WHERE (column5=300004584884))))


How can I prevent these kind of deadlocks?

Edit:

The .xdl content:


    
        
            
                unknown
            
            UPDATE Table1 SET column1=8, column2=1 WHERE ((column3=117902028987) OR (column3 IN(SELECT column4 FROM Table2 WHERE (column5=117902028987))))
        
        
            
                unknown
            
            UPDATE Table1 SET column1=13, column2=1 WHERE ((column3=300088596120) OR (column3 IN(SELECT column4 FROM Table2 WHERE (column5=300088596120))))
        
    
    
        
            
                
                
            
            
                
            
        
        
            
                
                
            
            
                
            
        
    


It is some adhoc query being ran against the database, probably part of some transaction.
I'll contact the owner of the application to get more information about it.

Meanwhile, the estimated execution plan for this query is (100% cost in the clutered index update):

```



















Solution

Is Column3 the clustered index column of the table? If not, change the query to get the clustered index column based on the predicate, and then, update those records only.

Always ensure the updates are happening based on the clustered indexed column to avoid deadlocks.

Select (ClusteredKey) into #tblSometable From Table1 
Where Column3 =(Your Predicate)

Update A
SET Column2 = ABC, ...ColumnN = MNO
FROM Table1 A
Join #tblSometable B
ON A.ClusteredKey = B.ClusteredKey;


Let me know if this doesn't solve your problem.

Code Snippets

Select (ClusteredKey) into #tblSometable From Table1 
Where Column3 =(Your Predicate)

Update A
SET Column2 = ABC, ...ColumnN = MNO
FROM Table1 A
Join #tblSometable B
ON A.ClusteredKey = B.ClusteredKey;

Context

StackExchange Database Administrators Q#234947, answer score: 2

Revisions (0)

No revisions yet.