MS SQL Server第三方事务阻塞监视工具

时间:2013-02-12 13:01:34

标签: sql-server alert database-deadlocks

是否有任何良好的开源或其他免费的MS SQL Server事务阻止监视工具? 可以检测到持续时间超过X的阻塞事务然后通过电子邮件向某个地方发送警报将是理想的。

一台服务器很简单。 对于MS SQL Express 2008,但应该适用于所有真实或最近的所有。

1 个答案:

答案 0 :(得分:6)

是的,实际上SQL Server提供了这样一个开箱即用的选项,但很少有人知道它,甚至更少知道如何使用它。它被称为blocked process threshold

  

使用阻止进程阈值选项指定生成阻止进程报告的阈值(以秒为单位)。阈值可以设置为0到86,400。默认情况下,不会生成阻止的进程报告。不会为系统任务或等待不会生成可检测到的死锁的资源的任务生成此事件。

     

您可以定义在生成此事件时要执行的警报。例如,您可以选择寻呼管理员以采取适当的措施来处理阻止情况。

启用此选项后,系统将生成Blocked Process Report Event Class的探查器事件。下一个难题是可以使用DDL Event Notifications捕获此(以及更多)Profiler事件。这是一个实际的例子:

use msdb;
go

create queue events;
go

create service events on queue [events] (
    [http://schemas.microsoft.com/SQL/Notifications/PostEventNotification]);
go

create event notification [blocked_threshold_exceeded]
    on server for BLOCKED_PROCESS_REPORT
    to service N'events', N'current database';
go

exec sp_configure 'show advanced options', 1;
reconfigure
go


exec sp_configure 'blocked process threshold', 10;
reconfigure
go

-- simulate blocking
use tempdb;
go

begin transaction;
create table x (a int);
go

现在,在另一个会话上,runa查询阻止上面启动的未提交事务:

select * from tempdb..x

果然,在10秒内(我们配置的阈值),我们会收到通知:

use msdb;
receive cast(message_body as xml) from [events];

<EVENT_INSTANCE>
  <EventType>BLOCKED_PROCESS_REPORT</EventType>
  <PostTime>2013-02-12T16:19:55.610</PostTime>
  <SPID>5</SPID>
  <TextData>
    <blocked-process-report monitorLoop="104441">
      <blocked-process>
        <process id="process47b946cf8" 
             waitresource="OBJECT: 2:373576369:0 " 
             waittime="18952" ...>
          <executionStack>
            <frame line="1" stmtstart="-1" ... />
          </executionStack>
          <inputbuf>
select * from x   </inputbuf>
        </process>
      </blocked-process>
      <blocking-process>
        <process status="sleeping" ....>
          <executionStack />
          <inputbuf>
create table x (a int)   </inputbuf>
        </process>
      </blocking-process>
    </blocked-process-report>
  </TextData>
...
</EVENT_INSTANCE>

您可以看到阻止程序最后执行的语句,阻止的当前执行语句,以及等待时间等等。

将活动通知连线以激活使用sp_send_db_mail发送邮件的程序,这是留给读者的练习。是的,上面提到的一切在Express Edition中都可用。

相关问题