原文:SqlServer 监控发布中未分发的命令数

对于查看未分发的命令数,我们通常这样查看。

然而当服务器有很多发布时,一个个打开查看就很麻烦

当然,如果想用脚本查看就更方便了,运行下面的语句

--查看各发布订阅未分发的命令数和估计时间
SELECT 'EXEC distribution.sys.sp_replmonitorsubscriptionpendingcmds @publisher = N'''
+ a.publisher + ''', @publisher_db = N''' + a.publisher_db
+ ''', @publication = N''' + a.publication + ''', @subscriber = N'''
+ c.name + ''', @subscriber_db = N''' + b.subscriber_db
+ ''', @subscription_type =' + CAST(b.subscription_type AS VARCHAR)
FROM distribution.dbo.MSreplication_monitordata a ( NOLOCK )
INNER JOIN (
SELECT publication_id ,subscriber_id ,subscriber_db ,subscription_type
FROM distribution.dbo.MSsubscriptions (NOLOCK)
GROUP BY publication_id ,subscriber_id ,subscriber_db ,subscription_type
) b ON a.publication_id = b.publication_id
INNER JOIN sys.servers c ( NOLOCK ) ON b.subscriber_id = c.server_id
WHERE a.agent_type = 1

上面自动生成各个发布 未分发的命令的存储过程,执行就能查看各个发布等待分发的命令和估计时间:

EXEC distribution.sys.sp_replmonitorsubscriptionpendingcmds
@publisher = N'发布服务器'
, @publisher_db = N'发布数据库'
, @publication = N'发布名称'
, @subscriber = N'订阅服务器'
, @subscriber_db = N'订阅数据库'
, @subscription_type =0

/***************************************************************************/
/***************************************************************************/

但是,由于监控需要,只想统计所有发布未分发的命令数“ pendingcmdcount”的总和,

本来想用下面这个方法把各个存储过程的返回值插入到临时表,但是没有这种方法!!

CREATE TABLE #countab(pendingcmdcount int,estimatedprocesstime int)

INSERT INTO #countab(pendingcmdcount,estimatedprocesstime)
EXEC distribution.sys.sp_replmonitorsubscriptionpendingcmds
@publisher = N'发布服务器'
, @publisher_db = N'发布数据库'
, @publication = N'发布名称'
, @subscriber = N'订阅服务器'
, @subscriber_db = N'订阅数据库'
, @subscription_type =0

上面这个方法也可行,但是只在本地的测试可行。就是创建链接服务器,加到数据库distribution前面。

实际服务器有本地的链接服务器,但是没显示出来,也用不了。虽然可以创建其他命名的,但不想创建太多链接服务器。

于是想到更改系统的存储过程!!~错了,是参考系统存储过程创建一个几乎一样定义的存储过程。

即不需要distribution.sys.sp_replmonitorsubscriptionpendingcmds ,创建类似的dbo.sp_getsubscriptionpendingcmds ,如下

create procedure dbo.sp_getsubscriptionpendingcmds
(
@publisher sysname -- cannot be null
,@publisher_db sysname -- cannot be null
,@publication sysname -- cannot be null
,@subscriber sysname -- cannot be null
,@subscriber_db sysname -- cannot be null
,@subscription_type int
,@pendingcmdcount int output
)
as
begin
set nocount on
declare @retcode int
,@agent_id int
,@publisher_id int
,@subscriber_id int
,@lastrunts timestamp
,@xact_seqno varbinary(16) ,@inactive int = 1
,@virtual int = -1 select @publisher_id = server_id from sys.servers where upper(name) = upper(@publisher)
select @subscriber_id = server_id from sys.servers where upper(name) = upper(@subscriber) select @agent_id = id
from distribution.dbo.MSdistribution_agents
where publisher_id = @publisher_id
and publisher_db = @publisher_db
and publication in (@publication, 'ALL')
and subscriber_id = @subscriber_id
and subscriber_db = @subscriber_db
and subscription_type = @subscription_type ;with dist_sessions (start_time, runstatus, timestamp)
as(
select start_time, max(runstatus), max(timestamp)
from distribution.dbo.MSdistribution_history
where agent_id = @agent_id
group by start_time
)
select @lastrunts = max(timestamp) from dist_sessions where runstatus in (2,3,4); if (@lastrunts is null)
begin
if exists (
select * from distribution.dbo.MSpublications p
inner join distribution.dbo.MSsubscriptions s on p.publication_id = s.publication_id
where p.publisher_id = @publisher_id
and p.publisher_db = @publisher_db
and p.publication = @publication
and p.immediate_sync = 1
and s.status = @inactive and s.subscriber_id = @virtual)
begin
select 'pendingcmdcount' = 0, N'estimatedprocesstime' = 0
return 0
end select @lastrunts = max(timestamp) from distribution.dbo.MSdistribution_history where agent_id = @agent_id
end select @xact_seqno = xact_seqno from distribution.dbo.MSdistribution_history where agent_id = @agent_id and timestamp = @lastrunts select @xact_seqno = isnull(@xact_seqno, 0x0) DECLARE @countab TABLE ( pendingcmdcount int )
insert into @countab (pendingcmdcount)
exec @retcode = distribution.sys.sp_MSget_repl_commands
@agent_id = @agent_id
,@last_xact_seqno = @xact_seqno
,@get_count = 2
,@compatibility_level = 9000000
if (@retcode != 0 or @@error != 0)
return 1 select @pendingcmdcount=pendingcmdcount from @countab return @pendingcmdcount
end

上面的存储过程主要更改3个地方,然后在其他一个数据库运行,页可在distribution运行。

1. 多一个返回参数,@pendingcmdcount int output ,返回未分发的命令数

2. 估计时间不需要,去掉

3. 查询的每个表或内部存储过程加数据库前缀distribution



接下来再用一个存储过程把所有值加起来,给计数器!

新建一个作业,定时每分钟执行,观察性能计数器统计情况。

这样也可以再cacti捕获了!~

SqlServer 监控发布中未分发的命令数的更多相关文章

  1. Sysstat性能监控工具包中20个实用命令

    Sysstat性能监控工具包中20个实用命令 学习mpstat, pidstat, iostat和sar等工具,这些工具可以帮组我们找出系统中的问题.这些工具都包含了不同的选项,这意味着你可以根据不同 ...

  2. SQLServer 使用sp_repldone标识所有未分发的事务为已分发

    原文:SQLServer 使用sp_repldone标识所有未分发的事务为已分发 对于发布数据库的数据大量操作时,会使日志扫描并读取太多,会导致分发堵塞很久.也有一些解决方法,参考 <SqlSe ...

  3. Replication--查看未分发命令和预估所需时间

    当复制有延迟时,我们可以使用复制监视器来查看各订阅的未分发命令书和预估所需时间,如下图: 但是当分发和订阅数比较多的时候,依次查看比较费时,我们可以使用sys.sp_replmonitorsubscr ...

  4. SQLServer 事务复制中使用脚本添加某个对象的发布

    原文:SQLServer 事务复制中使用脚本添加某个对象的发布 -- use [发布库] --添加表:创建项目并将其添加到发布中 exec sp_addarticle @publication = N ...

  5. SQLSERVER监控复制并使用数据库邮件功能发告警邮件

    SQLSERVER监控复制并使用数据库邮件功能发告警邮件 最近熬出病来了,都说IT行业伤不起,不说了,说回今天的正题 正题 上个月月底的时候因为要搬迁机房,需要将一个数据信息数据库先搬到我们的机房,然 ...

  6. sqlserver关于发布订阅replication_subscription的总结

    (转载)sqlserver关于发布订阅replication_subscription的总结 来自 “ ITPUB博客 ” ,原文地址:http://blog.itpub.net/30126024/v ...

  7. 监控Linux性能的18个命令行工具

    监控 Linux 性能的 18 个命令行工具 对于系统和网络管理员来说每天监控和调试Linux系统的性能问题是一项繁重的工作.在IT领域作为一名Linux系统的管理员工作5年后,我逐渐 认识到监控和保 ...

  8. 使用MySQL中的EXPLAIN解释命令来检查SQL

    我们看到许多客户的系统因为SQL及数据库设计的很差所以导致许多性能上的问题,这些问题不好解决,但是可以采用一套简单的策略来检查生产系统,发现并纠正一些共性问题. 很显然,您应该尽最大努力设计出最好的数 ...

  9. iOS应用发布中的一些细节

    iOS应用发布中的一些细节 前言 这几天最大的新闻我想就是巴黎恐怖袭击了,诶,博主每年跨年都那么虔诚地许下“希望世界和平”的愿望,想不到每年都无法实现,维护世界和平这么难,博主真是有心无力啊,其实芸芸 ...

随机推荐

  1. 【hdu 4315】Climbing the Hill

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...

  2. [Ramda] Filter an Array Based on Multiple Predicates with Ramda's allPass Function

    In this lesson, we'll filter a list of objects based on multiple conditions and we'll use Ramda's al ...

  3. javascript中隐藏显示的样式表属性

    display属性 隐藏不占据位置 visibility属性 隐藏占据位置 //使用display的样式属性 隐藏 显示 //隐藏后不占据文档流位置 function showAddForm(){ v ...

  4. hudson添加批处理编译命令的注意事项

    作者:朱金灿 来源:http://blog.csdn.net/clever101 Hudson可以在Build Step里添加自定义的批处理命令,如下图: 需要注意的是这些批处理命令并不能识别huds ...

  5. Android中使用JUnit测试

    package com.meritit.lottery.test; import java.util.List; import android.test.AndroidTestCase; import ...

  6. linux awk(good)

    一个用awk处理字符串的例子: #!/bin/bash source="nokia201703148855" preffixStr=$(echo $source |awk '{pr ...

  7. Qt控件焦点切换

    们日常切换控件,例如QQ登陆的账号和密码输入框就可以通过Tab键切换焦点.  图1 qq切换焦点 Qt中QWidget提供了一个静态方式实现该效果 其中也包含介绍使用 [static] void QW ...

  8. 比较实时分布式搜索引擎(senseidb、Solr、elasticsearch)

    1.它们是基于lucene的. 2.它们分布:sensedb它是multi-write;Solr的shards它是master-slave状态.基于pull策略:elasticsearch的shard ...

  9. PBS 作业调度应用

    PBS(Portable Batch System),最初由 NASA 的 Ames 研究中心开发,主要为了提供一个能满足异构计算网络需要的软件包,用于灵活的批处理(Portable Batch Pr ...

  10. Apache Cordova开发环境搭建(二)VS Code

    原文:Apache Cordova开发环境搭建(二)VS Code 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u011127019/articl ...