0% found this document useful (0 votes)
11 views2 pages

To Find Blocking

The document provides instructions on identifying and managing blocking processes in SQL Server, including queries to find blocked sessions and the blocking query. It emphasizes the importance of not killing certain commands like select into, insert, update, and delete. Additionally, it includes scripts for viewing current processes and killing multiple sessions if necessary.

Uploaded by

www.greeny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

To Find Blocking

The document provides instructions on identifying and managing blocking processes in SQL Server, including queries to find blocked sessions and the blocking query. It emphasizes the importance of not killing certain commands like select into, insert, update, and delete. Additionally, it includes scripts for viewing current processes and killing multiple sessions if necessary.

Uploaded by

www.greeny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

How to identify Blockings:


--> select *from sys.sysprocesses where blocked <>0 ----From here
-->sp_who or sp_who2 SPID
-->sp_whoisactive
-->DBCC Inputbuffer(SPID) - To know the blocking query

2. How to kill the transaction.


--> Kill SPID

Note: Don`t kill - select into, Insert/update and Delete copmmands never and ever.

SPID-1 to 50 (System processes - Background)


SPID-51 to ....(User db or server processes.)

SELECT blocking_session_id ,*
FROM sys.dm_exec_requests
WHERE blocking_session_id <> 0;
GO

---Script to Find All the Blocked Processes:

SELECT
spid,
status,
loginame=SUBSTRING(loginame,1,12),
hostname=SUBSTRING(hostname,1, 12),
blk = CONVERT(char(3), blocked),
dbname=SUBSTRING(DB_NAME(dbid),1, 10),
cmd,
waittype
FROM master.dbo.sysprocesses
WHERE spid IN (SELECT blocked FROM master.dbo.sysprocesses)

-----Script to Identify the blocking query:

SELECT
db.name DBName,
tl.request_session_id,
wt.blocking_session_id,
OBJECT_NAME(p.OBJECT_ID) BlockedObjectName,
tl.resource_type,
h1.TEXT AS RequestingText,
h2.TEXT AS BlockingTest,
tl.request_mode
FROM sys.dm_tran_locks AS tl
INNER JOIN sys.databases db ON db.database_id = tl.resource_database_id
INNER JOIN sys.dm_os_waiting_tasks AS wt ON tl.lock_owner_address
=wt.resource_address
INNER JOIN sys.partitions AS p ON p.hobt_id =tl.resource_associated_entity_id
INNER JOIN sys.dm_exec_connections ec1 ON ec1.session_id =tl.request_session_id
INNER JOIN sys.dm_exec_connections ec2 ON ec2.session_id =wt.blocking_session_id
CROSS APPLY sys.dm_exec_sql_text(ec1.most_recent_sql_handle) AS h1
CROSS APPLY sys.dm_exec_sql_text(ec2.most_recent_sql_handle) AS h2
GO

---- Script to view all current processes / sessions on the server:

select * from master.dbo.sysprocesses


--------------------------------------
SELECT
db.name DBName,
tl.request_session_id,
wt.blocking_session_id,
OBJECT_NAME(p.OBJECT_ID) BlockedObjectName,
tl.resource_type,
h1.TEXT AS RequestingText,
h2.TEXT AS BlockingTest,
tl.request_mode
FROM sys.dm_tran_locks AS tl
INNER JOIN sys.databases db ON db.database_id = tl.resource_database_id
INNER JOIN sys.dm_os_waiting_tasks AS wt ON tl.lock_owner_address =
wt.resource_address
INNER JOIN sys.partitions AS p ON p.hobt_id = tl.resource_associated_entity_id
INNER JOIN sys.dm_exec_connections ec1 ON ec1.session_id = tl.request_session_id
INNER JOIN sys.dm_exec_connections ec2 ON ec2.session_id = wt.blocking_session_id
CROSS APPLY sys.dm_exec_sql_text(ec1.most_recent_sql_handle) AS h1
CROSS APPLY sys.dm_exec_sql_text(ec2.most_recent_sql_handle) AS h2
GO

----------------------------------------------------------
-- How to kill multiple sessions
--SELECT 'KILL ' + CAST(session_id as varchar(100)) AS Sessions_to_kill
--FROM sys.dm_exec_requests where session_id in (54,57,58)
--GO

You might also like