最近遇到“应用复制的命令时在订阅服务器上找不到该行”问题,报错如下:

官方给出的建议是重新同步和初始化订阅,当然,这是一种选择,但是对于动辄上百G的生产库,这种方法会消耗大量的资源和时间。可以通过定位出错数据,选择跳过,等从库追平主库后,修复错误数据方式,达到最终主从数据一致。
解决思路
1.
找到分发任务的事务序列号
获取当前出错的事务序列号有如下方法:
- 如果是复制分发停止了,那么在错误信息中会显示事务序列号。上图中是‘0x000311CC00013A7300010000000’
- 如果复制错误信息在显示窗口中被覆盖,比如选择了跳过错误(经过试验,发现跳过错误后,事务号会显示0x0000000000000000000000000000,所以最好不要跳过),或者其他错误覆盖了该错误,可以在分发服务器上执行 :
sp_helpsubscriptionerrors [ @publisher = ]'publisher'
, [ @publisher_db = ]'publisher_db'
, [ @publication = ]'publication'
, [ @subscriber = ]'subscriber'
, [ @subscriber_db = ]'subscriber_db'
参数对应值可以通过在分发服务器查询获取:
获取publisher和subscriber
select * from MSsubscriber_info
获取publisher_db和publication
select * from MSpublications
在订阅服务器上执行,获取事务序列号
sp_setsubscriptionxactseqno [ @publisher= ]'publisher',
[ @publisher_db= ]'publisher_db',
[ @publication= ]'publication',
[ @xact_seqno= ] xact_seqno
示例:


- 直接查询订阅服务器目标库上相关表,字段transaction_timestamp就是序列号
SELECT * FROM dbo.MSreplication_subscriptions
2.
查看出错命令
获取事务序列号之后,查看事务号对应的具体命令:
sp_browsereplcmds [ [ @xact_seqno_start = ] 'xact_seqno_start' ]
[ , [ @xact_seqno_end = ] 'xact_seqno_end' ]
[ , [ @originator_id = ] 'originator_id' ]
[ , [ @publisher_database_id = ] 'publisher_database_id' ]
[ , [ @article_id = ] 'article_id' ]
[ , [ @command_id= ] command_id ]
[ , [ @agent_id = ] agent_id ]
[ , [ @compatibility_level = ] compatibility_level ]
前两个参数是开始事务号和结束事务号,建议一定要输入publisher_database_id这个参数。该存储过程实际上查询的是MSrepl_commands表,该表的复合索引前缀是publisher_database_id该字段,对于大数据量的分法库,没用到索引的查询所消耗的时间可想而知。

command 默认的命名规则是sp_MS+operation+_schemaName+tableName

通过示例知道该操作表示对T_CarPakingRecord表进行update操作,最后的表示的是主键。
3.
补救措施
根据上面分析的命令结果,将完整数据插入到从库中(这里是补救缺失行错误,如果是插入重复则直接删除即可)
如果认为数据一致性不重要,则可以直接跳过,不推荐这样。跳过错误的命令:
sp_setsubscriptionxactseqno [ @publisher = ] '发布服务器主机名'
, [ @publisher_db = ] '发布数据库'
, [ @publication = ] '发布名称'
, [ @xact_seqno = ] 跳过的事务号
|