如何为事务复制测量滞后时间并验证连接(复制 Transact-SQL 编程)

有关事务复制,重要的是能够验证已连接该服务器并测量滞后时间。 滞后时间是将在发布服务器中进行的更改传播到订阅服务器所用的时间。 有关详细信息,请参阅监视(复制)。 可以使用复制存储过程以编程方式获得此信息。

注意注意

跟踪令牌信息的保留时间与其他历史数据的保留时间一样长,该时间由分发数据库的历史记录保持期来控制。 若要更改保持期,请使用 sp_changedistributiondb (Transact-SQL) 更改 history_retention 属性的值。

若要将跟踪令牌发布到事务发布

  1. (可选)在发布服务器的发布数据库中,执行 sp_helppublication (Transact-SQL)。 请验证发布是否存在且状态是否处于活动状态。

  2. (可选)在发布服务器的发布数据库中,执行 sp_helpsubscription (Transact-SQL)。 请验证订阅是否存在且状态是否处于活动状态。

  3. 在发布服务器的发布数据库中,请执行 sp_posttracertoken (Transact-SQL),并指定 @publication。 请记录 @tracer_token_id 输出参数的值。

确定事务发布的滞后时间并验证连接

  1. 使用前一过程将跟踪令牌发送到发布。

  2. 在发布服务器的发布数据库中,请执行 sp_helptracertokens (Transact-SQL),指定 @publication。 这会返回发布到此发布的所有跟踪令牌的列表。 请记录结果集中所需的 tracer_id

  3. 在发布服务器的发布数据库中,执行 sp_helptracertokenhistory (Transact-SQL),指定 @publication,并将 @tracer_id 指定为从步骤 2 中获得的跟踪令牌 ID。 这会返回所选跟踪令牌的滞后时间信息。

删除跟踪令牌

  1. 在发布服务器的发布数据库中,请执行 sp_helptracertokens (Transact-SQL),指定 @publication。 这会返回发布到此发布的所有跟踪令牌的列表。 请记录结果集中要删除的跟踪令牌的 tracer_id

  2. 在发布服务器的发布数据库中,执行 sp_deletetracertokenhistory (Transact-SQL),指定 @publication 并将 @tracer_id 指定为从步骤 2 中获得的跟踪令牌 ID。

示例

本示例发送一个跟踪令牌记录并使用返回的已发送跟踪令牌的 ID 来查看滞后时间信息。

DECLARE @publication AS sysname;
DECLARE @tokenID AS int;
SET @publication = N'AdvWorksProductTran'; 

USE [AdventureWorks2008R2]

-- Insert a new tracer token in the publication database.
EXEC sys.sp_posttracertoken 
  @publication = @publication,
  @tracer_token_id = @tokenID OUTPUT;
SELECT 'The ID of the new tracer token is ''' + 
    CONVERT(varchar,@tokenID) + '''.'
GO

-- Wait 10 seconds for the token to make it to the Subscriber.
WAITFOR DELAY '00:00:10';
GO

-- Get latency information for the last inserted token.
DECLARE @publication AS sysname;
DECLARE @tokenID AS int;
SET @publication = N'AdvWorksProductTran'; 

CREATE TABLE #tokens (tracer_id int, publisher_commit datetime)

-- Return tracer token information to a temp table.
INSERT #tokens (tracer_id, publisher_commit)
EXEC sys.sp_helptracertokens @publication = @publication;
SET @tokenID = (SELECT TOP 1 tracer_id FROM #tokens
ORDER BY publisher_commit DESC)
DROP TABLE #tokens

-- Get history for the tracer token.
EXEC sys.sp_helptracertokenhistory 
  @publication = @publication, 
  @tracer_id = @tokenID;
GO