17

My website doesn't seem to handle a high number of visitors, I believe it's because the server is too simple.

2 hours ago my website was getting a lot of hits and I noticed that 3 deadlock errors occurred, the error is:

System.Data.SqlClient.SqlException : Transaction (Process ID 58) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.

I'm not sure why this happened... Looking at the stack trace, I could see that this happened with a select query.

Anyone knows what may be the cause of this error?

The server is running Windows 2008 and Sql Server 2008.

1

3 Answers 3

13

SQL Server 2008 has multiple ways to identify processes and queries involved in deadlock.

  1. If deadlocks are easy to reproduce,frequency is higher and you can profile SQL server (you have the access and performance cost on server when profiler is enabled) using SQL Profiler will give you nice graphical view of deadlock. This page has all the information you need to use deadlock graphs http://sqlmag.com/database-performance-tuning/gathering-deadlock-information-deadlock-graph

  2. Most of the times reproducing deadlocks is hard, or they happen in production environment where we dont want to attach Profiler to it and affect performance.

I would use this query to get deadlocks happened:

SELECT
  xed.value('@timestamp', 'datetime') as Creation_Date,
  xed.query('.') AS Extend_Event
FROM
(
  SELECT CAST([target_data] AS XML) AS Target_Data
  FROM sys.dm_xe_session_targets AS xt
  INNER JOIN sys.dm_xe_sessions AS xs
  ON xs.address = xt.event_session_address
  WHERE xs.name = N'system_health'
  AND xt.target_name = N'ring_buffer'
) AS XML_Data
CROSS APPLY Target_Data.nodes('RingBufferTarget/event[@name="xml_deadlock_report"]') AS XEventData(xed)
ORDER BY Creation_Date DESC

I would NOT go in the direction of using (NOLOCK) to fix deadlocks. That is slippery slope and hiding the original problem.

2
  • 1
    Your sqlmag link is broken. You might want to link another page that explains how to get deadlock graphs.
    – John Odom
    Mar 27, 2020 at 14:53
  • Also do you have a link to a documentation that explains how to read the query that you provided?
    – John Odom
    Mar 27, 2020 at 15:05
8

Writes will block reads on SQL Server, unless you have row versioning enabled. You should use the sp_who2 stored procedure and a SQL Profiler trace. sp_who2 will tell you which processes are blocking which, and the profiler will tell you what the last statement was for the blocking process.

-4

If you don't mind dirty reads you can try putting (NOLOCK) after your table names in your SELECT queries. The trade off here is that you are not guaranteed the most up to date data as UPDATE and INSERT statements currently executing are ignored.

Usually this is not to much of a train-smash as most systems read far more than they update/insert, but obviously it depends on the nature of your application.

Alternatively have a look at http://www.sql-server-performance.com/tips/deadlocks_p1.aspx

2
  • 5
    Nolock, without precisely understanding the consequence of "not guaranteed the most up to date data" is a very bad idea.
    – Eric J.
    Mar 12, 2015 at 16:54
  • 3
    I heavily recommend AGAINST using NOLOCK. I realize this is an old post but I feel I need to offer a warning to anyone who stumbles across this (like I did) looking for help with deadlocks. I have personally seen the consequences described by the evils of NOLOCK - and they are difficult to cleanup. Do not allow NOLOCK to become a habit in your team. One can argue that NOLOCK was necessary in SQLServer 2000 due to its less than robust locking model, but starting with SQLServer2005 there is little to no need for it. Look into ALTER DATABASE mydatabase SET READ_COMMITTED_SNAPSHOT ON.
    – ripvlan
    Oct 6, 2016 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.