Question : SQL Server Blocking

I am looking for your best MSSQL scripts and techniques for troubleshooting query blocking.  I am looking for something a little more substantial then "select * from sysprocess where blocked !=0".  Please post actual scripts and techniques.  I have read all the technet literature and want some case studies for how other people have resolved issues.

Links to good articles would be appreciated also.  SQL Server Profiler configurations would be accepted too. I am really looking for good troubleshooting scripts here, I want to develop a "best practices" manual.  The person who simply copies and pastes a sophisticated script with some brief notes regarding their experience with it will most likely receive the points.

Thanks in Advance,

Integer

Answer : SQL Server Blocking

I configure a job that runs an sp every two minutes (you can do whatever) that checks for blocking. If there is a problem, the sp sends an email with info on the block including the inputbuffer of what caused it. I keep these sps in a resource db called "Admin."

You might think running a job every two minutes will not catch everything, but over time, on a busy environment, you will indeed understand what is causing your problems.

You can also setup an alert type "SQL Server event alert" for severity 016 - Miscellaneous User Errors since blocking/deadlocks are considered user errors. You can have this email you each time a 016 occurs.

CREATE procedure Admin_monitor_deadlock
as
set nocount on
declare @ServerName      varchar (20)
declare @DeadlockCount      int
declare @Message       varchar (400)
declare @Subject       varchar (400)
declare @Query             varchar (800)
declare @Spid             varchar (10)
declare @Recipients       varchar (40)
declare @SendEMail       varchar (3)

select @ServerName = @@servername
set @SendEmail = 'NO'
set @Message = ''

--enter email address:
set @Recipients ='[email protected]'

      if not exists (select * from master.dbo.sysmessages where error =60001)
      exec sp_addmessage 60001, 16,
       'User error: blocking or deadlock occurred. ServerName:%s SPID:%s'
            
      if exists ( select * from INFORMATION_SCHEMA.TABLES
                  where table_name = 'Admin_blk' )
      drop table Admin.dbo.Admin_blk

      select       spid,
            blocked,
            hostname=substring (hostname, 1, 10),
            progname=substring(program_name, 1, 10),
            cmd=substring(cmd, 1, 100),
            status,
            physical_io,
            waittype
      into       admin.dbo.Admin_blk
      from       master..sysprocesses (nolock)
      where       blocked != 0
      
      delete from admin.dbo.Admin_blk
      where blocked in (select spid from admin.dbo.Admin_blk)
      
      select       @DeadlockCount=count(*)
      from       master..sysprocesses (nolock)
      where       spid in (select blocked from admin.dbo.Admin_blk)

--print @DeadlockCount


if (@DeadlockCount > 0)
begin
            
      select getdate()
      select @ServerName=@@servername
      select @Spid=spid  
      from       master..sysprocesses (nolock)
      where       spid in (select blocked from admin.dbo.Admin_blk)

      raiserror(60001, 16, 1, @serverName, @spid) with log
      
      set @Query ='
            select       Blocking_spid = spid,
            loginame=substring(loginame,1,10),
             hostname=substring (hostname, 1, 10),
            progname=substring(program_name, 1,10),
            cmd=substring(cmd, 1, 100),
            status, physical_io,
            waittype
            from       master..sysprocesses (nolock)
            where       spid in (select blocked from admin.dbo.Admin_blk)
            '
      exec (@Query)

      if (@SendEmail = 'YES')
            begin
            set @Subject = @ServerName + ': Deadlock '
                  exec master..xp_sendmail  
                          @recipients            = @Recipients,
                        @message            = @Message,
                        @subject            = @Subject,
                        @query                  = @Query
            end

      set @Query = 'dbcc inputbuffer ('+@Spid+')'
      exec (@Query)

      if (@SendEmail = 'YES')
      begin

      set @Subject = @ServerName+ ': Deadlock Input buffer on ' +@ServerName
            exec master..xp_sendmail  
                    @recipients            = @Recipients,
                  @message            = @Message,
                  @subject            = @Subject,
                  @query                  = @Query
      end

      set @Query ='exec master..sp_lock'      
      exec (@Query)
      if @sendEMail ='YES'
      begin
            exec master..xp_sendmail  
                    @recipients            = @Recipients,
                  @message            = @Message,
                  @subject            = @Subject,
                  @query                  = @Query
            set @Subject = @ServerName+': Deadlock Input buffer'
      end
end





GO
Random Solutions  
 
programming4us programming4us