deadlocks and Stats

  • Hi

    I was getting alerts about deadlocks continuously every min on the server, I updated stats and alerts stopped. Again after  two days, got deadlock alerts, updated stats, alerts gone.
    did the trace (1222,-1), sent the deadlock victim details to development team.

    What  is the connection between stats and deadlocks?

    Thanks

  • If stats are out of date, the query optimizer is more likely to make a poor choice of execution plan.  The query will take longer to run and deadlocks will become more likely.  You'll want to have a look at the actual queries that are causing the deadlocks to see whether they can be tuned to make them more resilient to this situation.

    John

  • 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.

  • Review the query plan of the longer running queries and see if tuning indexes can help speed up the queries.  Also, look into fragmentation of the tables as well.  Maybe a rebuilding of the indexes if they get a high number of inserts, deletes and or updates to them causing fragmentation.

  • sqlguy80 - Wednesday, February 21, 2018 9:48 AM

    Hi

    I was getting alerts about deadlocks continuously every min on the server, I updated stats and alerts stopped. Again after  two days, got deadlock alerts, updated stats, alerts gone.
    did the trace (1222,-1), sent the deadlock victim details to development team.

    What  is the connection between stats and deadlocks?

    Thanks

    Could you check the cardinality estimator settings of your SQL Server ?

  • 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.

  • 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


  • And depending on the code it may also be worthwhile to set things up in an infinite loop with a break to exit when processing is done or an error other than a deadlock is encountered.  I did this in some code I wrote, and in which I made sure it would be the deadlock victim if it was involved in a deadlock, so that the transaction aborted by the deadlock would be retried after a slight delay to allow the other code to finish.

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply