zoukankan      html  css  js  c++  java
  • INF: How to Monitor SQL Server 7.0 Blocking [ZT from MS]

    INF: How to Monitor SQL Server 7.0 Blocking

    Article ID : 251004
    Last Review : March 22, 2004
    Revision : 3.0
    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 (http://support.microsoft.com/kb/224453/EN-US/) 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 (http://support.microsoft.com/kb/271509/EN-US/) 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:
    1. 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.
    2. 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
    					
    3. 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 (http://support.microsoft.com/kb/224453/EN-US/) INF: Understanding and Resolving SQL Server 7.0 Blocking Problem
    4. 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 
    	

    APPLIES TO
    Microsoft SQL Server 7.0 Standard Edition

    Back to the top

    Keywords: 
    kbhowto kbinfo kbsqlserv700 KB251004
  • 相关阅读:
    Layui 两个table 人员选择 多选
    iphone7 忘记密码 重装系统
    使用hql-统计连续登陆的三天及以上的用户
    azkaban群起/群停脚本
    Hive读取索引文件问题:select * 和select count(*)读取出来的行数不一致
    scala/java等其他语言从CSV文件中读取数据,使用逗号','分割可能会出现的问题
    hadoop3.1.3版本的secondaryNamenode的web界面不能显示的问题?
    启动kafka消费报错:WARN [Consumer clientId…] 1 partitions have leader brokers without a matching listener,…
    centos6和centos7的防火墙命令,以及它们的区别是是什么?
    kafka项目经验之如何进行Kafka压力测试、如何计算Kafka分区数、如何确定Kaftka集群机器数量
  • 原文地址:https://www.cnblogs.com/liangqihui/p/1083544.html
Copyright © 2011-2022 走看看