user1 running a command on table1 which is still executing at the same time other user try to read/update the data from/to same table then user1 query will block the user2 Query.
Step1: Find the Blocking
There are number of ways to find out the details of the system processes IDs (spids) involved in blocking.
1. sp_who2 System Stored Procedure
2. sys.dm_exec_requests DMV
5. SQL Server Management Studio Activity Monitor
6. SQL Server Management Studio Reports
7. SQL Server Profiler
1.sp_who2 System Stored Procedure
The sp_who2 system stored procedure provides information about the current SQL Server processes with the associated SPID, status,login,Hostname,BlkBy,DBName,Command,CPU time, etc. The information returned can be filtered to return only the active processes by using the ‘active' parameter.
Below is some sample code and a screen shot with showing process 54 being blocked by process 53.
2. sys.dm_exec_requests DMV
The sys.dm_exec_requests DMV provides details on all of the processes running in SQL Server. With the WHERE condition listed below, only blocked processes will be returned.
select * from sys.dm_exec_requests
The sys.sysprocesses provides details on all of the processes running in SQL Server. With the WHERE condition listed below, only blocked processes will be returned.
Select * from sys.sysprocesses where blocked<>0
The sys.dm_os_waiting_tasks DMV returns information about the tasks that are waiting on resources.
SELECT session_id, wait_duration_ms, wait_type, blocking_session_id
WHERE blocking_session_id <> 0
5.SQL Server Management Studio Activity Monitor
It is easy procedure to do find the blocking from SSMS.
Open SSMS->Connect instance->Right click on instance-> select 'Activity Monitor'->Processes
6.SQL Server Management Studio Reports
We can monitor the blocking on standard reports.
Open SSMS->Connect instance->Right click on instance-> Reports -> Standard Reports-> Activity - All Blocking Transactions.
7.SQL Server Profiler
To capture blocking related data on a continuous basis, one option is to run SQL Server Profiler and save the data to a table or file for analysis purposes. In order to configure Profiler to capture blocking related data, execute Profiler, configure the general properties. In addition, be sure to configure the 'blocked process threshold' before you start Profiler using this code:
sp_configure 'show advanced options', 1
sp_configure 'blocked process threshold', 20
Menu Bar->Tools->SQL Profiler->Connect the Instance->Select use template as TSQL-Lock->click on column filter->Select Database->Give the database name->ok->Run
Note: Don’t do this process in business hours. It causes a performance issue. Without change we can’t run the profiler.
Find the command which is running under SPID by using DBCCDBCC INPUTBUFFER(53)
Kill the transaction by using below command
1.If select then kill the SPID ( Select command is not modify the data)
2. There is any other command part of the blocking then we can take the user/application team approval then we will kill it
SQL and Azure DBA Trainer
Microsoft & Azure SQL DBA certified.