启用在订阅数据库上为SQL Server 2012事务复制生成非聚集索引



我们有一个SQL脚本,用于创建发布及其文章,以及请求订阅。这是非常基本的,因为我们没有明确地排除任何文章,而是将它们全部传递给订阅者(我们的报告使用的用于查询的数据库,以防止应用程序数据库上的过度负载)。

作为设置发布文章的一部分,脚本在源数据库中的每一篇文章上循环,对于表,执行以下操作:

DECLARE @Ins nvarchar(255) = 'CALL sp_MSins_'+@articleSchema+@articleName
DECLARE @Del nvarchar(255) = 'CALL sp_MSdel_'+@articleSchema+@articleName
DECLARE @Upd nvarchar(255) = 'CALL sp_MSupd_'+@articleSchema+@articleName
exec sp_addarticle
@publication = @PublishName, 
@article = @Joined, 
@source_owner = @articleSchema,
@source_object = @articleName,
@type = N'logbased',
@description = null, 
@creation_script = null, 
@pre_creation_cmd = N'drop', 
@schema_option = 0x00000000080350DF, 
@identityrangemanagementoption = N'manual',
@destination_table = @articleName,
@destination_owner = @articleSchema, 
@vertical_partition = N'false', 
@ins_cmd = @Ins,
@del_cmd = @Del, 
@upd_cmd = @Upd

我的问题是sp_addarticle存储过程的@schema_option参数。

我使用了此处的脚本:
https://blogs.msdn.microsoft.com/repltalk/2010/02/24/decrypting-schema_option-parameters-binary-value-for-a-transactional-replication-article/

为了检查0x00000000080350DF的计算值(按位或),并根据该脚本为表项目启用了以下选项:

**SCHEMA OPTIONS HERE ARE**  
—————————————
0x01 Generates the object creation script (CREATE TABLE, CREATE PROCEDURE, and so on). This value is the default for stored procedure articles.
0x02 – Generates the stored procedures that propagate changes for the article, if defined.
0x04 – Identity columns are scripted using the IDENTITY property.
0x08 – Replicate timestamp columns. If not set, timestamp columns are replicated as binary.
0x10 – Generates a corresponding clustered index. Even if this option is not set, indexes related to primary keys and unique constraints are generated if they are already defined on a published table.
0x40 – Generates corresponding nonclustered indexes. Even if this option is not set, indexes related to primary keys and unique constraints are generated if they are already defined on a published table.
0x80 – Replicates primary key constraints. Any indexes related to the constraint are also replicated, even if options 0x10 and 0x40 are not enabled.
0x1000 – Replicates column-level collation
0x4000 – Replicates UNIQUE constraints. Any indexes related to the constraint are also replicated, even if options 0x10 and 0x40 are not enabled
0x10000 – Replicates CHECK constraints as NOT FOR REPLICATION so that the constraints are not enforced during synchronization
0x20000 – Replicates FOREIGN KEY constraints as NOT FOR REPLICATION so that the constraints are not enforced during synchronization
0x8000000 – Creates any schemas not already present on the subscriber

正如您所看到的,似乎应该为表项目启用用于生成非聚集索引的0x40选项。

但是,在快照代理启动、生成快照并由日志读取器代理完成其工作后,如果我访问发布的属性,我可以看到对于表项目,"复制非聚集索引"的设置设置为false

有人知道为什么表中的文章没有启用这个选项吗?我一直在尝试解释technet上的文档:https://technet.microsoft.com/en-us/library/ms173857(v=sql.105).aspx,但还没有发现忽略此标志的原因。

我不能仅仅更改management studio中属性对话框中的选项,因为对于我们的产品,我们不一定可以直接访问客户端的SQL Server实例,而是必须通过powershell从安装机器远程执行脚本,这个脚本就是其中之一。

提前谢谢。

索引视图的默认架构选项不包括聚集索引。默认情况下,使用GUI设置文章时,只复制索引视图的架构,而不复制索引。

若要复制索引,可以更改sp_addarticle的脚本,并将@schema_option从0x0000000008000001替换为0x0000000008000051。

最新更新