Killing SQL Server Processes (SPIDs)

In SQL Server, processes (SPIDs) can sometimes become blocked, leading to performance issues. As a Database Administrator, you may need to terminate such processes to maintain system efficiency. This guide demonstrates how to identify and kill blocking SPIDs.

Contents:
> Creating a Blocking SPID
> Killing a Blocking SPID
> Considerations When Killing SPIDs

How to Kill SQL User Connections

Creating a Blocking SPID

To replicate a blocking scenario, we create a table and initiate a transaction that locks it. Then, we execute a SELECT query, which gets blocked due to the transaction lock.

-- Create a sample table
CREATE TABLE TestTable (ID INT PRIMARY KEY, Name NVARCHAR(50));

-- Start a transaction that locks the table
BEGIN TRANSACTION;
INSERT INTO TestTable (ID, Name) VALUES (1, 'Blocked Row');

-- Run a SELECT query in another session to observe the blocking
SELECT * FROM TestTable;

Using sp_who2, sp_whoisactive, or sys.dm_exec_requests, we can identify the blocking SPID.

sp_whoisactive blocking query

From the screenshot, you can see that session_id 55 is the blocking SPID, causing the other session to wait for the lock to be released. This happens when a transaction remains open or a query holds onto resources for too long, creating a bottleneck.

To resolve this, we need to terminate the blocking session. This requires identifying the correct SPID (in this case, 55) and ensuring it’s safe to kill, but first try make sure there are no dependencies or unintentional side effects.

Identifying and Killing the Blocking SPID

Once identified, you can terminate the blocking session using the KILL command:

-- Kill the blocking session by replacing <SPID> with the correct value  
KILL <SPID>

Considerations When Killing SPIDs

> Killing a session with an open transaction may trigger a rollback, which could take time. Rollbacks may continue to block things up and can last longer than you’d ever expect. Listen to this story of a 2 month rollback, crazy to even imagine
> Use KILL <SPID> WITH STATUSONLY; to monitor rollback progress. This won’t always return a valid output.
> If sp_who2 or sp_whoisactive doesn’t show the blocking SPID, check the SSMS Activity Monitor.
> Killing SPIDs can help when dropping or detaching databases with active connections on test instances.

For more information on this, check out my latest post on Killing SQL Server SPIDs. This is now, old, but still useful.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts
Categories
Tags

Always On Availability Groups (AAG) (4) AWS (4) AWS Redshift (6) Certificates & Encryption (3) Change Data Capture (CDC) (2) Database Admin (72) Database Backups & Recovery (14) Database Mirroring (2) Deleting Data (1) Error Messages (5) Git Commands (6) Importing & Exporting Data (2) Linked Servers (3) Linux Admin (2) Logging & Monitoring (1) Measuring Databases (10) Microsoft Patching (2) MySQL (4) Postgres (6) PowerShell Scripts (1) SQL Server Agent (5) SQL Server Database Files (1) SQL Server Data Types (2) SQL Server Management Studio (SSMS) (15) SQL Server Network Connectivity (3) SQL Server on Linux (1) SQL Server Patching (2) SQL Server Performance (6) SQL Server Permissions (2) SQL Server Processes (SPIDs) (7) SQL Server Replication (2) SQL Server Scripts (13) Windows Admin (21) Windows Authentication (2) Windows Automation (1) Windows Events (2) Windows Firewall (4) Windows Subsystem for Linux (WSL) (18)