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

当复制有延迟时,我们可以使用复制监视器来查看各订阅的未分发命令书和预估所需时间,如下图:

但是当分发和订阅数比较多的时候,依次查看比较费时,我们可以使用sys.sp_replmonitorsubscriptionpendingcmds来查看,但是该命令需要输入多个参数,也比较累人,后从菠萝兄哪找寻得一个脚本,对该命令进行了一次封装:

--在分发服务器执行 
USE distribution 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    dbo.MSreplication_monitordata a ( NOLOCK )JOIN ( SELECT   publication_id ,subscriber_id ,subscriber_db ,subscription_typeFROM     MSsubscriptions (NOLOCK)GROUP BY publication_id ,subscriber_id ,subscriber_db ,subscription_type) b ON a.publication_id = b.publication_idJOIN sys.servers c ( NOLOCK ) ON b.subscriber_id = c.server_id
WHERE   a.agent_type = 1

执行该脚本,可以生成相应命令,再依次执行命令可以获取我们想要的结果。

为方便查看,我在菠萝的脚本上做了改进,以便可以更方便查看:

--查看为传递到订阅的命令和预估时间
--在分发服务器执行 
IF(OBJECT_ID('tempdb..#tmpSubscribers') IS NOT NULL)
BEGIN
DROP TABLE #tmpSubscribers
END
GO
--IF(OBJECT_ID('tempdb..#tmpPendingResult') IS NOT NULL)
--BEGIN
--DROP TABLE #tmpPendingResult
--END--GO
--IF(OBJECT_ID('tempdb..#tmpSinglePendingResult') IS NOT NULL)
--BEGIN
--DROP TABLE #tmpSinglePendingResult
--END
GO
USE distribution 
GO
SELECT  
a.publisher
,a.publisher_db
,a.publication
,c.name as subscriber
,b.subscriber_db as subscriber_db
,CAST(b.subscription_type AS VARCHAR) as subscription_type
,'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) AS ScriptTxt
INTO #tmpSubscribers
FROM    dbo.MSreplication_monitordata a ( NOLOCK )JOIN ( SELECT   publication_id ,subscriber_id ,subscriber_db ,subscription_typeFROM     MSsubscriptions (NOLOCK)GROUP BY publication_id ,subscriber_id ,subscriber_db ,subscription_type) b ON a.publication_id = b.publication_idJOIN sys.servers c ( NOLOCK ) ON b.subscriber_id = c.server_id
WHERE   a.agent_type = 1
--====================================================
--CREATE TABLE #tmpPendingResult
--(
--publisher NVARCHAR(200)
--,publisher_db NVARCHAR(200)
--,publication NVARCHAR(200)
--,subscriber NVARCHAR(200)
--,subscriber_db NVARCHAR(200)
--,subscription_type NVARCHAR(200)
--,pendingcmdcount BIGINT
--,estimatedprocesstime BIGINT
--)--CREATE TABLE #tmpSinglePendingResult
--(
--pendingcmdcount BIGINT
--,estimatedprocesstime BIGINT
--)--==================================================
--使用游标遍历
DECLARE @publisher NVARCHAR(200);;
DECLARE @publisher_db NVARCHAR(200);
DECLARE @publication NVARCHAR(200);DECLARE @subscriber NVARCHAR(200);;
DECLARE @subscriber_db NVARCHAR(200);
DECLARE @subscription_type NVARCHAR(200);
DECLARE @ScriptTxt NVARCHAR(MAX);DECLARE MyCursor CURSOR FOR
SELECT publisher
,publisher_db 
,publication
,subscriber
,subscriber_db
,subscription_type
,ScriptTxt
FROM #tmpSubscribers;OPEN MyCursorFETCH NEXT FROM MyCursor 
INTO @publisher
,@publisher_db 
,@publication
,@subscriber
,@subscriber_db
,@subscription_type
,@ScriptTxt;WHILE @@FETCH_STATUS = 0
BEGIN
SELECT 
@publisher AS publisher
,@publisher_db AS publisher_db
,@publication AS publication
,@subscriber AS subscriber
,@subscriber_db AS subscriber_db
,@subscription_type AS subscription_type
,@ScriptTxt;EXEC(@ScriptTxt)FETCH NEXT FROM MyCursor 
INTO @publisher
,@publisher_db 
,@publication
,@subscriber
,@subscriber_db
,@subscription_type
,@ScriptTxt;ENDCLOSE MyCursor
DEALLOCATE MyCursor

由于使用sp_replmonitorsubscriptionpendingcmds,无法将存储过程的结果插入到一个临时表中查看,因此想到参考其存储过程,编写一个类似脚本,于是有了下面脚本:

USE distribution
go
IF ( OBJECT_ID('dbo.sp_replmonitorsubscriptionpendingcmds_EX ') IS NOT NULL ) BEGINDROP PROCEDURE dbo.sp_replmonitorsubscriptionpendingcmds_EX END
GO
CREATE PROCEDURE dbo.sp_replmonitorsubscriptionpendingcmds_EX
AS BEGINSET nocount ONCREATE TABLE #tmpPendingResult(publisher NVARCHAR(200) ,publisher_db NVARCHAR(200) ,publication NVARCHAR(200) ,subscriber NVARCHAR(200) ,subscriber_db NVARCHAR(200) ,subscription_type NVARCHAR(200) ,pendingcmdcount BIGINT ,estimatedprocesstime BIGINT)--查找所有订阅SELECT  a.publisher ,a.publisher_db ,a.publication ,c.name AS subscriber ,b.subscriber_db AS subscriber_db ,CAST(b.subscription_type AS VARCHAR) AS subscription_typeINTO    #tmpSubscribersFROM    dbo.MSreplication_monitordata a ( NOLOCK )JOIN ( SELECT   publication_id ,subscriber_id ,subscriber_db ,subscription_typeFROM     MSsubscriptions (NOLOCK)GROUP BY publication_id ,subscriber_id ,subscriber_db ,subscription_type) b ON a.publication_id = b.publication_idJOIN sys.servers c ( NOLOCK ) ON b.subscriber_id = c.server_idWHERE   a.agent_type = 1DECLARE @count INTSELECT  @count = COUNT(1)FROM    #tmpSubscribersPRINT 'Subscriber Counter:' + CAST(@count AS VARCHAR(200));DECLARE @publisher NVARCHAR(200);;DECLARE @publisher_db NVARCHAR(200);DECLARE @publication NVARCHAR(200);DECLARE @subscriber NVARCHAR(200);;DECLARE @subscriber_db NVARCHAR(200);DECLARE @subscription_type NVARCHAR(200);DECLARE MyCursor CURSORFORSELECT  publisher ,publisher_db ,publication ,subscriber ,subscriber_db ,subscription_typeFROM    #tmpSubscribers;OPEN MyCursorFETCH NEXT FROM MyCursor 
INTO @publisher, @publisher_db, @publication, @subscriber, @subscriber_db,@subscription_type;DECLARE @Error NVARCHAR(MAX)WHILE @@FETCH_STATUS = 0 BEGINSELECT  @Error = '@publisher=' + @publisher+ ';@publisher_db=' + @publisher_db + ';@publication='+ @publication + ';@subscriber=' + @subscriber+ ';@subscriber_db' + @subscriber_dbPRINT '开始:' + @Error;DECLARE @retcode INT ,@agent_id INT ,@publisher_id INT ,@subscriber_id INT ,@lastrunts TIMESTAMP ,@avg_rate FLOAT ,@xact_seqno VARBINARY(16) ,@inactive INT = 1 ,@virtual INT = -1--
    -- PAL security check done inside sp_MSget_repl_commands-- security: Has to be executed from distribution database--
  --  if sys.fn_MSrepl_isdistdb (db_name()) != 1--  begin--      --raiserror (21482, 16, -1, 'sp_replmonitorsubscriptionpendingcmds', 'distribution')--      --return 1--SELECT  @Error='@publisher='+@publisher+';@publisher_db='+@publisher_db--+';@publication='+@publication+';@subscriber='+@subscriber+';@subscriber_db'+@subscriber_db--PRINT @Error--CONTINUE;--  end--
    -- validate @subscription_type--
                IF ( @subscription_type NOT IN ( 0, 1 ) ) BEGIN--raiserror(14200, 16, 3, '@subscription_type')--return 1PRINT 'ERROR IN subscription_type'CONTINUE;END--
    -- get the server ids for publisher and subscriber--
                SELECT  @publisher_id = server_idFROM    sys.serversWHERE   UPPER(name) = UPPER(@publisher)IF ( @publisher_id IS NULL ) BEGIN--raiserror(21618, 16, -1, @publisher)--return 1PRINT 'ERROR IN publisher_id'CONTINUE;ENDSELECT  @subscriber_id = server_idFROM    sys.serversWHERE   UPPER(name) = UPPER(@subscriber)IF ( @subscriber_id IS NULL ) BEGIN--raiserror(20032, 16, -1, @subscriber, @publisher)--return 1PRINT 'ERROR IN subscriber_id'CONTINUE;END--
    -- get the agent id--
                SELECT  @agent_id = idFROM    dbo.MSdistribution_agentsWHERE   publisher_id = @publisher_idAND publisher_db = @publisher_dbAND publication IN ( @publication, 'ALL' )AND subscriber_id = @subscriber_idAND subscriber_db = @subscriber_dbAND subscription_type = @subscription_typeIF ( @agent_id IS NULL ) BEGIN--raiserror(14055, 16, -1)--return (1)PRINT 'ERROR IN agent_id'CONTINUE;END;--
    -- Compute timestamp for latest run--
                WITH    dist_sessions ( start_time, runstatus, timestamp )AS ( SELECT   start_time ,MAX(runstatus) ,MAX(timestamp)FROM     dbo.MSdistribution_historyWHERE    agent_id = @agent_idAND runstatus IN ( 2, 3, 4 )GROUP BY start_time)SELECT  @lastrunts = MAX(timestamp)FROM    dist_sessions;IF ( @lastrunts IS NULL ) BEGIN--
        -- Distribution agent has not run successfully even once-- and virtual subscription of immediate sync publication is inactive (snapshot has not run), no point of returning any counts-- see SQLBU#320752, orig fix SD#881433, and regression bug VSTS# 140179 before you attempt to fix it differently :)IF EXISTS ( SELECT  *FROM    dbo.MSpublications pJOIN dbo.MSsubscriptions s ON p.publication_id = s.publication_idWHERE   p.publisher_id = @publisher_idAND p.publisher_db = @publisher_dbAND p.publication = @publicationAND p.immediate_sync = 1AND s.status = @inactiveAND s.subscriber_id = @virtual ) BEGIN--   select 'pendingcmdcount' = 0, N'estimatedprocesstime' = 0--return 0INSERT  INTO #tmpPendingResult( publisher ,publisher_db ,publication ,subscriber ,subscriber_db ,subscription_type ,pendingcmdcount ,estimatedprocesstime)SELECT  @publisher ,@publisher_db ,@publication ,@subscriber ,@subscriber_db ,@subscription_type ,0 ,0END--
        -- Grab the max timestamp--
                        SELECT  @lastrunts = MAX(timestamp)FROM    dbo.MSdistribution_historyWHERE   agent_id = @agent_idEND--
    -- get delivery rate for the latest completed run-- get the latest sequence number--
                SELECT  @xact_seqno = xact_seqno ,@avg_rate = delivery_rateFROM    dbo.MSdistribution_historyWHERE   agent_id = @agent_idAND timestamp = @lastrunts--
    -- if no rows are selected in last query-- explicitly initialize these variables--
                SELECT  @xact_seqno = ISNULL(@xact_seqno, 0x0) ,@avg_rate = ISNULL(@avg_rate, 0.0)--
    -- if we do not have completed run-- get the average for the agent in all runs--
                IF ( @avg_rate = 0.0 ) BEGINSELECT  @avg_rate = ISNULL(AVG(delivery_rate), 0.0)FROM    dbo.MSdistribution_historyWHERE   agent_id = @agent_idEND--
    -- get the count of undelivered commands-- PAL check done inside--
                DECLARE @countab TABLE ( pendingcmdcount INT )INSERT  INTO @countab( pendingcmdcount)EXEC @retcode = sys.sp_MSget_repl_commands @agent_id = @agent_id,@last_xact_seqno = @xact_seqno, @get_count = 2,@compatibility_level = 9000000IF ( @retcode != 0OR @@error != 0)--return 1CONTINUE;--
    -- compute the time to process-- return the resultset--
                INSERT  INTO #tmpPendingResult( publisher ,publisher_db ,publication ,subscriber ,subscriber_db ,subscription_type ,pendingcmdcount ,estimatedprocesstime)SELECT  @publisher ,@publisher_db ,@publication ,@subscriber ,@subscriber_db ,@subscription_type ,pendingcmdcount ,CASE WHEN ( @avg_rate != 0.0 )THEN CAST(( CAST(pendingcmdcount AS FLOAT)/ @avg_rate ) AS INT)ELSE pendingcmdcountENDFROM    @countab--
    -- all done--
    --CONTINUE;FETCH NEXT FROM MyCursor 
INTO @publisher, @publisher_db, @publication, @subscriber, @subscriber_db,@subscription_type;ENDCLOSE MyCursorDEALLOCATE MyCursorSELECT  *FROM    #tmpPendingResultEND
GO
--=========================================================
--测试
EXEC dbo.sp_replmonitorsubscriptionpendingcmds_EX 

上面相对使用起来更方便些。哈哈

--============================================================================================

来个妹子给大家降降温

 

转载于:https://www.cnblogs.com/TeyGao/p/3795452.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/420844.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

1006 换个格式输出整数 (15分)

1006 换个格式输出整数 (15分) 让我们用字母 B 来表示“百”、字母 S 表示“十”&#xff0c;用 12…n 来表示不为零的个位数字 n&#xff08;<10&#xff09;&#xff0c;换个格式来输出任一个不超过 3 位的正整数。例如 234 应该被输出为 BBSSS1234&#xff0c;因为它有 2…

利用反射和xml配置文件手写一个小型的框架

通用的增删改查1. 利用xml配置实体类和数据库表名的映射关系2. 根据xml设计&#xff0c;用正确的数据结构映射类封装好xml信息3. 得到数据库连接前&#xff0c;读取xml信息&#xff0c;用map封装成映射数据4. 写dao时根据反射和map生成sql语句&#xff0c;拿到属性值测试为了解…

DPtoLP/LPtoDP 和 ScreenToClient/ClientToScreen

设备坐标&#xff08;Device Coordinate&#xff09;又称为物理坐标&#xff08;Physical Coordinate&#xff09;&#xff0c;是指输出设备上的坐标。通常将屏幕上的设备坐标称为屏幕坐标。设备坐标用对象距离窗口左上角的水平距离和垂直距离来指定对象的位置&#xff0c;是以…

前端学习(1126):递归求数学题

阶乘1*2*3.....*n <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>Document</title> </he…

1007 素数对猜想 (20分)

1007 素数对猜想 (20分) 让我们定义 ​​ 为&#xff1a;dnpn1−pnd_{n} p_{n1} - p_{n}dn​pn1​−pn​&#xff0c;其中pip_{i}pi​是第iii个素数。显然有d11d_{1} 1d1​1&#xff0c;且对于n>1n>1n>1有dnd_{n}dn​是偶数。“素数对猜想”认为“存在无穷多对相邻且…

Spring工作原理

一、 IoC(Inversion of control): 控制反转1、IoC&#xff1a;概念&#xff1a;控制权由对象本身转向容器&#xff1b;由容器根据配置文件去创建实例并创建各个实例之间的依赖关系核心&#xff1a;bean工厂&#xff1b;在Spring中&#xff0c;bean工厂创建的各个实例称作bean二…

前端学习(1127):递归求数学题2

斐波那契数列 <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>Document</title> </head>…

注解和反射实现dao层增删改查

注解和反射写dao1. 注解的使用2. 使用注解体现映射关系针对上一篇文章&#xff0c;使用xml映射文件和反射实现dao&#xff0c;提出了扩展功能&#xff0c;利用注解来体现实体类和表的映射关系本文是上一篇文章的扩展使用反射和xml实现dao 1. 注解的使用 什么是注解&#xff1…

1008 数组元素循环右移问题 (20分)

输入样例: 6 2 1 2 3 4 5 6 输出样例: 5 6 1 2 3 4 # -*- coding: utf-8 -*- import mathdef right_shift(lst, m):n len(lst)m m % nfor j in range(math.gcd(m, n)):temp lst[j]i jwhile (i - m) % n > j:lst[i] lst[(i - m) % n]i (i - m) % nlst[i] tempretur…

其它数据类型和Json的转化

1.ResultSet→Json public static String resultSetToJson(ResultSet rs) throws SQLException,JSONException { // json数组 JSONArray array new JSONArray(); // 获取列数 ResultSetMetaData metaData rs.getMetaData(); int columnCount metaData.getColumnCount(…

MVC分层开发模式

MVC 1. 什么是mvc开发模式2. 基于servlet手写mvc框架1. 什么是mvc开发模式 mvc不是一种技术&#xff0c;只是一种开发模式使用分层开发模式能在大型项目中&#xff0c;让开发人员更好的协同工作便于项目的维护和扩展 M: Model 模型层->数据库层->daoV: View 视图层->…

1009 说反话 (20分)

1009 说反话 (20分) 给定一句英语&#xff0c;要求你编写程序&#xff0c;将句中所有单词的顺序颠倒输出。 输入格式&#xff1a; 测试输入包含一个测试用例&#xff0c;在一行内给出总长度不超过 80 的字符串。字符串由若干单词和若干空格组成&#xff0c;其中单词是由英文字…

C#_uploadify_mvc_version

jQuery Uploadify在ASP.NET MVC3中的使用 1、Uploadify简介 Uploadify是基于jQuery的一种上传插件&#xff0c;支持多文件、带进度条显示上传&#xff0c;在项目开发中常被使用。 Uploadify官方网址&#xff1a;http://www.uploadify.com/ 2、ASP.NET MVC3中的使用Uploadify 搭…

1010 一元多项式求导 (25分)

输入样例: 3 4 -5 2 6 1 -2 0 输出样例: 12 3 -10 1 6 0 # -*- coding: utf-8 -*-def get_derivation(lst):length len(lst)idx 0while idx < length:if lst[idx 1] ! 0:lst[idx] * lst[idx 1]lst[idx 1] - 1else:lst[idx] 0idx 2return lstif __name__ __main__…

VS2012 发布网站步骤

VS2012中发布网站的方式与以往有了不同&#xff0c;前面的版本发布如图 而2012点publish的时候弹出框有所不同&#xff0c;这边需要新建一个profile名字随便起&#xff0c;发布的方式有好几种&#xff0c; 当然不同的方式配置不同&#xff0c;用的最多的就是files system了 选择…

python 生成个性二维码

1. 效果 gif 图片二维码 带背景图二维码&#xff08;修改了&#xff09; 2. 依赖库 核心库myqr pip install myqr其它依赖库安装pip install pip install pillow, numpy, imageio3. 核心代码 我这里是F盘下的joy文件夹下面代码改变路径&#xff0c;图片名称参数即可 im…

1011 A+B 和 C (15分)

输入样例&#xff1a; 4 1 2 3 2 3 4 2147483647 0 2147483646 0 -2147483648 -2147483647 输出样例&#xff1a; Case #1: false Case #2: true Case #3: true Case #4: false # -*- coding: utf-8 -*-if __name__ __main__:n eval(input())input_list []for i in range…

SharePoint 2013:解决添加域名后每次都需要登录的问题

在SharePoint 2013中&#xff0c;当我们添加一个域名给SP后&#xff08;添加域名的方法请参考此文&#xff1a;http://www.cnblogs.com/jianyus/archive/2013/08/10/3249461.html &#xff09;&#xff0c;每次用域名访问都需要输入用户名和密码&#xff0c;即使该用户已经正确…

1012 数字分类 (20分)

输入样例 1&#xff1a; 13 1 2 3 4 5 6 7 8 9 10 20 16 18 输出样例 1&#xff1a; 30 11 2 9.7 9 输入样例 2&#xff1a; 8 1 2 4 5 6 7 9 16 输出样例 2&#xff1a; N 11 2 N 9 # -*- coding: utf-8 -*-def class_numbers(nums):arr [0] * 5exists [0] * 5flag1 1…

mvc框架upgrade

upgrade...1. actionservlet核心控制器更改以上1&#xff0c;2点2. 工具类&#xff08;对反射拿到的属性对象数据转型&#xff09;3. 业务服务类使用适配器设计模式针对上一篇文字做出了改造&#xff0c;既然要写手写lowb版mvc框架&#xff0c;就得付出一定代价&#xff0c;加大…