Blocking vs. Deadlocks in Microsoft SQL Server (2024)

by Wes Moody | May 13, 2022 | Blog, SQL Server | 3 comments

Blocking vs. Deadlocks in Microsoft SQL Server (1)

When I was learning about Microsoft SQL Server issues a DBA needs to understand, I was reading about deadlocks. At first, I was confused because it sounded like the definition of blocking, which I was already more familiar with. I re-read it a couple of times and still wasn’t getting it. However, after compiling some information from a few different sources, I put this together to help others who might be in the same boat. I hope this helps!

Blocking occurs as a result of two (or more) processes needing the same resource. The problem is that in Microsoft SQL Server, only one process can have access to a resource at a time. So, if the first process is already utilizing (or locking) the desired resource, then the second process is blocked from accessing it. Microsoft SQL Server will force the second process to wait until the first process is finished. The diagram below illustrates what happens:

Blocking

Blocking vs. Deadlocks in Microsoft SQL Server (2)

Deadlocks are different, but they can be easily confused at first glance. Like blocking, a deadlock involves two processes that need specific resources to complete. However, unlike blocking, the two processes are not trying to get the same resource. A deadlock occurs when Process 1 is locking Resource A and Process 2 is locking Resource B. In order for Process 1 to complete and free up Resource A, it needs to put a lock on Resource B. However,in order for Process 2 to complete and free up Resource B, it needs to put a lock on Resource A. So we get a deadlock (think of it like a stalemate in chess). The diagram below illustrates the deadlock.

Deadlock

Blocking vs. Deadlocks in Microsoft SQL Server (3)

In short, blocking occurs when two processes need the same resource. The second process will typically wait for the first to release the lock, then it will finish its job. On the other hand, a deadlock occurs when two processes are at a “stalemate” nobody’s going anywhere, and neither side can get what it needs because they need a lock on each other’s processes in order to finish their jobs. Typically, Microsoft SQL Server will resolve the deadlock on its own by rolling back the process that has done the least work, allowing the other process to move forward and hopefully finish unimpeded. Eventually, the process that was rolled back will restart and finish unimpeded as well.

If you have any questions, feel free to reach out to us!

  1. Blocking vs. Deadlocks in Microsoft SQL Server (4)

    Innocent K Nchamchamon July 10, 2022 at 8:10 pm

    Perfect explanation here; wasn’t so clear about locking, blocking and deadlock until after this beautiful illustration.
    Thank you so much

    Reply

  2. Blocking vs. Deadlocks in Microsoft SQL Server (5)

    venadelleon January 22, 2023 at 5:38 pm

    This demonstration is just awesome.

    Reply

  3. Blocking vs. Deadlocks in Microsoft SQL Server (6)

    Jayantaon June 12, 2023 at 10:42 am

    Thank You

    Reply

Submit a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Blocking vs. Deadlocks in Microsoft SQL Server (2024)

FAQs

How to check blocking and deadlock in SQL Server? ›

To do this, you can use one of the following methods:
  1. In SQL Server Management Studio (SSMS) Object Explorer, right-click the top-level server object, expand Reports, expand Standard Reports, and then select Activity - All Blocking Transactions. ...
  2. Open Activity Monitor in SSMS and refer to the Blocked By column.
Jun 20, 2023

How to solve blocking issues in SQL Server? ›

Resolving locking issues (and the related blocking) requires looking at the database query causing the locking, not looking at the query being blocked. Monitoring the SQL Server database reveals queries that cause excessively long locking and blocking.

How to avoid blocking and deadlock in SQL Server? ›

Our experts have put together this list of tips to help you avoid a deadlock in SQL Server:
  1. Make sure the transactions are short. ...
  2. Our experts recommend changing the isolation level to SNAPSHOT ISOLATION or READ COMMITTED SNAPSHOT ISOLATION. ...
  3. Try to access objects by following a logical manner for multiple transactions.
Dec 21, 2022

How to get list of deadlocks in SQL Server? ›

To view deadlock information, the SQL Server Database Engine provides monitoring tools in the form of the system_health xEvent session, two trace flags, and the deadlock graph event in SQL Profiler.

How to solve deadlock issue in SQL Server? ›

Recap the solution steps:
  1. Check the system_health session for deadlocks.
  2. Create an extended event session to capture the deadlocks.
  3. Analyze the deadlock reports and graphs to figure out the problem.
  4. If it is possible to make improvements or changing the queries involved in the deadlock.
Jul 15, 2020

How to check blocking query in SQL Server? ›

To check blocking in SQL Server, you can use a query to identify the blocking SPID. All you need to do is use SQL Server DMVs and execute a query using the sys. dm_exec_requests DMV. This will return a list of the blocking SPID, any blocked SPIDs, and the wait type and duration.

How to clear a blocking session in SQL Server? ›

Killing a Blocking Process

After you have connected, right click on the instance name and select 'Activity Monitor' from the menu. Once Activity Monitor has loaded, expand the 'Processes' section. Scroll down to the SPID of the process you would like to kill. Right click on that line and select 'Kill Process'.

How to resolve deadlock in database? ›

However, you may be able to solve them by setting the query's MAXDOP to 1. This forces the SQL Server to execute the query in a serial plan, preventing a parallel deadlock. Alternatively, you can use OPTION (FORCE ORDER), which forces the query optimizer to use your specified order of joins.

What is the difference between blocking and deadlock in SQL? ›

A deadlock is a more severe form of blocking where two or more processes are each waiting for the other to release a resource, creating an endless loop that can't be resolved without external intervention. Deadlocks can halt operations, causing significant business impact.

How to solve deadlock? ›

A deadlock is resolved by aborting and restarting a process, relinquishing all the resources that the process held. This technique does not limit resource access or restrict process action.

Which best practice minimizes deadlocks in a database? ›

  • Ensure database design is properly normalized.
  • Keep transactions short.
  • Set up database mirroring.
  • Avoid using cursors.
  • Add more space on the storage side.
Jan 17, 2024

How to detect deadlock in SQL Server? ›

Use SQL Server Profiler to identify the cause of a deadlock. A deadlock occurs when there is a cyclic dependency between two or more threads, or processes, for some set of resources within SQL Server. Using SQL Server Profiler, you can create a trace that records, replays, and displays deadlock events for analysis.

How to check SQL locks in SQL Server? ›

To obtain information about locks in the SQL Server Database Engine, use the sys. dm_tran_locks dynamic management view.

What is an example of a deadlock? ›

In a database, a deadlock is a situation in which two or more transactions are waiting for one another to give up locks. For example, Transaction A might hold a lock on some rows in the Accounts table and needs to update some rows in the Orders table to finish.

How to check deadlock trace status in SQL Server? ›

You can check the status of the trace flag using the DBCC TRACESTATUS (1222, -1) command. You can see by the following results that the trace flag is enabled, and that it is enabled globally. You can turn off the trace flag any time by simply issuing the DBCC TRACEOFF (1222,-1) command.

How do you identify and resolve deadlock? ›

Deadlock detection algorithms, such as the Wait-For Graph, are used to identify deadlocks, and recovery algorithms, such as the Rollback and Abort algorithm, are used to resolve them. The recovery algorithm releases the resources held by one or more processes, allowing the system to continue to make progress.

How to get XML deadlock report in SQL Server? ›

Open SQL server profiler > click on file > Open trace file > change the file type to XML > Select the XML trace file. Click on the deadlock graph event in the profiler displayed traces.

Top Articles
Latest Posts
Article information

Author: Greg Kuvalis

Last Updated:

Views: 6458

Rating: 4.4 / 5 (55 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Greg Kuvalis

Birthday: 1996-12-20

Address: 53157 Trantow Inlet, Townemouth, FL 92564-0267

Phone: +68218650356656

Job: IT Representative

Hobby: Knitting, Amateur radio, Skiing, Running, Mountain biking, Slacklining, Electronics

Introduction: My name is Greg Kuvalis, I am a witty, spotless, beautiful, charming, delightful, thankful, beautiful person who loves writing and wants to share my knowledge and understanding with you.