--step1: Creating new table
create table prods(pid int,pname varchar(40),qty int)
insert prods values(1,'Pens',450),(2,'Balls',455)
--step2: Take new query and run
begin tran
update prods set qty=8000 where pid=2
--step3: Check the locks information
sp_lock 58
select * from sys.dm_tran_locks where request_session_id=58
--step4: Take new query
select * from prods where pid=2
--step5: Take new query and check
blocking information
sp_who2
select * from sys.sysprocesses where blocked<>0
select * from sys.dm_exec_requests where blocking_session_id<>0
select * from sys.dm_os_waiting_tasks where session_id>50
--To check Head blocker(s) spids
select blocking_session_id as
Head_Blocker
from sys.dm_os_waiting_tasks
where session_id>50 and blocking_task_address is null
group by resource_description,blocking_session_id
--step6: Check the query running in head
blocker
sp_who2 58
dbcc inputbuffer(58)
--step7: Depends on appl team approval we
can wait or kill the session
kill 58
--step8: We can configure WMI alert to
get blocking information
sp_configure 'show adv',1
reconfigure
go
sp_configure 'blocked process threshold (s)',30
reconfigure
--step9: Creating table to capture
blocking info
USE [msdb]
GO
select * into Blocking_Info from sys.sysprocesses where 1=5
go
/****** Object: Job [BlockingInfo] Script Date: 7/12/2016 8:02:10 AM ******/
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode =
0
/****** Object: JobCategory [[Uncategorized (Local)]] Script Date: 7/12/2016 8:02:10 AM ******/
IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'[Uncategorized (Local)]' AND category_class=1)
BEGIN
EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'[Uncategorized (Local)]'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
END
DECLARE @jobId BINARY(16)
EXEC @ReturnCode = msdb.dbo.sp_add_job @job_name=N'BlockingInfo',
@enabled=1,
@notify_level_eventlog=0,
@notify_level_email=0,
@notify_level_netsend=0,
@notify_level_page=0,
@delete_level=0,
@description=N'No description available.',
@category_name=N'[Uncategorized (Local)]',
@owner_login_name=N'sa', @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object: Step [Blocking] Script Date: 7/12/2016 8:02:10 AM ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Blocking',
@step_id=1,
@cmdexec_success_code=0,
@on_success_action=1,
@on_success_step_id=0,
@on_fail_action=2,
@on_fail_step_id=0,
@retry_attempts=0,
@retry_interval=0,
@os_run_priority=0, @subsystem=N'TSQL',
@command=N'INSERT INTO Blocking_info
SELECT * FROM SYS.SYSPROCESSES WHERE BLOCKED<>0',
@database_name=N'msdb',
@flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:
GO
--step10: Creating alert to call the job
USE [msdb]
GO
/****** Object: Alert [Blocking_Capture] Script Date: 7/12/2016 8:01:43 AM ******/
EXEC msdb.dbo.sp_add_alert @name=N'Blocking_Capture',
@message_id=0,
@severity=0,
@enabled=1,
@delay_between_responses=0,
@include_event_description_in=1,
@category_name=N'[Uncategorized]',
@wmi_namespace=N'\\.\root\Microsoft\SqlServer\ServerEvents\LIC',
@wmi_query=N'SELECT * FROM
BLOCKED_PROCESS_REPORT',
@job_name=N'BlockingInfo'
GO
--step11: After 30 sec check the alert
history
--step12: Verify the information from
Blocking_Info
select * from Blocking_Info
--step13: Using Report (All Blocking
Transactions)/Performance Dashboard
No comments
Post a Comment
Note: Only a member of this blog may post a comment.