

If you have the deadlock information you will be able to pinpoint the actual root cause.
#Deadlock in sql how to#
You can monitor deadlocking by following the information in the article: How to monitor deadlock using extended events in SQL Server 2008 and laterĪ quick win can be achieved by turning on two trace flags to capture the deadlocks in the errorlog file of SQL Server.
#Deadlock in sql update#
If you can stop all other activity on the database and run only the two update procedures simultaneously, do you still have deadlocks?ĭeadlocking is explained here: Detecting and Ending Deadlocks
#Deadlock in sql code#
Looking at your code I wouldn't think the two updates are the root cause of the deadlocks, but more victims of other statements running in the background. I think that for SQLServer a very little chance of deadlocks might exist because of potential lock escalation though.ĭeadlocks can occur because two processes are accessing the same tables, but each from the other side, while additional locking occurs. Then in case of simultaneous execution the second transaction will wait for the first one. open cursor that reads all required rows in particular (unique) order, iterate through it, and for each row issue SELECT WITH ROWLOCK), and issue update after all rows are locked. It can be implemented, for instance, by using sp_getapplock.Īnother option is to ensure that rows are locked in the same order (e.g. In my opinion, the easiest and fastest way is to modify procedure so it waits until the other instance of the same procedure completes updates. Another approach is to somehow "serialize" updates. One option is just to catch error and re-run failed transactions. They both change the same set of rows, and nothing guaranties in which order the rows are locked. Any advice would be greatly appreciated!Įach of your 2 update statements may cause deadlock when executing within different transactions, no matter explicit or implicit. I'm about 2 months into my career as a dba, and deadlocks have been difficult to get my head around.

Doesn't actually fix the problem though, just makes it happen less often.

Implementing some boolean logic in the second UPDATE to avoid running it unnecessarily.This, however, may impact other things that use the PK elsewhere. Removing the PK and making UserID the (non-unique) clustered index.Since the two updates don't really rely on each other, that seems viable. Removing the explicit transaction control.When I run the procedure in my test environment, the execution plans show that both UPDATEs are using the covering index to get the rows (seek, no lookups), then having to update the PK and the covering index. Stored procedure basically looks like this (can't share actual code but here's the pertinent pseudocode): BEGIN TRANĭeadlocks are occurring on Table, indexes involved are UserID (non-clustered covering index) and the Primary Key. Stored procedure fires twice with the same parameters within 10ms of each other, and we get deadlocks.
