码迷,mamicode.com
首页 > 其他好文 > 详细

Publishing Stored Procedure Execution in Transactional Replication

时间:2015-12-30 21:48:37      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

Replication 不仅可以将Table Article 或 SP Article 推送到subscription,而且还能将sp的execution推送到subscription。推送sp的execution是指将执行sp的command 推送到subscriber去执行, 而不是将sp执行之后产生的大量transaction推送到subscriber上。Transaction是逐个更新Table Article中的数据行。

推送sp的execution来同步subscription的数据,对sp有一定的限制:

Stored procedure replication is not appropriate for all applications. If an article is filtered horizontally, so that there are different sets of rows at the Publisher than at the Subscriber, executing the same stored procedure at both returns different results. Similarly, if an update is based on a subquery of another, nonreplicated table, executing the same stored procedure at both the Publisher and Subscriber returns different results.

 

推送SP的execution设置Article的Properties :Replicate  为 Execution of the stored procedure 或 Execution in a serialized of the SP 之一。

技术分享

 

sp的代码是更新Table Article值,dbo.dt_study是一个Table Article,设置没有row filter 条件

ALTER PROCEDURE [dbo].[usp_Update_dt_study]
@id int,
@name varchar(100),
@sex bit
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    update dbo.dt_study
    set name=@name,
    sex=@sex
    where id=@id;

END


 

参考MSDN文档:Publishing Stored Procedure Execution in Transactional Replication

If you have one or more stored procedures that execute at the Publisher and affect published tables, consider including those stored procedures in your publication as stored procedure execution articles. The definition of the procedure (the CREATE PROCEDURE statement) is replicated to the Subscriber when the subscription is initialized; when the procedure is executed at the Publisher, replication executes the corresponding procedure at the Subscriber. This can provide significantly better performance for cases where large batch operations are performed, because only the procedure execution is replicated, bypassing the need to replicate the individual changes for each row. For example, assume you create the following stored procedure in the publication database:

CREATE PROC give_raise AS
UPDATE EMPLOYEES SET salary = salary * 1.10

 

This procedure gives each of the 10,000 employees in your company a 10 percent pay increase. When you execute this stored procedure at the Publisher, it updates the salary for each employee. Without the replication of stored procedure execution, the update would be sent to Subscribers as a large, multi-step transaction:

 
BEGIN TRAN
UPDATE EMPLOYEES SET salary = salary * 1.10 WHERE PK = emp 1
UPDATE EMPLOYEES SET salary = salary * 1.10 WHERE PK = emp 2

 

And this repeats for 10,000 updates.

With the replication of stored procedure execution, replication sends only the command to execute the stored procedure at the Subscriber, rather than writing all the updates to the distribution database and then sending them over the network to the Subscriber:

EXEC give_raise

 

Types of Stored Procedure Execution Articles

There are two different ways in which the execution of a stored procedure can be published: serializable procedure execution article and procedure execution article.
  • The serializable option is recommended because it replicates the procedure execution only if the procedure is executed within the context of a serializable transaction. If the stored procedure is executed from outside a serializable transaction, changes to data in published tables are replicated as a series of DML statements. This behavior contributes to making data at the Subscriber consistent with data at the Publisher. This is especially useful for batch operations, such as large cleanup operations.

  • With the procedure execution option, it is possible that execution could be replicated to all Subscribers regardless of whether individual statements in the stored procedure were successful. Furthermore, because changes made to data by the stored procedure can occur within multiple transactions, data at the Subscribers might not be consistent with data at the Publisher. To address these issues, it is required that Subscribers are read-only and that you use an isolation level greater than read uncommitted. If you use read uncommitted, changes to data in published tables are replicated as a series of DML statements.

The following example illustrates why it is recommended that you set up replication of procedures as serializable procedure articles.

BEGIN TRANSACTION T1
SELECT @var = max(col1) FROM tableA
UPDATE tableA SET col2 = <value> 
   WHERE col1 = @var 

BEGIN TRANSACTION T2
INSERT tableA VALUES <values>
COMMIT TRANSACTION T2

 

In the previous example, it is assumed that the SELECT in transaction T1 happens before the INSERT in transaction T2.

If the procedure is not executed within a serializable transaction (with isolation level set to SERIALIZABLE), transaction T2 will be allowed to insert a new row within the range of the SELECT statement in T1 and it will commit before T1. This also means that it will be applied at the Subscriber before T1. When T1 is applied at the Subscriber, the SELECT can potentially return a different value than at the Publisher and can result in a different outcome from the UPDATE.

If the procedure is executed within a serializable transaction, transaction T2 will not be allowed to insert within the range covered by the SELECT statement in T2. It will be blocked until T1 commits ensuring the same results at the Subscriber.

Locks will be held longer when you execute the procedure within a serializable transaction and may result in reduced concurrency.

 

When replicating stored procedure execution, the setting for the session executing the stored procedure should specify XACT_ABORT ON. If XACT_ABORT is set to OFF, and an error occurs during execution of the procedure at the Publisher, the same error will occur at the Subscriber, causing the Distribution Agent to fail. Specifying XACT_ABORT ON ensures that any errors encountered during execution at the Publisher cause the entire execution to be rolled back, avoiding the Distribution Agent failure.

If you require a setting of XACT_ABORT OFF, specify the -SkipErrors parameter for the Distribution Agent. This allows the agent to continue applying changes at the Subscriber even if an error is encountered.

 

Publishing Stored Procedure Execution in Transactional Replication

标签:

原文地址:http://www.cnblogs.com/ljhdo/p/5077949.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!