标签:
在transactional replication中,在publication中执行了一个更新,例如:update table set col=? Where ?,如果table中含有大量的数据行,那么该操作会产生大量数据更新操作。在subscription端,这个更新操作会分解成多条command(默认情况下产生一个Transaction,每个数据行产生一个Command)。Distribution Agent包含两个子进程,Reader和Writer。 Reader负责从 Distribution 数据库中读取数据,Writer负责将reader读取的数据写入到订阅数据库。Reader是通过 sys.sp_MSget_repl_commands 来读取Distribution数据库中(读取Msrepl_transactions表和Msrepl_Commands表)的数据。
CREATE PROCEDURE sys.sp_MSget_repl_commands ( @agent_id int, @last_xact_seqno varbinary(16), @get_count tinyint = 0, -- 0 = no count, 1 = cmd and tran (legacy), 2 = cmd only @compatibility_level int = 7000000, @subdb_version int = 0, @read_query_size int = -1 )
1,在Distribution Agent同步数据到Subscriber中时,发现Subscriber中有很多Session处于 ASYNC_NETWORK_IO,说明该Session返回的数据集太大,导致Writer不能及时将Command读取,使用Distribution Latency增加,该Session正在执行的sp是:sys.sp_MSget_repl_commands,正在执行的语句如下,这条查询用于返回Distribution Agent读取的Commands。
select rc.xact_seqno, rc.partial_command, rc.type, rc.command_id, rc.command, rc.hashkey, -- extra columns for the PeerToPeer resultset -- originator_id, srvname, dbname, originator_publication_id, originator_db_version, originator_lsn NULL, NULL, NULL, NULL, NULL, NULL, rc.article_id from MSrepl_commands rc with (nolock, INDEX(ucMSrepl_commands)) JOIN dbo.MSsubscriptions s with (INDEX(ucMSsubscriptions)) -- At end, we use the FASTFIRSTROW option which tends to force -- a nested inner loop join driven from MSrepl_commands ON (rc.article_id = s.article_id) where s.agent_id = @agent_id and rc.publisher_database_id = @publisher_database_id and rc.xact_seqno > @last_xact_seqno and rc.xact_seqno <= @max_xact_seqno and (rc.type & @snapshot_bit) <> @snapshot_bit and (rc.type & ~@snapshot_bit) not in ( 37, 38 ) and (@compatibility_level >= 9000000 or (rc.type & ~@postcmd_bit) not in (47)) order by rc.xact_seqno, rc.command_id asc OPTION (FAST 1)
通过SQL Server Profile抓取当前正在执行的SQL,从抓取的大量语句中发现,一般只会用到前四个参数,第三个和第四个参数的值是固定不变的,分别是0和10000000。
exec sp_MSget_repl_commands 74,0x0008ECE200307E10014C00000000,0,10000000
2,Distribution Agent 读取的Commnd数量
sys.sp_MSget_repl_commands 返回的Result Set的大小跟变量 @max_xact_seqno 有关
rc.xact_seqno > @last_xact_seqno and rc.xact_seqno <= @max_xact_seqno
对变量 @max_xact_seqno 的赋值,是由 @read_query_size 参数控制的,在调用该sp时,其值是默认值-1。下面代码表示 将 dbo.MSrepl_commands 最大的 xact_seqno 赋值给变量@max_xact_seqno,那么Distribution Agent 每次都会读取所有的Command。
--Note4: The max_xact_seqno is calculated based on the @read_query_size parameter -
-- this parameter limit the number of commands retrieved by this call.
if(@read_query_size <= 0) begin select @max_xact_seqno = max(xact_seqno) from MSrepl_commands with (READPAST) where publisher_database_id = @publisher_database_id and command_id = 1 and type <> -2147483611 end else begin -- calculate the @max_xact_seqno from the next @read_query_size commands. declare @GetMaxCommand nvarchar(1024) select @GetMaxCommand = N‘select top ‘ + convert(nvarchar(1024),@read_query_size)+ N‘ @max_xact_seqno = xact_seqno from MSrepl_commands with (READPAST) where publisher_database_id = @publisher_database_id and type <> -2147483611 and xact_seqno > @last_xact_seqno order by xact_seqno, command_id asc‘ exec sys.sp_executesql @GetMaxCommand, N‘@max_xact_seqno varbinary(16) output ,@last_xact_seqno varbinary(16),@publisher_database_id int‘, @publisher_database_id = @publisher_database_id, @max_xact_seqno = @max_xact_seqno output, @last_xact_seqno=@last_xact_seqno if(@max_xact_seqno is null) select @max_xact_seqno = @last_xact_seqno end
3,为@read_query_size传递一个参数值,而不是使用默认值 -1,由于该sp是system stored procedure,不能直接修改,而Distribution Agent profile中也没有参数能够控制Reader读取的Command数量。
参考文档:
SQL Server复制系列4 – Transactional replication中如何跳过一个事务
Distribution1:Distribution Reader
标签:
原文地址:http://www.cnblogs.com/ljhdo/p/5730395.html