• kevaburg - Monday, March 5, 2018 10:34 AM

    Ivan R. - Sunday, February 25, 2018 7:21 PM

    agree that the more time a query has to run, it increases the chance it could deadlock with something else. this might be the most generic correlation to deadlock.

    on the other hand, reducing the time a query takes will generally be the way to reduce the chances of a deadlock.

    hope this helps
    ivan r.

    Reducing the time a query takes to execute will not get rid of the deadlock issue.  Deadlocks are a result of resource contention (very generally described) at runtime and can only be solved by programmatically altering the query to eliminate that contention.  If you are experiencing Deadlocks as show by the trace flag then you need to hunt down the rpogrammtic cause for it.

    What you are talking with regards to execution times are related to exclusive locks implemented during DML operations.  You "could" resolve the issue by using a NOLOCK hint but this is definately not advisable because you could end up with inconsistent results due to the reading of, in end effect, inconsistent data.  This is similat to having a database isolation level of Read Uncommitted.

    Changing the isolation level to "Read Committed Snapshot" helps often by using versioning to reduce the problems associated with exclusive locking.  You will need to be careful though because in some cases the version store could become bloated.

    Hi kevaburg -- Thanks for the reply. I agree with what you said. I'm very clear on what a deadlock and locking is and also read committed snapshot and why NOLOCK is not a good fix 🙂 Also keen on not giving bad advice as you are.

    My response however was indeed very over simplified. I was mostly chiming in to what John Mitchell-245523 already said that poorly optimized queries (may it be to stale stats, lack of index, locking to many rows or tables unnecessarily) has a correlation to more deadlocks.

    I was also citing along the lines of the official doco for minimizing deadlocks, which I think is a pretty good starting point: https://technet.microsoft.com/en-us/library/ms191242(v=sql.105).aspx