博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Central Subscriber Model Explained
阅读量:6485 次
发布时间:2019-06-23

本文共 5620 字,大约阅读时间需要 18 分钟。

原文 

The majority of SQL Server Replication topologies are based on the Central Publisher model, which is comprised of a single publisher replicating to one or more subscribers.  Another replication model, which is sometimes overlooked, is the Central Subscriber model, which is comprised of multiple publishers replicating to one subscriber using Transactional Replication.

The Central Subscriber model is useful for rolling up or consolidating data from multiple sources.  Some examples include:

  • Rolling up inventory from several warehouses into a central server at corporate headquarters.
  • Sending data from remote offices within a company to a central office for business continuity.
  • Consolidating order information to one location for centralized ordering.

Priming the pump

By default, subscriptions are initialized from a snapshot generated by the Snapshot Agent and then applied by the Distribution Agent.  When the snapshot is applied, by default the article property Action if name is in use is set to Drop existing object and create a new one, which instructs that the destination table be dropped if it already exists at a subscriber.  This behavior can be problematic in the Central Subscriber model when snapshots are applied since snapshots must be applied from multiple publications.  The first snapshot is applied as expected, however, subsequent snapshot applications result in the previous snapshot data being wiped out.

The solution to this problem is horizontal partitioning, static row filters, and setting the Action if name is in use article property to Delete data. If article has a row filter, delete only data that matches the filter.

Horizontal partitioning

Ideally, published tables in a Central Subscriber topology will be horizontally partitioned.  In order to horizontally partition the tables to be published, a location-specific column should be added and included as a part of a composite primary key.  Consider a table that looks like this:

CREATE TABLE TestTable(    ID int IDENTITY(1,1) NOT NULL,    FirstName varchar(100) NULL,    CONSTRAINT PK_TestTable_ID PRIMARY KEY CLUSTERED (ID ASC))

To horizontally partition TestTable and prepare it for a Central Subscriber configuration at Publisher 1, drop primary key constraint PK_TestTable_ID, add a location-specific column named LocationID with a default value of 1, and add the new composite primary key including the LocationID column.

ALTER TABLE TestTableDROP CONSTRAINT PK_TestTable_IDGO ALTER TABLE TestTableADD LocationID INT NOT NULL DEFAULT(1)GO ALTER TABLE TestTableADD CONSTRAINT PK_TestTable_ID_LocationID PRIMARY KEY CLUSTERED (ID, LocationID)GO

Next, to horizontally partition TestTable and prepare it for a Central Subscriber configuration at Publisher 2, the same preparation can be done with a default value of 2 for LocationID.

ALTER TABLE TestTableDROP CONSTRAINT PK_TestTable_IDGO ALTER TABLE TestTableADD LocationID INT NOT NULL DEFAULT(2)GO ALTER TABLE TestTableADD CONSTRAINT PK_TestTable_ID_LocationID PRIMARY KEY CLUSTERED (ID, LocationID)GO

Finally, to horizontally partition TestTable and prepare it for a Central Subscriber configuration at Publisher 3, the same preparation can be done with a default value of 3 for LocationID.

ALTER TABLE TestTableDROP CONSTRAINT PK_TestTable_IDGO ALTER TABLE TestTableADD LocationID INT NOT NULL DEFAULT(3)GO ALTER TABLE TestTableADD CONSTRAINT PK_TestTable_ID_LocationID PRIMARY KEY CLUSTERED (ID, LocationID)GO

Once the tables are horizontally partitioned, they can be properly published in a Central Subscriber topology by using static row filters, filtering on the LocationID column and setting the article property Action if name is in use to Delete data. If article has a row filter, delete only data that matches the filter.

Static row filters

For each article to be published in a Central Subscriber topology, a static row filter should be defined to leverage the Action if name is in usearticle property appropriately.  A static row filter uses a WHERE clause to select the data to be published.  To publish rows from Publisher 1, specify LocationID = 1 for the filter clause. Likewise, to publish rows from Publisher 2 and Publisher 3, specify LocationID = 2 and LocationID = 3 for the filter clause, respectively.

Action if name is in use

When creating the publications and adding articles, the article property Action if name is in use needs to be set to Delete data. If article has a row filter, delete only data that matches the filter.  This can be set using the New Publication Wizard Article Properties dialog or by using replication stored procedures  and specifying a value of delete for the @pre_creation_cmd argument.  This way, when the central subscriber is initialized or reinitialized from multiple publication snapshots, previously applied snapshot data will be preserved since only data matching the filter clause will be deleted.

The caveat

As we can see, horizontal partitioning requires that tables have a location-specific column added, however, the location-specific column does not necessarily need to be included as a part of the primary key at the publication databases.  In addition, it is not a hard requirement that published tables in a Central Subscriber topology be horizontally partitioned.  In some shops, changing a primary key or adding additional columns is strictly prohibited, in which case I would urge you to take an alternative approach.  If you would like some ideas on implementing a Central Subscriber topology without modifying primary keys or horizontally partitioning publication databases, feel free to  or leave a comment below.

转载地址:http://vtsuo.baihongyu.com/

你可能感兴趣的文章
软考复习之路——软考总结
查看>>
Kali linux 2016.2(Rolling)里Metasploit的常用模块
查看>>
企业项目开发--企业中的项目架构以及多环境分配(1)
查看>>
ZOJ 2412 Farm Irrigation
查看>>
C++语言基础(19)-模板的显式具体化
查看>>
深入理解JavaScript系列(18):面向对象编程之ECMAScript实现
查看>>
如何改变Android tab 的高度和字体大小
查看>>
hdu 2853
查看>>
VS2013 MVC Web项目使用内置的IISExpress支持局域网内部机器(手机、PC)访问、调试...
查看>>
Vue.js常用指令:v-show和v-if
查看>>
java自定义接口
查看>>
Codeforces Round #152 (Div. 2) B题 数论
查看>>
马云马化腾等大佬,是如何看待区块链的?
查看>>
10倍于行业增速!海尔冰箱套圈引领
查看>>
Java集合总结【面试题+脑图】,将知识点一网打尽!
查看>>
java基础(十) 数组类型
查看>>
小程序 Canvas绘图不同尺寸设备 UI兼容的两个解决方案
查看>>
产品规划,你通常规划多久的时间线?
查看>>
Android-MVP架构
查看>>
HTML5前端教程分享:CSS浏览器常见兼容问题
查看>>