脚本监控sqlserver 2008 R2 replication性能

脚本在分发服务器上执行!我主要使用来监控复制情况.下面是个案例,仅供参考!根据实际情况修改相关参数

/*
说明:命令在主用用于监控replication性能:
1.分发服务器上执行:监控复制进程是否运行、发布服务器到分发服务器、分发到订阅服务器传递的命令数;
2.分发服务器到订阅服务器复制等待情况

*/

--**********************************************************************************************
--第一部分:监控复制进程是否运行、发布服务器到分发服务器、分发到订阅服务器传递的命令数
--**********************************************************************************************


set nocount on

--新建临时表
create table #result
(
dbname sysname null,
name nvarchar(100) not null,
status int NOT NULL,
publisher sysname null,
publisher_db sysname null,
publication sysname null,
start_time datetime null,
time datetime null,
duration int NULL,
comments nvarchar(255) NULL,
delivered_transactions int NULL,
delivered_commands int NULL,
delivery_rate int NULL,
job_id varchar(36) NULL,
delivery_latency int NULL,
subscriber sysname null,
subscriber_db sysname null,
subscription_type int NULL,
subscriber_type tinyint NULL,
publisher_insertcount int NULL,
publisher_updatecount int NULL,
publisher_deletecount int NULL,
publisher_conflicts int NULL,
subscriber_insertcount int NULL,
subscriber_updatecount int NULL,
subscriber_deletecount int NULL,
subscriber_conflicts int NULL,
agent_type nvarchar(4000)
)

--新建临时表
create table #qs_rep_merge
(dbname sysname not null
,name nvarchar(100) not null
,status int NOT NULL
,publisher sysname not null
,publisher_db sysname not null
,publication sysname null
,subscriber sysname null
,subscriber_db sysname null
,subscription_type int NULL
,start_time nvarchar(24) null
,time nvarchar(24) null
,duration int NULL
,comments nvarchar(255) NULL
,delivery_rate int NULL
,publisher_insertcount int NULL
,publisher_updatecount int NULL
,publisher_deletecount int NULL
,publisher_conflicts int NULL
,subscriber_insertcount int NULL
,subscriber_updatecount int NULL
,subscriber_deletecount int NULL
,subscriber_conflicts int NULL
,error_id int NULL
,job_id binary(16) NULL
,local_job bit NULL
,profile_id int NOT NULL
,agent_id int NOT NULL
,local_timestamp binary(8) NOT NULL
,offload_enabled bit NOT NULL
,offload_server sysname null
,subscriber_type tinyint NULL
)

--新建临时表
create table #qs_rep_distribution
(dbname sysname not null
,name nvarchar(100) not null
,status int NOT NULL
,publisher sysname not null
,publisher_db sysname not null
,publication sysname null
,subscriber sysname null
,subscriber_db sysname null
,subscription_type int NULL
,start_time nvarchar(24) null
,time nvarchar(24) null
,duration int NULL
,comments nvarchar(4000) NULL
,delivery_time int NULL
,delivered_transactions int NULL
,delivered_commands int NULL
,average_commands int NULL
,delivery_rate int NULL
,delivery_latency int NULL
,error_id INT NULL
,job_id binary(16) NULL
,local_job bit NULL
,profile_id int NOT NULL
,agent_id int NOT NULL
,local_timestamp binary(8) NOT NULL
,offload_enabled bit NOT NULL
,offload_server sysname null
,subscriber_type tinyint NULL
)

--新建临时表
create table #qs_rep_misc
(
name sysname not null
,agent_type nvarchar(4000)
,status int NOT NULL
,message nvarchar(1024)
,start_time nvarchar(24) null
,run_duration int
,job_id binary(16) NULL
,local_timestamp binary(8) NOT NULL
)

--新建临时表
create table #qs_rep_snapshot
(dbname sysname not null
,name sysname not null
,status int NOT NULL
,publisher sysname not null
,publisher_db sysname not null
,publication sysname not null
,start_time nvarchar(24) null
,time nvarchar(24) null
,duration int NULL
,comments nvarchar(4000) NULL
,delivered_transactions int NULL
,delivered_commands int NULL
,delivery_rate float NULL
,error_id INT NULL
,job_id binary(16) NULL
,local_job bit NULL
,profile_id int NOT NULL
,agent_id int NOT NULL
,local_timestamp binary(8) NOT NULL
,dynamic_filter_login sysname NULL
,dynamic_filter_hostname sysname NULL)
create table #qs_rep_logreader
(dbname sysname not null
,name sysname not null
,status int NOT NULL
,publisher sysname not null
,publisher_db sysname not null
,start_time nvarchar(24) null
,time nvarchar(24) null
,duration int NULL
,comments nvarchar(4000) NULL
,delivery_time int NULL
,delivered_transactions int NULL
,delivered_commands int NULL
,average_commands int NULL
,delivery_rate int NULL
,delivery_latency int NULL
,error_id INT NULL
,job_id binary(16) NULL
,local_job bit NULL
,profile_id int NOT NULL
,agent_id int NOT NULL
,local_timestamp binary(8) NOT NULL
)

--新建临时表
create table #qs_rep_qreader
(dbname sysname not null
,name sysname not null
,status int NOT NULL
,start_time nvarchar(24) null
,time nvarchar(24) null
,duration int NULL
,comments nvarchar(4000) NULL
,transactions_processed int NULL
,commands_processed int NULL
,average_commands int NULL
,delivery_rate int NULL
,delivery_latency int NULL
,error_id INT NULL
,job_id binary(16) NULL
,profile_id int NULL
,agent_id int NOT NULL
,local_timestamp binary(8) NOT NULL
)


declare @db_name sysname
declare @cmd nvarchar(1000)
SET ANSI_WARNINGS OFF
insert into #qs_rep_misc with (tablockx)
select j.name, 'agent_type' = c.name,
'status' = case isnull(jh.run_status, 5) -- mapped to never run
when 0 then 5 -- Fail mapping
when 1 then 2 -- Success mapping
when 2 then 5 -- Retry mapping
when 3 then 2 -- Shutdown mapping
when 4 then 3 -- Inprogress mapping
when 5 then 0 -- Unknown is mapped to never run
end,
jh.message, 'start_time' = convert(nvarchar(8), jh.run_date) + N' ' +
stuff(stuff(right(convert(nvarchar(7), jh.run_time + 1000000), 6), 5, 0, N':'), 3, 0, N':') +
+ N'.000',
jh.run_duration,
'job_id' = convert(binary(16), j.job_id), 'local_timestamp' = 0 from
msdb..sysjobs j
LEFT OUTER JOIN msdb..sysjobhistory jh ON
j.job_id = jh.job_id and
jh.instance_id = (select max(instance_id) from msdb..sysjobhistory jh2 where
jh2.job_id = j.job_id)
INNER JOIN msdb..syscategories c ON
j.category_id = c.category_id
where j.category_id in ( 11, 12,16,17,18)
order by j.job_id asc
SET ANSI_WARNINGS ON

declare hCdatabase CURSOR LOCAL FAST_FORWARD FOR
select name from master.dbo.sysdatabases
where
category & 16 <> 0 and
has_dbaccess(name) = 1
for read only
open hCdatabase
fetch next from hCdatabase into @db_name
while (@@fetch_status <> -1)
begin

-- 检查用户权限
declare @has_pm bit
select @cmd = quotename(@db_name) + '.dbo.sp_executesql'
exec @cmd
N'if is_member(N''db_owner'') = 1 or is_member(N''replmonitor'') = 1 set @has_pm = 1',
N'@has_pm bit output',
@has_pm output
if @has_pm = 1
begin
-- 快照
select @cmd = 'insert into #qs_rep_snapshot with (tablockx) ' +
' exec ' + quotename(@db_name) + '.dbo.sp_MSenum_snapshot @show_distdb = 1'
exec (@cmd)
-- 日志读取
select @cmd = 'insert into #qs_rep_logreader with (tablockx) ' +
' exec ' + quotename(@db_name) + '.dbo.sp_MSenum_logreader @show_distdb = 1'
exec (@cmd)
-- 分发
select @cmd = 'insert into #qs_rep_distribution with (tablockx) ' +
' exec ' + quotename(@db_name) + '.dbo.sp_MSenum_distribution @show_distdb = 1, @exclude_anonymous = 0 '
exec (@cmd)
--merge
select @cmd = 'insert into #qs_rep_merge with (tablockx) ' +
' exec ' + quotename(@db_name) + '.dbo.sp_MSenum_merge @show_distdb = 1, @exclude_anonymous = 0 '
exec (@cmd)

select @cmd = 'insert into #qs_rep_qreader with (tablockx) ' +
' exec ' + quotename(@db_name) + '.dbo.sp_MSenum_qreader @show_distdb = 1'
exec (@cmd)

end
fetch next from hCdatabase into @db_name
end
close hCdatabase
deallocate hCdatabase
declare @misc_list_columns varchar(4000)
declare @snapshot_list_columns varchar(4000)
declare @logreader_list_columns varchar(4000)
declare @distribution_list_columns varchar(4000)
declare @merge_list_columns varchar(4000)
declare @qreader_list_columns varchar(4000)
set @misc_list_columns ='NULL dbname ' +
',a.name ' +
',a.status ' +
',NULL publisher ' +
',NULL publisher_db ' +
',NULL publication ' +
',convert(datetime,a.start_time,121) start_time ' +
',convert(datetime,a.start_time,121) [time] ' +
',a.run_duration run_duration ' +
',a.message comments ' +
',NULL delivered_transactions ' +
',NULL delivered_commands ' +
',NULL delivery_rate ' +
',convert(varchar(36), cast(a.job_id as uniqueidentifier)) job_id' +
',NULL delivery_latency ' +
',NULL subscriber ' +
',NULL subscriber_db ' +
',NULL subscription_type ' +
',NULL subscriber_type ' +
',NULL publisher_insertcount ' +
',NULL publisher_updatecount ' +
',NULL publisher_deletecount ' +
',NULL publisher_conflicts ' +
',NULL subscriber_insertcount ' +
',NULL subscriber_updatecount ' +
',NULL subscriber_deletecount ' +
',NULL subscriber_conflicts ' +
',5 agent_type '
set @snapshot_list_columns = 'dbname '+
',name '+
',status '+
',publisher '+
',publisher_db '+
',publication '+
',convert(datetime,start_time,121) start_time '+
',convert(datetime,[time],121) [time] '+
',duration run_duration '+
',comments '+
',delivered_transactions '+
',delivered_commands '+
',delivery_rate '+
',convert(varchar(36), cast(job_id as uniqueidentifier)) job_id'+
',NULL delivery_latency '+
',NULL subscriber '+
',NULL subscriber_db '+
',NULL subscription_type '+
',NULL subscriber_type '+
',NULL publisher_insertcount '+
',NULL publisher_updatecount '+
',NULL publisher_deletecount '+
',NULL publisher_conflicts '+
',NULL subscriber_insertcount '+
',NULL subscriber_updatecount '+
',NULL subscriber_deletecount '+
',NULL subscriber_conflicts '+
',1 agent_type '
set @logreader_list_columns ='dbname ' +
',name ' +
',status ' +
',publisher ' +
',publisher_db ' +
',NULL publication ' +
',convert(datetime,start_time,121) start_time ' +
',convert(datetime,[time],121) [time] ' +
',duration run_duration ' +
',comments ' +
',delivered_transactions ' +
',delivered_commands ' +
',delivery_rate ' +
',convert(varchar(36), cast(job_id as uniqueidentifier)) job_id' +
',delivery_latency ' +
',NULL subscriber ' +
',NULL subscriber_db ' +
',NULL subscription_type ' +
',NULL subscriber_type ' +
',NULL publisher_insertcount ' +
',NULL publisher_updatecount ' +
',NULL publisher_deletecount ' +
',NULL publisher_conflicts ' +
',NULL subscriber_insertcount ' +
',NULL subscriber_updatecount ' +
',NULL subscriber_deletecount ' +
',NULL subscriber_conflicts ' +
',2 agent_type '
set @distribution_list_columns ='dbname '+
',name '+
',status '+
',publisher '+
',publisher_db '+
',publication '+
',convert(datetime,start_time,121) start_time '+
',convert(datetime,[time],121) [time] '+
',duration run_duration '+
',comments '+
',delivered_transactions '+
',delivered_commands '+
',delivery_rate '+
',convert(varchar(36), cast(job_id as uniqueidentifier)) job_id'+
',delivery_latency '+
',subscriber '+
',subscriber_db '+
',subscription_type '+
',NULL subscriber_type '+
',NULL publisher_insertcount '+
',NULL publisher_updatecount '+
',NULL publisher_deletecount '+
',NULL publisher_conflicts '+
',NULL subscriber_insertcount '+
',NULL subscriber_updatecount '+
',NULL subscriber_deletecount '+
',NULL subscriber_conflicts '+
',3 agent_type '
set @merge_list_columns ='dbname '+
',name '+
',status '+
',publisher '+
',publisher_db '+
',publication '+
',convert(datetime,start_time,121) start_time '+
',convert(datetime,[time],121) [time] '+
',duration run_duration '+
',comments '+
',NULL delivered_transactions '+
',NULL delivered_commands '+
',delivery_rate '+
',convert(varchar(36), cast(job_id as uniqueidentifier)) job_id'+
',NULL delivery_latency '+
',subscriber '+
',subscriber_db '+
',subscription_type '+
',NULL subscriber_type '+
',publisher_insertcount '+
',publisher_updatecount '+
',publisher_deletecount '+
',publisher_conflicts '+
',subscriber_insertcount '+
',subscriber_updatecount '+
',subscriber_deletecount '+
',subscriber_conflicts '+
',4 agent_type '
set @qreader_list_columns ='dbname '+
',name '+
',status '+
',NULL publisher '+
',NULL publisher_db '+
',NULL publication '+
',convert(datetime,start_time,121) start_time '+
',convert(datetime,[time],121) [time] '+
',duration run_duration '+
',comments '+
',NULL delivered_transactions '+
',NULL delivered_commands '+
',delivery_rate '+
',convert(varchar(36), cast(job_id as uniqueidentifier)) job_id'+
',delivery_latency '+
',NULL subscriber '+
',NULL subscriber_db '+
',NULL subscription_type '+
',NULL subscriber_type '+
',NULL publisher_insertcount '+
',NULL publisher_updatecount '+
',NULL publisher_deletecount '+
',NULL publisher_conflicts '+
',NULL subscriber_insertcount '+
',NULL subscriber_updatecount '+
',NULL subscriber_deletecount '+
',NULL subscriber_conflicts '+
',9 agent_type '

set nocount off



insert into #result
exec(
' select ' + @misc_list_columns +
' from #qs_rep_misc a with (tablockx) , msdb..sysjobs b ' +
' where a.job_id = b.job_id ' +
' and b.category_id in (10, 11, 12,16,17,18)' +
' union all ' +
' select ' + @snapshot_list_columns +
' from #qs_rep_snapshot with (tablockx) ' +
' union all ' +
' select ' + @logreader_list_columns +
' from #qs_rep_logreader with (tablockx) ' +
' union all ' +
' select ' + @distribution_list_columns +
' from #qs_rep_distribution with (tablockx) ' +
' union all ' +
' select ' + @merge_list_columns +
' from #qs_rep_merge with (tablockx) ' +
' union all ' +
' select ' + @qreader_list_columns +
' from #qs_rep_qreader with (tablockx) ')

select dbname as [数据库名] ,name as [作业名],
case status
when 3 then '正在运行(复制)'
when 2 then '未运行'
when NULL then '未知'
end as [状态],
publisher as [发布服务器],
publisher_db as [发布数据库],
isnull(publication,'日志读取') as [发布名],
comments as [说明],subscriber as [订阅服务器],
subscriber_db as [订阅数据库] from #result
where publisher_db is not null and agent_type=3
union all
select dbname as [数据库名] ,name as [作业名],
case status
when 3 then '正在运行(日志读取)'
when 2 then '未运行'
when NULL then '未知'
end as [状态],
publisher as [发布服务器],
publisher_db as [发布数据库],
isnull(publication,'日志读取') as [发布名],
comments as [说明],subscriber as [订阅服务器],
subscriber_db as [订阅数据库] from #result
where publisher_db is not null and agent_type=2

drop table #qs_rep_merge
drop table #qs_rep_distribution
drop table #qs_rep_misc
drop table #qs_rep_snapshot
drop table #qs_rep_logreader
drop table #qs_rep_qreader
drop table #result
go

--**********************************************************************************************
--第二部分:监控复制进程是否运行、发布服务器到分发服务器、分发到订阅服务器传递的命令数

--**********************************************************************************************

/*

说明:根据自己环境需要定义下面变量:
set @inactive=1 --默认不变
set @virtual=-1 --默认不变
set @publisher='dbclu' --发布服务器名
set @publisher_db='jin' --发布数据库
set @publication='jinpolicy' --发布名称
set @subscriber='jin81' --订阅服务器名
set @subscriber_db='jin' --订阅数据库
set @subscription_type=0 --默认不变

--------新建临时表(不同名)

*/

----2.1 监视第一台订阅服务器

use distribution
declare @publisher sysname
declare @publisher_db sysname
declare @publication sysname
declare @subscriber sysname
declare @subscriber_db sysname
declare @subscription_type int
declare @retcode int
declare @agent_id int
declare @publisher_id int
declare @subscriber_id int
declare @lastrunts timestamp
declare @avg_rate float
declare @xact_seqno varbinary(16)
declare @inactive int
declare @virtual int


set @inactive=1
set @virtual=-1
set @publisher='dbclu'
set @publisher_db='jin'
set @publication='jinpolicy'
set @subscriber='jin81'
set @subscriber_db='jin'
set @subscription_type=0
set nocount on

--------新建临时表(不同名)
create table #t001
(
subname sysname,
waits int,
est int
)


if (@subscription_type not in (0,1))
begin
raiserror(14200, 16, 3, '@subscription_type')
return
end

select @publisher_id = server_id from sys.servers where upper(name) = upper(@publisher)
if (@publisher_id is null)
begin
raiserror(21618, 16, -1, @publisher)
return
end
select @subscriber_id = server_id from sys.servers where upper(name) = upper(@subscriber)
if (@subscriber_id is null)
begin
raiserror(20032, 16, -1, @subscriber, @publisher)
return
end
--
-- 获得agentID
--
select @agent_id = id
from 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
if (@agent_id is null)
begin
raiserror(14055, 16, -1)
return
end;
--
-- 计算时间
--
with dist_sessions (start_time, runstatus, timestamp)
as
(
select start_time, max(runstatus), max(timestamp)
from 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 dbo.MSpublications p join 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
end

select @lastrunts = max(timestamp)
from dbo.MSdistribution_history
where agent_id = @agent_id
end

select @xact_seqno = xact_seqno
,@avg_rate = delivery_rate
from dbo.MSdistribution_history
where agent_id = @agent_id
and timestamp = @lastrunts

select @xact_seqno = isnull(@xact_seqno, 0x0)
,@avg_rate = isnull(@avg_rate, 0.0)

if (@avg_rate = 0.0)
begin
select @avg_rate = isnull(avg(delivery_rate),0.0)
from dbo.MSdistribution_history
where agent_id = @agent_id
end

--------新建临时表(不同名)
create table #countab (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 = 9000000


if (@retcode != 0 or @@error != 0)
return
--
-- compute the time to process
-- return the resultset
--
--------新建临时表(不同名)
create table #subname(subscriber char(30))
insert into #subname (subscriber) values(@subscriber)

insert into #t001
select subscriber as '订阅名',
pendingcmdcount as [等待的命令数]
,N'估计秒数' = case when (@avg_rate != 0.0)
then cast((cast(pendingcmdcount as float) / @avg_rate) as int)
else pendingcmdcount end
from #countab , #subname

--------删除临时表
drop table #countab
drop table #subname

----2.2 监视第二台订阅服务器

set @inactive=1
set @virtual=-1
set @publisher='dbclu'
set @publisher_db='jin'
set @publication='dbtc'
set @subscriber='dbtc'
set @subscriber_db='policy'
set @subscription_type=0
set nocount on
--------新建临时表(不同名)
create table #t002
(
subname sysname,
waits int,
est int
)
if (@subscription_type not in (0,1))
begin
raiserror(14200, 16, 3, '@subscription_type')
return
end

select @publisher_id = server_id from sys.servers where upper(name) = upper(@publisher)
if (@publisher_id is null)
begin
raiserror(21618, 16, -1, @publisher)
return
end
select @subscriber_id = server_id from sys.servers where upper(name) = upper(@subscriber)
if (@subscriber_id is null)
begin
raiserror(20032, 16, -1, @subscriber, @publisher)
return
end

select @agent_id = id
from 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
if (@agent_id is null)
begin
raiserror(14055, 16, -1)
return
end;

with dist_sessions (start_time, runstatus, timestamp)
as
(
select start_time, max(runstatus), max(timestamp)
from 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 dbo.MSpublications p join 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
end

select @lastrunts = max(timestamp)
from dbo.MSdistribution_history
where agent_id = @agent_id
end

select @xact_seqno = xact_seqno
,@avg_rate = delivery_rate
from dbo.MSdistribution_history
where agent_id = @agent_id
and timestamp = @lastrunts

select @xact_seqno = isnull(@xact_seqno, 0x0)
,@avg_rate = isnull(@avg_rate, 0.0)

if (@avg_rate = 0.0)
begin
select @avg_rate = isnull(avg(delivery_rate),0.0)
from dbo.MSdistribution_history
where agent_id = @agent_id
end

--------新建临时表(不同名)
create table #countab1 (pendingcmdcount int )
insert into #countab1 (pendingcmdcount)
exec @retcode = 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
--
-- compute the time to process
-- return the resultset
--
--------新建临时表(不同名)
create table #subname1(subscriber char(30))
insert into #subname1 (subscriber) values(@subscriber)

insert into #t002
select subscriber as '订阅名',
pendingcmdcount as [等待的命令数]
,N'估计秒数' = case when (@avg_rate != 0.0)
then cast((cast(pendingcmdcount as float) / @avg_rate) as int)
else pendingcmdcount end
from #countab1 , #subname1
--------删除临时表
drop table #countab1
drop table #subname1


----2.3 监视第三台订阅服务器
set @inactive=1
set @virtual=-1
set @publisher='dbclu'
set @publisher_db='jin'
set @publication='rate168'
set @subscriber='dell'
set @subscriber_db='jin'
set @subscription_type=0
set nocount on

--------新建临时表(不同名)
create table #t003
(
subname sysname,
waits int,
est int
)
if (@subscription_type not in (0,1))
begin
raiserror(14200, 16, 3, '@subscription_type')
return
end

select @publisher_id = server_id from sys.servers where upper(name) = upper(@publisher)
if (@publisher_id is null)
begin
raiserror(21618, 16, -1, @publisher)
return
end
select @subscriber_id = server_id from sys.servers where upper(name) = upper(@subscriber)
if (@subscriber_id is null)
begin
raiserror(20032, 16, -1, @subscriber, @publisher)
return
end

select @agent_id = id
from 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
if (@agent_id is null)
begin
raiserror(14055, 16, -1)
return
end;

with dist_sessions (start_time, runstatus, timestamp)
as
(
select start_time, max(runstatus), max(timestamp)
from 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 dbo.MSpublications p join 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
end

select @lastrunts = max(timestamp)
from dbo.MSdistribution_history
where agent_id = @agent_id
end

select @xact_seqno = xact_seqno
,@avg_rate = delivery_rate
from dbo.MSdistribution_history
where agent_id = @agent_id
and timestamp = @lastrunts

select @xact_seqno = isnull(@xact_seqno, 0x0)
,@avg_rate = isnull(@avg_rate, 0.0)

if (@avg_rate = 0.0)
begin
select @avg_rate = isnull(avg(delivery_rate),0.0)
from dbo.MSdistribution_history
where agent_id = @agent_id
end

--------新建临时表(不同名)
create table #countab2 (pendingcmdcount int )

insert into #countab2 (pendingcmdcount)
exec @retcode = 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

--------新建临时表(不同名)
create table #subname2(subscriber char(30))
insert into #subname2 (subscriber) values(@subscriber)

insert into #t003
select subscriber as '订阅名',
pendingcmdcount as [等待的命令数]
,N'估计秒数' = case when (@avg_rate != 0.0)
then cast((cast(pendingcmdcount as float) / @avg_rate) as int)
else pendingcmdcount end
from #countab2 , #subname2

--------删除临时表
drop table #countab2
drop table #subname2


-----合并所有数据
select c.subname as [订阅名] , c.waits as [等待的命令数] ,c.est as [估计秒数] from
(
select * from #t001
union all
select * from #t002
union all
select * from #t003
) c
--------删除临时表
drop table #t001
drop table #t002
drop table #t003

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Microsoft SQL Server 2008 Service Pack 3 (SP3) 现在可供下载。我们不仅控制着产品更改,而且还投入了很大的力量来简化 Service Pack 的部署和管理。对于客户来说,Microsoft SQL Server 2008 Service Pack 3 中有这样几项重要改进:   从 SQL Server 的旧版本升级到 SQL Server 2008 SP3 的过程体验得到了改进。另外,我们增强了安装体验的性能和可靠性。   在 SQL Server Integration Services 日志中,现在能看到在数据流中发送的总行数。   在启用了“缩减数据库”选项的情况下创建维护计划时的警告信息得到了改进。   在启用了透明数据加密的情况下解决数据库问题,而且即使删除了证书也能使用数据库。   在 DTA(数据库调节顾问)引用经过索引的“空间数据类型”列时,查询结果经过了优化。   用户在并行执行计划中使用“序列函数”(如 Row_Numbers())可获得优异的体验。   Microsoft SQL Server 2008 Service Pack 2 不是安装 SQL Server 2008 Service Pack 3 的先决条件。可以使用这些包升级以下 SQL Server 2008 版本:   Enterprise   Standard   Evaluation   开发人员   Workgroup   SQL Server 2008 SP3 包含 SQL Server 2008 SP2 累积更新包 1 至 4 的累积更新,以及对通过客户反馈平台所收集问题的修复。它包括可支持性增强以及通过 Windows 错误报告收集的问题。   支持的操作系统:Windows 7;Windows Server 2003;Windows Server 2008;Windows Server 2008 R2;Windows Vista Windows 7; Windows8, Windows Server 2003 Service Pack 2 和 R2;Windows Server 2008 Service Pack 1 和 Service Pack 2;Windows Server 2008 R2;Windows Vista Service Pack 1 和 Service Pack2;Windows XP Service Pack 332 位系统 (x86)具有 Intel Pentium III 600 MHz(或同等性能的兼容处理器)或速度更快的处理器(建议使用 1 GHz 或速度更快的处理器)的 PC64 位系统(x64、ia64)1 GHz 或速度更快的处理器最低 512 MB 的 RAM(建议使用 1 GB 或更高的 RAM)675 MB 可用硬盘空间。
? 第四章 T-SQL编程 ? ? 1、TSQL语句的分类 ? 2、TSQL语句实战1 ? 3、TSQL语句实战2 ? 4、TSQL语句实战3 ? 5、TSQL语句实战4 ? 6、TSQL语句实战5 ? 7、TSQL语句实战6 ? 8、TSQL语句实战7 ? 9、TSQL语句实战8 ? 10、TSQL语句实战9 ? 11、TSQL语句实战10 ? 12、TSQL语句实战11 ? 13、TSQL语句实战12 ? 14、TSQL语句实战13 ? 15、TSQL语句实战14 ? 16、TSQL语句实战15 ? 17、TSQL语句实战16 ? 18、TSQL语句实战17 ? 19、TSQL语句实战18 ? 20、TSQL语句实战19 ? 21、TSQL语句实战20 ? ? 第五章 函数 ? ? 1、函数简介 ? 2、函数(1) ? 3、函数(2) ? 4、函数(3) ? 5、函数(4) ? 6、函数(5) ? 7、函数(6) ? 8、函数(7) ? 9、函数(8) ? 10、函数(9) ? 11、函数(10) ? 12、函数(11) ? 13、函数(12) ? 14、函数(13) ? 15、函数(14) ? 16、函数(15) ? 17、函数(16) ? 18、函数(17) ? 19、函数(18) ? 20、函数(19) ? 21、函数(20) ? 22、函数(21) ? 23、函数(22) ? ? ? 第六章 存储过程 ? ? 1、存储过程(1) ? 2、存储过程(2) ? 3、存储过程(3) ? 4、存储过程(4) ? 5、存储过程(5) ? 6、存储过程(6) ? 7、存储过程(7) ? 8、存储过程(8) ? 9、存储过程(9) ? 10、存储过程(10) ? 11、存储过程(11) ? 12、存储过程(12) ? 13、存储过程(13) ? 14、存储过程(14) ? 15、存储过程(15) ? 16、存储过程(16) ? 17、存储过程(17) ? 18、存储过程(18) ? 19、存储过程(19) ? 20、存储过程(20) ? 21、存储过程(21) ? ? 第七章 游标 ? ? 1、游标1 ? 2、游标2 ? 3、游标3 ? 4、游标4 ? 5、游标5 ? 6、游标6 ? 7、游标7 ? 8、游标8 ? 9、游标9
第一章 SQL Server基础 ? ? 1、SQL 2008视频教程—SQL数据库连接 ? 2、SQL 2008视频教程-系统数据库 ? 3、SQL 2008视频教程-数据库创建 ? 4、SQL 2008视频教程-数据库创建2 ? 5、SQL 视频教程-对数据库属性的更改 ? 6、SQL 2008视频教程-分离数据库 ? 7、SQL 2008视频教程-删除数据库 ? 8、SQL 2008视频教程-常用数据类型 ? 9、SQL 2008视频教程-常用数据类型2 ? 10、标识种子和标识自增量 ? 11、SQL 2008视频教程-字段默认值 ? 12、利用T-SQL语句创建数据库 ? 13、利用T-SQL语句更改数据库名称 ? 14、利用T-SQL语句创建数据库 ? ? ? 第二章T-SQL语句 ? ? 1、SQL 2008视频教程-数据库表常用术语 ? 2、SQL视频教程-数据库表常用术语2 ? 3、SQL视频教程-T-SQL创建数据库详解 ? 4、SQL视频教程-T-SQL创建数据库详解2 ? 5、SQL视频-T-SQL语言与SQL语言的区别 ? 6、SQL 2008视频教程-T-SQL语句1 ? 7、SQL 2008视频教程-T-SQL语句2 ? 8、SQL 2008视频教程-T-SQL语句3 ? 9、SQL 2008视频教程-T-SQL语句4 ? 10、T-SQL语句5 ? 11、SQL 2008视频教程-T-SQL语句6 ? 12、SQL 2008视频教程-T-SQL语句7 ? 13、SQL 2008视频教程-T-SQL语句8 ? 14、利用T-SQL语句修改表 ? 15、利用T-SQL语句插入记录 ? 16、设置字段是否允许Null ? 17、利用Update语句更新表中记录 ? 18、利用Update语句更新表中记录2 ? 18、利用Update语句更新表中记录3 ? 19、利用Update语句更新表中记录4 ? 20、利用T_SQL语句删除记录 ? 21、Select查询语句1 ? 22、Select查询语句2 ? 23、Select查询语句3 ? 24、Select查询语句—逻辑或运算符 ? 25、Select查询语句—不等于运算符 ? 26、 Select查询语句—不等于运算符2 ? 27、 Select查询语句—不等于运算符3 ? 28、 T-SQL语句中的运算符优先级 ? 29、 Select查询语句1 ? 30、 Select查询语句2 ? 31、 Select查询语句4 ? 32、 Select查询语句5 ? 33、 Select查询语句6 ? 34、 Select查询语句7 ? 35、 Select查询语句-嵌套查询1 ? 36、 用T-SQL创建表及其主键 ? 38、多表嵌套查询 ? 39、多表嵌套查询2 ? 40、对结果进行排序 ? 41、 统计函数SUM ? 42、 统计函数AVG ? 43、 统计函数AVG2 ? 44、 定义标识 ? 45、 Like运算符 ? 46、函数的综合运用 ? 47、分组汇总 ? 48、Group分组 ? 49、Group分组2 ? 50、对分组设定条件 ? 51、谓词查询 exists ? 52、谓词查询 any ? 53、查询指定数量的记录 ? 54、将查询结果存到另一个表中 ? 55、更新表中记录 ? 56、联合查询 ? 57、从多个表中查询记录 ? 58、按指定条件查询多个表中的记录 ? 59、超联接查询 ? 60、左联接和右联接 ? ? 第三章 约束、索引和视图 ? ? 1、简介 ? 2、Check约束 ? 3、主键约束 ? 4、创建唯一键 ? 5、索引1 ? 6、索引2 ? 7、索引3 ? 8、索引4 ? 9、索引5 ? 10、如何修改索引名称 ? 11、对索引的删除 ? 12、什么是视图及视图优缺点 ? 13、设计模式下创建视图 ? 14、视图的修改 ? 15、视图的修改2 ? 16、利用T-SQL语句创建视图 ? 17、利用T-SQL语句修改视图
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值