gotchaModerate
Why does this query cause a deadlock?
Viewed 0 times
thiswhydeadlockquerydoescause
Problem
Why does this query cause a deadlock?
Deadlock graph added:
ADDED:
Thank you Sankar for article that has solutions how to avoid this type of deadlock:
UPDATE TOP(1) system_Queue SET
[StatusID] = 2,
@ID = InternalID
WHERE InternalID IN (
SELECT TOP 1
InternalID FROM system_Queue
WHERE IsOutGoing = @IsOutGoing AND StatusID = 1
ORDER BY MessageID ASC, InternalID ASC)Deadlock graph added:
ADDED:
Thank you Sankar for article that has solutions how to avoid this type of deadlock:
- eliminate unnecessary columns from reader’s projection so he does not have to look up the clustered index
- add required columns as contained columns to the non-clustered index to make the index covering, again so that the reader does not have look up the clustered index
- avoid updates that have to maintain the non-clustered index
Solution
It looks to me as if you are trying to do an SELECT and an UPDATE in the same statement and onto the same table.
The SELECT is holding a shared lock on the values inside the IX_system_Queue_DirectionByStatus index, and the UPDATE needs for those locks to be released before it can get it's exclusive lock which will update the primary key (which I will guess is clustered and also part of the IX_system_Queue_DirectionByStatus key value).
Anyway, my guess is this query would only succeed on the rare chance that the index values it is selecting and updating are not conflicting. Is it deadlocking each time you execute (I assume that it would be).
Here is a link that explains deadlocks in more detail: http://sqlblog.com/blogs/jonathan_kehayias/archive/2008/07/30/the-anatomy-of-a-deadlock.aspx
The SELECT is holding a shared lock on the values inside the IX_system_Queue_DirectionByStatus index, and the UPDATE needs for those locks to be released before it can get it's exclusive lock which will update the primary key (which I will guess is clustered and also part of the IX_system_Queue_DirectionByStatus key value).
Anyway, my guess is this query would only succeed on the rare chance that the index values it is selecting and updating are not conflicting. Is it deadlocking each time you execute (I assume that it would be).
Here is a link that explains deadlocks in more detail: http://sqlblog.com/blogs/jonathan_kehayias/archive/2008/07/30/the-anatomy-of-a-deadlock.aspx
Context
StackExchange Database Administrators Q#2063, answer score: 13
Revisions (0)
No revisions yet.