背景
事务复制中发布表有分区表,如何配置发布项,使分区结构传播到订阅库?有何限制?
测试环境
CodeUSE [master]GOCREATE DATABASE [OMS_Test] ON PRIMARY ( NAME = N'OMS_Test_data1', FILENAME = N'C:/Program Files/Microsoft SQL Server/MSSQL10_50.MSSQLSERVER/MSSQL/DATA/OMS_Test.mdf' , SIZE = 34816KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ), FILEGROUP [FG_OMSTest_1] ( NAME = N'OMS_Test_data2', FILENAME = N'C:/Program Files/Microsoft SQL Server/MSSQL10_50.MSSQLSERVER/MSSQL/DATA/OMS_Test_data2.ndf' , SIZE = 475136KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ), FILEGROUP [FG_OMSTest_2] ( NAME = N'OMS_Test_data3', FILENAME = N'C:/Program Files/Microsoft SQL Server/MSSQL10_50.MSSQLSERVER/MSSQL/DATA/OMS_Test_data3.ndf' , SIZE = 611328KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ), FILEGROUP [FG_OMSTest_3] ( NAME = N'OMS_Test_data4', FILENAME = N'C:/Program Files/Microsoft SQL Server/MSSQL10_50.MSSQLSERVER/MSSQL/DATA/OMS_Test_data4.ndf' , SIZE = 245760KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) LOG ON ( NAME = N'OMS_Test_log', FILENAME = N'C:/Program Files/Microsoft SQL Server/MSSQL10_50.MSSQLSERVER/MSSQL/DATA/OMS_Test_log.ldf' , SIZE = 5093952KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)GOuse OMS_Testgocreate partition function pf_orders_createon(datetime2)as range right forvalues('20120101','20130101','20140101')gocreate partition scheme ps_orders_createonas partition pf_orders_createonto ([primary],[FG_OMSTest_1],[FG_OMSTest_2],[FG_OMSTest_3])gocreate table orders (ID INT IDENTITY(1,1),OrderNumber varchar(20) not null,CreateOn datetime2 not null)on [ps_orders_createon](CreateOn)goalter table orders add constraint PK_Orders_ID_CreateON primary key nonclustered (ID,CREATEON)gocreate clustered index CIX_Orders_CreateOn on Orders(CreateOn)on [ps_orders_createon](CreateOn)go
加载测试数据之后的数据分布如下:
前置条件
使用分析
1. 满足“前置条件”建立的事务复制,分区表(包括数据、分区函数和分区架构)成功发布并传播到订阅库。但是下列命令不会从发布库传播到订阅库:
ALTER INDEX 的 ALTER PARTITION FUNCTION、ALTER PARTITION SCHEME 或 REBUILD WITH PARTITION。
在订阅库执行如下语句,新增的数据可以传播到订阅库,但是Partition Function & Scheme的修改没有传播到订阅库:
Codealter partition scheme ps_orders_createonnext used [PRIMARY]GOalter partition function pf_orders_createon()split range('20140201')GOinsert into dbo.ordersvalues ('140714001074904685','2014-07-15 04:06:19.6600000')GO
需要在订阅库执行上面的两个Alter,才能与发布库一致。如果使用是非PRIMARY文件组,则还需要在订阅库上先增加同样的文件组。
分割分区后,发布和订阅的数据分布是一样,如下:
2. 对事务复制中的分区表启用PARTITION SWITCH,使得发布库PARTITION SWITCH传播到订阅库,需要满足如下条件:
前者允许对已发布的数据库执行 ALTER TABLE…SWITCH 语句,后者使得对已发布的数据库执行的 ALTER TABLE…SWITCH 语句传播到订阅服务器。
执行如下语句修改发布属性并将Partition 4切到临时表:
CodeUSE [OMS_Test]GO--Set Publication properties for partition switchexec sp_changepublication 'OMSTEST_PUB',N'allow_partition_switch',N'true';exec sp_changepublication 'OMSTEST_PUB',N'replicate_partition_switch','true';go--create table for partition switch outCREATE TABLE [dbo].[orders_Temp]([ID] [int] NOT NULL,[OrderNumber] [varchar](20) NOT NULL,[CreateOn] [datetime2](7) NOT NULL, CONSTRAINT [PK_OrdersTemp_ID_CreateON] PRIMARY KEY NONCLUSTERED ([ID] ASC,[CreateOn] ASC))on [FG_OMSTest_3]GOCREATE CLUSTERED INDEX CIX_OrdersTemp_CreateON on dbo.orders_temp(createon)goALTER TABLE dbo.orders_Temp ADD CONSTRAINTCK_orders_Temp CHECK (CreateOn>='2014-01-01' and CreateOn<'2014-02-01')go--Switch partition 4 to orders_tempALTER TABLE dbo.orders switch partition 4 to dbo.orders_tempgo
执行后,订阅和发布的数据都成功切出。现在再尝试加载分区,也就是切回来,结果也是成功的:
新闻热点
疑难解答