[MS SQL Server]如何在Replication中新增Article時只建立新增的Article的Snapshot

在 MS SQL Server 中, 已建立好的 Replication , 若是要新增 article 時, 可以參考這篇: http://tim.diary.tw/2009/02/27/adding-articles-to-existing-publications/ , 比較討厭的地方就是若是原來的 publication 內容多或是資料量大時, 在重做一個新的可用的 snapshot 會很久, 而且佔用很大的空間, (雖然只會將新增的 article 同步到 subscriber).

其實有個參數可以讓新增 article 到 publication 時, 只建立新增 article 的 snapshot, 這樣就會省時又省空間多了, 參數就是在 publication 中的 @allow_anonymous 及 @immediate_sync, 預設在建立 publication 時, 這兩個參數是 true, 我們可以利用 sp_changepublication 這個 stored procedure 來調整已建立的 publication 將參數改為 false 即可, 如下:

[code]

EXEC sp_changepublication
@publication = ‘your publication name’,
@property = ‘allow_anonymous’ ,
@value = ‘false’
GO

EXEC sp_changepublication
@publication = ‘your publication name’,
@property = ‘immediate_sync’ ,
@value = ‘false’
GO

[/code]

如此一來, 在新增 article 到publication時, 就可以大幅減少建立整個 snapshot 的時間與空間了.

參考資料: http://www.mssqltips.com/sqlservertip/2502/limit-snapshot-size-when-adding-new-article-to-sql-server-replication/

SQL Server如何新增article到publication

在使用 Replication 時, 若是使用 Transaction 的方式, 若有要新增 article 到 publication 時, 其實很容易.

基本上就只要在原來的 publication 的 property 裡的 articles, 多加上需要 replication 出來的article, 完成後, 他不會自動進行後續, 接下來的動作就是做 snapshot 就行了, 這裡比較討厭的是若是原來的publication資料多, 而加入的 article資料少, 其實很不划算, 不過做完 snapshot 後, replication 機制會開始將該新發行的article schema傳給 subscriber, 讓 subscriber 將沒有的資料寫入, 這樣就完成了.

其實只會做新的部分, 不過因為還是要一個 schema及起始資料, 所以還是得做一個 snapshot 是比較吃資源的地方, 和 alter table的狀況又不太一樣了. 不過操作上還是很直覺也很方便!

參考SQL Server 2005 help 資料: http://msdn.microsoft.com/en-us/library/ms152493(SQL.90).aspx

其中比較重要的是這段:

After adding an article to a publication, you must create a new snapshot for the publication (and all partitions if it is a merge publication with parameterized filters). The Distribution Agent or Merge Agent then copies the schema and data for the new article to the Subscriber (it does not reinitialize the entire publication).

也就是說, 新增完了之後, 要做一次 snapshopt, 不過 agent 會將新的 article 送到 subscriber, 而不會整個 reinitialize!