<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Carl Walker</title>
    <description>The latest articles on DEV Community by Carl Walker (@crw).</description>
    <link>https://dev.to/crw</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3813007%2Fe81d8ab7-6873-4b1e-acad-9a7cbb4f23be.png</url>
      <title>DEV Community: Carl Walker</title>
      <link>https://dev.to/crw</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/crw"/>
    <language>en</language>
    <item>
      <title>How to Detect and Resolve Blocking Sessions in SQL Server</title>
      <dc:creator>Carl Walker</dc:creator>
      <pubDate>Tue, 10 Mar 2026 09:59:48 +0000</pubDate>
      <link>https://dev.to/crw/how-to-detect-and-resolve-blocking-sessions-in-sql-server-447d</link>
      <guid>https://dev.to/crw/how-to-detect-and-resolve-blocking-sessions-in-sql-server-447d</guid>
      <description>&lt;p&gt;If you've ever worked with SQL Server in a production environment, you've probably encountered a situation where queries suddenly slow down or applications become unresponsive. One of the most common causes of this issue is blocking.&lt;/p&gt;

&lt;p&gt;Blocking occurs when one query holds a lock on a resource and prevents another query from accessing that same resource. While locking is a normal part of how SQL Server maintains data consistency, excessive blocking can seriously affect database performance.&lt;/p&gt;

&lt;p&gt;In this article, we’ll explore:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What blocking is in SQL Server&lt;/li&gt;
&lt;li&gt;How to detect blocking sessions&lt;/li&gt;
&lt;li&gt;How to identify the blocking query&lt;/li&gt;
&lt;li&gt;Ways to resolve and prevent blocking issues&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;What is Blocking in SQL Server?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Blocking happens when a transaction locks a resource (such as a table or row), and another transaction needs access to that same resource.&lt;/p&gt;

&lt;p&gt;For example: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Transaction A updates a row in a table&lt;/li&gt;
&lt;li&gt;Transaction B tries to read or update the same row&lt;/li&gt;
&lt;li&gt;Transaction B must wait until Transaction A completes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If Transaction A takes too long to finish, other queries start piling up behind it.&lt;/p&gt;

&lt;p&gt;In production systems, this can cause:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;slow application performance&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;timeouts&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;long query wait times&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;frustrated users&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;How to Detect Blocking Sessions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;SQL Server provides several Dynamic Management Views (DMVs) that help identify blocking activity.&lt;/p&gt;

&lt;p&gt;One of the easiest ways is to query sys.dm_exec_requests.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT
    session_id,
    blocking_session_id,
    wait_type,
    wait_time,
    wait_resource,
    status
FROM sys.dm_exec_requests
WHERE blocking_session_id &amp;lt;&amp;gt; 0;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What this query shows&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;session_id&lt;/strong&gt; → the waiting session&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;blocking_session_id&lt;/strong&gt; → the session causing the block&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;wait_type&lt;/strong&gt; → the type of wait occurring&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;wait_time&lt;/strong&gt; → how long the session has been waiting&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the blocking_session_id column contains a value, that means the session is being blocked by another process.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Identifying the Blocking Query&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Once you find the blocking session ID, the next step is to determine which query is causing the block.&lt;/p&gt;

&lt;p&gt;You can retrieve the query text using the following query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT
    r.session_id,
    r.blocking_session_id,
    t.text AS query_text
FROM sys.dm_exec_requests r
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) t
WHERE r.session_id = &amp;lt;blocking_session_id&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace  with the session ID returned from the previous query.&lt;/p&gt;

&lt;p&gt;This will show the SQL statement responsible for holding the lock.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using sp_who2 for Quick Monitoring&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;SQL Server also includes a useful built-in stored procedure called sp_who2 that provides a quick overview of active sessions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;EXEC sp_who2;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Look at the BlkBy column.&lt;/p&gt;

&lt;p&gt;If a session shows a value in this column, it means it is being blocked by another session.&lt;/p&gt;

&lt;p&gt;Although it's an older diagnostic tool, many DBAs still use it for quick troubleshooting.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Resolving Blocking Sessions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Once you identify the blocking session, you have several options.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Investigate the query&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Check if the query is performing unnecessary operations such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;table scans&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;missing indexes&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;large transactions&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Optimizing the query may resolve the issue without terminating it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Commit or rollback the transaction&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Sometimes the blocking occurs because a transaction was left open longer than expected.&lt;/p&gt;

&lt;p&gt;Completing the transaction will release the lock.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Terminate the blocking session&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If the blocking query is causing major disruption, you may need to stop it using the KILL command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;KILL &amp;lt;session_id&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Be cautious when using this command because SQL Server must roll back the transaction before releasing the locks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Monitoring Long-Running Queries&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In many cases, blocking occurs because a query has been running for too long.&lt;/p&gt;

&lt;p&gt;If you need to identify and terminate long-running queries, you can follow this guide: &lt;a href="https://dev.to/crw/how-to-find-and-kill-long-running-queries-in-sql-server-5269"&gt;How to Find and Kill Long-Running Queries in SQL Server&lt;/a&gt; This helps DBAs quickly detect problematic queries before they start blocking other sessions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Best Practices to Prevent Blocking&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;While blocking can't always be avoided, good database practices can reduce its frequency.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Keep transactions short&lt;/strong&gt;&lt;br&gt;
Long transactions hold locks longer and increase the chance of blocking.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Create proper indexes&lt;/strong&gt;&lt;br&gt;
Missing indexes often force SQL Server to scan entire tables.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Avoid unnecessary locks&lt;/strong&gt;&lt;br&gt;
Design queries that access only the data they need.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Monitor database activity regularly&lt;/strong&gt;&lt;br&gt;
Routine monitoring helps detect performance issues early.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Final Thoughts&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Blocking is a normal behavior in SQL Server, but when it becomes excessive, it can significantly affect database performance.&lt;/p&gt;

&lt;p&gt;By using SQL Server’s built-in monitoring tools and DMVs, DBAs can quickly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;detect blocking sessions&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;identify problematic queries&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;resolve issues before they impact users&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Understanding how blocking works and how to troubleshoot it is an essential skill for anyone managing SQL Server environments.&lt;/p&gt;

</description>
      <category>database</category>
      <category>performance</category>
      <category>sql</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How to Find and Kill Long-Running Queries in SQL Server</title>
      <dc:creator>Carl Walker</dc:creator>
      <pubDate>Sun, 08 Mar 2026 14:14:18 +0000</pubDate>
      <link>https://dev.to/crw/how-to-find-and-kill-long-running-queries-in-sql-server-5269</link>
      <guid>https://dev.to/crw/how-to-find-and-kill-long-running-queries-in-sql-server-5269</guid>
      <description>&lt;p&gt;Anyone who works with SQL Server long enough eventually faces the same problem: a query that just keeps running.&lt;/p&gt;

&lt;p&gt;Maybe it's blocking other sessions, maybe CPU usage spikes, or maybe an application suddenly becomes slow. Long-running queries are one of the most common causes of SQL Server performance issues.&lt;/p&gt;

&lt;p&gt;In this article, we'll walk through how to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Identify long-running queries&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Detect blocking sessions&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Investigate the query details&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Safely terminate problematic queries&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s get started.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Long-Running Queries Are a Problem&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A query running longer than expected can cause several issues:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Block other queries&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Consume excessive CPU or memory&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Lock critical database resources&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Slow down applications&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In high-traffic environments, even a single poorly optimized query can impact multiple users.&lt;/p&gt;

&lt;p&gt;That's why knowing how to quickly identify and stop problematic queries is essential.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Finding Long-Running Queries&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;SQL Server provides several Dynamic Management Views (DMVs) that help monitor currently executing queries.&lt;/p&gt;

&lt;p&gt;One of the most useful views is sys.dm_exec_requests.&lt;/p&gt;

&lt;p&gt;Run the following query to find active sessions and their execution time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT
    session_id,
    status,
    start_time,
    command,
    cpu_time,
    total_elapsed_time,
    blocking_session_id
FROM sys.dm_exec_requests
WHERE session_id &amp;gt; 50
ORDER BY total_elapsed_time DESC;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What this shows&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;session_id → the connection ID&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;total_elapsed_time → how long the query has been running&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;blocking_session_id → identifies blocking sessions&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;status → running, suspended, etc.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Queries with high elapsed time may require investigation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Viewing the Actual Query Text&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Once you identify a suspicious session, you’ll probably want to see the SQL statement being executed.&lt;/p&gt;

&lt;p&gt;You can retrieve the query text using sys.dm_exec_sql_text.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT
    r.session_id,
    r.status,
    r.start_time,
    r.cpu_time,
    r.total_elapsed_time,
    t.text AS query_text
FROM sys.dm_exec_requests r
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) t
WHERE r.session_id &amp;gt; 50
ORDER BY r.total_elapsed_time DESC;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Detecting Blocking Queries&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Sometimes the problem isn’t just a slow query, it's a blocking query.&lt;/p&gt;

&lt;p&gt;Blocking occurs when one query holds a lock that prevents other queries from accessing the same data.&lt;/p&gt;

&lt;p&gt;To find blocking sessions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT
    session_id,
    blocking_session_id,
    wait_type,
    wait_time,
    wait_resource
FROM sys.dm_exec_requests
WHERE blocking_session_id &amp;lt;&amp;gt; 0;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you see a session ID in the blocking_session_id column, it means another process is waiting on it.&lt;/p&gt;

&lt;p&gt;In these cases, resolving the blocking query becomes a priority.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Using sp_who2 for Quick Monitoring&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;SQL Server also includes a helpful system stored procedure called sp_who2.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;EXEC sp_who2;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This shows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Active sessions&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;CPU usage&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Disk IO&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Blocking sessions&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Look at the BlkBy column to identify blocking queries.&lt;/p&gt;

&lt;p&gt;Although it’s an older tool, it’s still very useful for quick troubleshooting.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Killing a Long-Running Query&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If a query is causing serious issues, you may need to terminate it.&lt;/p&gt;

&lt;p&gt;To stop a session, use the KILL command.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;KILL 78;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where 78 is the session ID.&lt;/p&gt;

&lt;p&gt;Important&lt;/p&gt;

&lt;p&gt;When you kill a query, SQL Server rolls back the transaction. If the transaction was large, the rollback may take some time.&lt;/p&gt;

&lt;p&gt;You can monitor rollback progress with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;KILL 78 WITH STATUSONLY;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;6. Preventing Long-Running Queries&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Stopping a query is only a short-term fix. Ideally, you should prevent performance issues before they occur.&lt;/p&gt;

&lt;p&gt;Some common strategies include:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Add proper indexes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Missing indexes often lead to table scans and slow queries.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Avoid SELECT *&lt;/em&gt;*&lt;/p&gt;

&lt;p&gt;Only retrieve the columns you actually need.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Monitor execution plans&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Execution plans reveal inefficient query operations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use query timeouts&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Applications should have limits to prevent runaway queries.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Long-running queries are inevitable in most SQL Server environments, but knowing how to quickly identify and resolve them can prevent serious performance issues.&lt;/p&gt;

&lt;p&gt;The key steps are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Identify the running query&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Investigate the SQL statement&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Detect blocking sessions&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Kill the query if necessary&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Optimize the query to prevent recurrence&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;SQL Server’s DMVs provide powerful insight into what's happening inside your database, and learning to use them effectively can make troubleshooting much easier.&lt;/p&gt;

</description>
      <category>database</category>
      <category>performance</category>
      <category>sql</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
