Table of Contents
1. Identify the Blocking Process
2. Get Details About the Blocking Session
3. Kill the Blocking Process
4. Prevent Future Blocking Issues
Home Database SQL How to find and kill a blocking process in SQL?

How to find and kill a blocking process in SQL?

Aug 18, 2025 am 04:36 AM
sql 阻塞进程

Use sys.dm_exec_requests and sp_who2 to identify the blocking process by checking blocking_session_id and the BlkBy column; 2. Query sys.dm_exec_sessions with sys.dm_exec_sql_text to get details like login name and SQL text of the blocking session; 3. Execute KILL <blocking_session_id> to terminate the blocking process, or use KILL <blocking_session_id> WITH STATUSONLY to monitor rollback progress; 4. Prevent future issues by improving indexing, keeping transactions short, using appropriate isolation levels like READ COMMITTED SNAPSHOT, and monitoring for long-running queries. This approach resolves current blocking and reduces future occurrences.

How to find and kill a blocking process in SQL?

When a SQL query is blocked by another process, it can slow down or freeze your application. Finding and killing the blocking process is a common troubleshooting task in SQL Server. Here’s how to do it step by step.

How to find and kill a blocking process in SQL?

1. Identify the Blocking Process

Use system views like sys.dm_exec_requests and sys.dm_exec_sessions to find which session is causing the block.

SELECT 
    r.session_id,
    r.blocking_session_id,
    r.status,
    r.command,
    r.wait_type,
    r.wait_time,
    r.wait_resource,
    t.text AS sql_text
FROM sys.dm_exec_requests r
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) t
WHERE r.blocking_session_id <> 0;

This query shows:

How to find and kill a blocking process in SQL?
  • session_id: The ID of the blocked process.
  • blocking_session_id: The ID of the process causing the block.
  • sql_text: The actual SQL command being executed.

You can also check all active sessions, including the blocker:

EXEC sp_who2;

Look for entries where the BlkBy column is not empty — those are blocked sessions, and the ID in BlkBy is the blocking SPID.

How to find and kill a blocking process in SQL?

2. Get Details About the Blocking Session

Once you have the blocking_session_id, get more info about it:

SELECT 
    s.session_id,
    s.login_name,
    s.host_name,
    s.program_name,
    t.text AS current_sql
FROM sys.dm_exec_sessions s
CROSS APPLY sys.dm_exec_sql_text(s.session_id)
WHERE s.session_id = <blocking_session_id>;

Replace <blocking_session_id> with the actual ID. This helps you understand who or what is causing the block — maybe a long-running report or an open transaction.


3. Kill the Blocking Process

If the process is not critical and has been running too long, you can terminate it using the KILL command:

KILL <blocking_session_id>;

For example:

KILL 54;

⚠️ Warning: Killing a session rolls back any uncommitted transactions, which might take time and temporarily increase load.

If you need more info after killing, you can use:

KILL <blocking_session_id> WITH STATUSONLY;

This shows the rollback progress.


4. Prevent Future Blocking Issues

While killing a process resolves the immediate issue, consider:

  • Indexing: Poorly indexed queries can hold locks longer.
  • Keep transactions short: Avoid long-running transactions.
  • Use appropriate isolation levels: Consider READ COMMITTED SNAPSHOT to reduce blocking.
  • Monitor regularly: Set up alerts for long-running or blocking queries.

Basically, find the blocker using DMVs, confirm it’s safe to kill, then use KILL to stop it. It’s a quick fix, but addressing the root cause prevents recurrence.

The above is the detailed content of How to find and kill a blocking process in SQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1598
276
How to get the first and last day of the year in SQL? How to get the first and last day of the year in SQL? Aug 11, 2025 pm 05:42 PM

ThefirstdayoftheyearisobtainedbyconstructingortruncatingtoJanuary1stofthegivenyear,andthelastdayisDecember31stofthesameyear,withmethodsvaryingbydatabasesystem;2.Fordynamiccurrentyeardates,MySQLusesDATE_FORMATorMAKEDATE,PostgreSQLusesDATE_TRUNCorDATE_

How to find the sum of a column in SQL? How to find the sum of a column in SQL? Aug 08, 2025 pm 05:54 PM

TofindthesumofacolumninSQL,usetheSUM()function,whichreturnsthetotalofallnumericvaluesinaspecifiedcolumnwhileignoringNULLs;1.Usebasicsyntax:SELECTSUM(column_name)ASaliasFROMtable_name;2.Ensurethecolumnhasnumericdatatoavoiderrors;3.ApplyWHEREtofilterro

Understanding SQL Execution Context and Permissions Understanding SQL Execution Context and Permissions Aug 16, 2025 am 08:57 AM

SQL execution context refers to the identity or role when running SQL statements, which determine which resources and operation permissions can be accessed. Permission setting should follow the principle of minimum permissions, and common permissions include SELECT, INSERT, EXECUTE, etc. To troubleshoot permission issues, you need to confirm the login name, role permissions, EXECUTEAS settings and schema authorization. Performing context switching can be implemented through EXECUTEAS, but attention should be paid to user existence, permission granting and performance security impact. It is recommended to avoid arbitrarily assigning db_owner or sysadmin roles. The application account should only access necessary objects and be authorized through schema.

How to join a table to itself in SQL How to join a table to itself in SQL Aug 16, 2025 am 09:37 AM

Aself-joinisusedtocomparerowswithinthesametable,suchasinhierarchicaldatalikeemployee-managerrelationships,bytreatingthetableastwoseparateinstancesusingaliases,asdemonstratedwhenlistingemployeesalongsidetheirmanagers'nameswithaLEFTJOINtoincludetop-lev

What is the ALTER TABLE statement in SQL? What is the ALTER TABLE statement in SQL? Aug 08, 2025 pm 02:13 PM

TheALTERTABLEstatementisusedtomodifyanexistingtable’sstructurewithoutrecreatingit;1.AddanewcolumnusingADDCOLUMN;2.DropacolumnwithDROPCOLUMN,whichalsodeletesitsdata;3.RenameacolumnusingRENAMECOLUMN,withsyntaxconsistentinMySQL,SQLServer,andPostgreSQL;4

How to create a view in SQL How to create a view in SQL Aug 11, 2025 pm 12:40 PM

The syntax for creating a view is the CREATEVIEWview_nameASSELECT statement; 2. The view does not store actual data, but is based on the real-time query results of the underlying table; 3. The view can be modified using CREATEORREPLACEVIEW; 4. The view can be deleted through DROPVIEW; 5. The view is suitable for simplifying complex queries, providing data access control, and maintaining interface consistency, but attention should be paid to performance and logic, and finally ends with a complete sentence.

How to use FULL OUTER JOIN in SQL? How to use FULL OUTER JOIN in SQL? Aug 17, 2025 am 12:25 AM

AFULLOUTERJOINreturnsallrowsfrombothtables,withNULLswherenomatchexists;1)Itcombinesmatchingrecordsandincludesunmatchedrowsfrombothleftandrighttables;2)Itisusefulfordatareconciliation,mergereports,andidentifyingmismatches;3)Notalldatabasessupportitnat

How do you limit the number of rows returned in a SQL query? How do you limit the number of rows returned in a SQL query? Aug 08, 2025 pm 05:46 PM

TolimitrowsinaSQLquery,usetheappropriateclausebasedonyourdatabasesystem:1.ForMySQL,PostgreSQL,andSQLite,useLIMIT10;2.ForSQLServerandMSAccess,useSELECTTOP10;3.ForstandardSQL,IBMDb2,Oracle12 ,andnewerPostgreSQL,useFETCHFIRST10ROWSONLY;4.ForolderOraclev

See all articles