Search code examples
sqlsql-serverdeadlock

Deadlocks - Will this really help?


So I've got a query that keeps deadlocking on me. People who know the system well can't figure out why the sproc is deadlocking, but they tell me that I should just add this to it:

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

Is this really a valid solution? What does that do?


Solution

  • SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
    

    This will cause the system to return inconsitent data, including duplicate records and missing records. Read more at Previously committed rows might be missed if NOLOCK hint is used, or here at Timebomb - The Consistency problem with NOLOCK / READ UNCOMMITTED.

    Deadlocks can be investigated and fixed, is not a big deal if you follow the proper procedure. Of course, throwing a dirty read may seem easier, but down the road you'll be sitting long hours staring at your general ledger and wondering why the heck it does not balance debits and credits. So read again until you really grok this: DIRTY READs ARE INCONSISTENT READS.

    If you want a get-out-of-jail card, turn on snapshot isolation:

    ALTER DATABASE MyDatabase
    SET READ_COMMITTED_SNAPSHOT ON
    

    But keep in mind that snapshot isolation does not fix the deadlocks, it only hides them. Proper investigation of the deadlock cause and fix is always the appropriate action.