Article ID: 251004
Article Last Modified on 3/22/2004
APPLIES TO
- Microsoft SQL Server 7.0 Standard Edition
This article was previously published under Q251004
SUMMARY
This article documents the use and design of a stored procedure that you can use to diagnose blocking and performance issues. For a description of how to understand and resolve blocking, see the following article in the Microsoft Knowledge Base:
224453 INF: Understanding and Resolving SQL Server 7.0 Blocking Problem
For information about these same issues in SQL Server 2000, see the following article in the Microsoft Knowledge Base:
271509 INF: How to Monitor SQL Server 2000 Blocking
MORE INFORMATION
The sp_blocker_pss70 stored procedure, described in this section, captures the following information:
- The starting time (according to the computer that is running SQL Server) so that this sampling of blocking can be time-aligned with other performance information, such as a Microsoft Windows NT Performance Monitor log or a SQL Profiler log.
- Information about connections to SQL Server, by querying the sysprocesses system table.
- Information about lock resources, by querying the syslockinfo system table.
- Information about resource waits, by running DBCC SQLPERF(WAITSTATS).
- The current running SQL Batch for connections that are blocked by others or blocking others, by running DBCC INPUTBUFFER.
- The ending time, according to the computer that is running SQL Server.
The stored procedure was created with the following optimizations to reduce the effect on performance and blocking by running this stored procedure:
- There is no output generated unless at least one connection is waiting on a resource.
- The sysprocesses and syslockinfo system tables from the master database are queried directly, to increase performance and to prevent this stored procedure from becoming blocked. Therefore, this stored procedure is specific to Microsoft SQL Server 7.0.
- A small worktable is created by the cursor to obtain the DBCC INPUTBUFFER output; this will have no noticeable effect on the usage in the tempdb database.
- Because the blocking can change while gathering the information, a fast mode exists, which increases performance by decreasing the results to relevant rows of the sysprocesses and syslockinfo system tables.
- If trying to track non-lock resource waits, a latch mode exists, which causes the lock output to be omitted.
This stored procedure is useful by running it from any query tool. However, Microsoft suggests that you follow these steps to perform blocking analysis:
- Create the stored procedure sp_blocker_pss70, which is included at the end of this article, from any query tool while you are connected with a login that has sysadmin privileges on the SQL Server server or the instance of SQL Server that you plan to monitor.
Create a script file with the following query to run the stored procedure in a loop. Note that the delay is between 5 and 60 seconds:
WHILE 1=1 BEGIN EXEC master.dbo.sp_blocker_pss70 -- Or for fast mode -- EXEC master.dbo.sp_blocker_pss70 @fast=1 -- Or for latch mode -- EXEC master.dbo.sp_blocker_pss70 @latch=1 WAITFOR DELAY '00:00:15' END GO
- This output is very useful when combined with a Microsoft Windows NT Performance Monitor log and a SQL Profiler log, so creating both at the same time is suggested. For information concerning which Profiler and Performance Monitor events to capture, and for information about how to interpret the results, see the following article in the Microsoft Knowledge Base:
224453 INF: Understanding and Resolving SQL Server 7.0 Blocking Problem
Run the script file created in step 2 from either the Isql.exe or Osql.exe query tool from a Windows command prompt on the computer that is running SQL Server that you intend to monitor to prevent networking problems from disconnecting the query tool. Following is an example command line to start Osql.exe, which assumes that the client is run from the computer that is running SQL Server, and that the script file is named Checkblk.sql. Make sure to correct the -S parameter, replace "server" with the name of your SQL Server server, correct the -i parameter, and replace "checkblk.sql" with the path and name to the script file created in step 2.
osql -E -Sserver -icheckblk.sql -ocheckblk.out -w2000
Note that you must use the other command line switches for the following reasons:
- To prevent line wrapping in the output files, which makes it easier to read.
- To send the output to a file, specified with the -o parameter, instead of to the screen so that if the query tool has problems, you still have output until the query tool fails.
Following is the script you can use to create the sp_blocker_pss70 stored procedure:
use master GO if exists (select * from sysobjects where id = object_id('dbo.sp_blocker_pss70') and sysstat & 0xf = 4) drop procedure dbo.sp_blocker_pss70 GO create proc sp_blocker_pss70 (@latch int = 0, @fast int = 1) as --version 13 set nocount on declare @spid varchar(6) declare @blocked varchar(6) declare @time datetime declare @time2 datetime declare @time3 datetime declare @dbname nvarchar(128) declare @IsDetached tinyint declare @IsEmergencyMode tinyint declare @IsInLoad tinyint declare @IsInRecovery tinyint declare @IsNotRecovered tinyint declare @IsOffline tinyint declare @IsShutDown tinyint declare @IsSingleUser tinyint declare @IsSuspect tinyint set @time = getdate() select spid, ecid, blocked, waittype, dbid, 'ignore_app' = case when convert(varchar(128),hostname) = 'PSSDIAG' then 1 else 0 end into #probclients from master..sysprocesses where blocked!=0 or waittype != 0x0000 create unique clustered index pc on #probclients (blocked, spid, ecid) set @time3 = getdate() if exists (select spid from #probclients where ignore_app != 1 or waittype != 0x020B) begin set @time2 = getdate() print '' print '7 Start time: ' + convert(varchar(26), @time, 121) + ' ' + convert(varchar(12), datediff(ms,@time,@time2)) + ' ' + convert(varchar(12), datediff(ms,@time,@time3)) insert #probclients select distinct blocked, 0, 0, 0x0000, 0, 0 from #probclients where blocked not in (select spid from #probclients) and blocked != 0 if (@fast = 1) begin print '' print 'SYSPROCESSES ' + ISNULL (@@servername,'(null)') + ' ' + str(@@microsoftversion) select spid, status, blocked, open_tran, waitresource, waittype, waittime, cmd, lastwaittype, cpu, physical_io, memusage,last_batch=convert(varchar(26), last_batch,121), login_time=convert(varchar(26), login_time,121), net_address, net_library, dbid, ecid, kpid, hostname, hostprocess, loginame, program_name, nt_domain, nt_username, uid, sid from master..sysprocesses where blocked!=0 or waittype != 0x0000 or spid in (select blocked from #probclients where blocked != 0) or spid in (select spid from #probclients where blocked != 0) print 'ESP ' + convert(varchar(12), datediff(ms,@time2,getdate())) print '' print 'SYSPROC FIRST PASS' select spid, ecid, waittype from #probclients where waittype != 0x0000 if exists(select blocked from #probclients where blocked != 0) begin print 'Blocking via locks at ' + convert(varchar(26), @time, 121) print '' print 'SPIDs at the head of blocking chains' select spid from #probclients where blocked = 0 and spid in (select blocked from #probclients where spid != 0) if @latch = 0 begin print 'SYSLOCKINFO' set @time2 = getdate() select spid = convert (smallint, req_spid), ecid = convert (smallint, req_ecid), rsc_dbid As dbid, rsc_objid As ObjId, rsc_indid As IndId, Type = case rsc_type when 1 then 'NUL' when 2 then 'DB' when 3 then 'FIL' when 4 then 'IDX' when 5 then 'TAB' when 6 then 'PAG' when 7 then 'KEY' when 8 then 'EXT' when 9 then 'RID' end, Resource = substring (rsc_text, 1, 16), Mode = case req_mode + 1 when 1 then NULL when 2 then 'Sch-S' when 3 then 'Sch-M' when 4 then 'IS' when 5 then 'SIU' when 6 then 'IS-S' when 7 then 'IX' when 8 then 'SIX' when 9 then 'S' when 10 then 'U' when 11 then 'IIn-Nul' when 12 then 'IS-X' when 13 then 'IU' when 14 then 'IS-U' when 15 then 'X' when 16 then 'BU' end, Status = case req_status when 1 then 'GRANT' when 2 then 'CNVT' when 3 then 'WAIT' end from master.dbo.syslockinfo s, #probclients p where p.spid = s.req_spid print 'ESL ' + convert(varchar(12), datediff(ms,@time2,getdate())) end -- latch not set end -- blocking via locks else print 'No blocking via locks at ' + convert(varchar(26), @time, 121) end -- fast set else begin -- Fast not set print '' print 'SYSPROCESSES ' + ISNULL (@@servername,'(null)') + ' ' + str(@@microsoftversion) select spid, status, blocked, open_tran, waitresource, waittype, waittime, cmd, lastwaittype, cpu, physical_io, memusage,last_batch=convert(varchar(26), last_batch,121), login_time=convert(varchar(26), login_time,121), net_address, net_library, dbid, ecid, kpid, hostname, hostprocess, loginame, program_name, nt_domain, nt_username, uid, sid from master..sysprocesses print 'ESP ' + convert(varchar(12), datediff(ms,@time2,getdate())) print '' print 'SYSPROC FIRST PASS' select spid, ecid, waittype from #probclients where waittype != 0x0000 if exists(select blocked from #probclients where blocked != 0) begin print 'Blocking via locks at ' + convert(varchar(26), @time, 121) print '' print 'SPIDs at the head of blocking chains' select spid from #probclients where blocked = 0 and spid in (select blocked from #probclients where spid != 0) if @latch = 0 begin print 'SYSLOCKINFO' set @time2 = getdate() select spid = convert (smallint, req_spid), ecid = convert (smallint, req_ecid), rsc_dbid As dbid, rsc_objid As ObjId, rsc_indid As IndId, Type = case rsc_type when 1 then 'NUL' when 2 then 'DB' when 3 then 'FIL' when 4 then 'IDX' when 5 then 'TAB' when 6 then 'PAG' when 7 then 'KEY' when 8 then 'EXT' when 9 then 'RID' end, Resource = substring (rsc_text, 1, 16), Mode = case req_mode + 1 when 1 then NULL when 2 then 'Sch-S' when 3 then 'Sch-M' when 4 then 'IS' when 5 then 'SIU' when 6 then 'IS-S' when 7 then 'IX' when 8 then 'SIX' when 9 then 'S' when 10 then 'U' when 11 then 'IIn-Nul' when 12 then 'IS-X' when 13 then 'IU' when 14 then 'IS-U' when 15 then 'X' when 16 then 'BU' end, Status = case req_status when 1 then 'GRANT' when 2 then 'CNVT' when 3 then 'WAIT' end from master.dbo.syslockinfo print 'ESL ' + convert(varchar(12), datediff(ms,@time2,getdate())) end -- latch not set end else print 'No blocking via locks at ' + convert(varchar(26), @time, 121) end -- Fast not set print '' print 'DBCC SQLPERF(WAITSTATS)' dbcc sqlperf(waitstats) print '' print '' Print '*********************************************************************' Print 'Print out DBCC INPUTBUFFER for all blocked or blocking spids.' Print '*********************************************************************' declare ibuffer cursor fast_forward for select cast (spid as varchar(6)) as spid, cast (blocked as varchar(6)) as blocked from #probclients where (spid <> @@spid) and (blocked!=0 or (waittype != 0x0000 and ignore_app = 0) or spid in (select blocked from #probclients where blocked != 0)) open ibuffer fetch next from ibuffer into @spid, @blocked while (@@fetch_status != -1) begin print '' exec ('print ''DBCC INPUTBUFFER FOR SPID ' + @spid + '''') exec ('dbcc inputbuffer (' + @spid + ')') fetch next from ibuffer into @spid, @blocked end deallocate ibuffer Print '' Print '*******************************************************************************' Print 'Print out DBCC OPENTRAN for active databases for all blocked or blocking spids.' Print '*******************************************************************************' declare ibuffer cursor fast_forward for select distinct cast (dbid as varchar(6)) from #probclients where dbid != 0 open ibuffer fetch next from ibuffer into @spid while (@@fetch_status != -1) begin print '' set @dbname = db_name(@spid) select @IsDetached = DATABASEPROPERTY(@dbname,'IsDetached') select @IsEmergencyMode = DATABASEPROPERTY(@dbname,'IsEmergencyMode') select @IsInLoad = DATABASEPROPERTY(@dbname,'IsInLoad') select @IsInRecovery = DATABASEPROPERTY(@dbname,'IsInRecovery') select @IsNotRecovered = DATABASEPROPERTY(@dbname,'IsNotRecovered') select @IsOffline = DATABASEPROPERTY(@dbname,'IsOffline') select @IsShutDown = DATABASEPROPERTY(@dbname,'IsShutDown') select @IsSingleUser = DATABASEPROPERTY(@dbname,'IsSingleUser') select @IsSuspect = DATABASEPROPERTY(@dbname,'IsSuspect') print 'DBCC OPENTRAN FOR DBID ' + @spid + ' ['+ @dbname + ']' if @IsDetached + @IsEmergencyMode + @IsInLoad + @IsInRecovery + @IsNotRecovered + @IsOffline + @IsShutDown + @IsSingleUser + @IsSuspect = 0 dbcc opentran(@dbname) else print 'Skipped: Status=ID=' + str(@IsDetached,1) + ' IEM=' + str(@IsEmergencyMode,1) + ' IIL=' + str(@IsInLoad,1) + ' IIR=' + str(@IsInRecovery,1) + ' INR' + str(@IsNotRecovered,1) + ' IO=' + str(@IsOffline,1) + ' ISD=' + str(@IsShutDown,1) + ' ISU=' + str(@IsSingleUser,1) + ' IS=' + str(@IsSuspect,1) print '' if @spid = '2' select @blocked = 'Y' fetch next from ibuffer into @spid end deallocate ibuffer if @blocked != 'Y' begin print '' print 'DBCC OPENTRAN FOR DBID 2 [tempdb]' dbcc opentran ('tempdb') end print '' print 'End time: ' + convert(varchar(26), getdate(), 121) end -- All else print '7 No Waittypes: ' + convert(varchar(26), @time, 121) + ' ' + convert(varchar(12), datediff(ms,@time,@time3)) + ' ' + ISNULL (@@servername,'(null)') GO
Additional query words: performance
Keywords: kbhowto kbinfo kbsqlserv700 KB251004