Ranga Babu

SQL Server Replication (Merge) – Performance Issues in replicating schema changes

January 30, 2019 by

This article will review performance issues in replicating schema changes on tables involved in SQL Server Replication (Merge).

We had SQL Server merge replication configured on one of our databases to replicate data from publisher to subscriber and vice versa. Replicate schema changes was enabled on the publication to send the DDL changes made on replicated tables at publishers to subscribers.

When we applied the deployment scripts which modify the existing replicating tables by adding new columns or dropping existing columns at publisher, these schema changes took a lot of time to replicate to subscribers. Let us see what happens internally when there is a schema change on the table involved in SQL Server merge replication and why it is taking time to replicate schema changes.

To illustrate this let us create a merge publication with replicate schema changes option enabled.

Below are the server and database details in my environment. Please change the T-SQL scripts by replacing your publisher server, database and subscriber server, database names.

  • Publisher Server: PUBSERV01
  • Publication Database: test_pub
  • Subscriber Server: SUBSERV01
  • Subscriber Database: test_sub1

Please follow below steps to create table, publication, adding table to publication and creating subscribers using T-SQL scripts.

Configuring SQL Server Merge Replication

Create table “schema_test” in publisher database and enable database for SQL Server Replication (Merge).

Now let us configure merge publication using T-SQL script. Execute sp_addmergepublication at publisher on database that is being published. Replace the snapshot folder as per your need.

Once the publication is created, Navigate to the Local Publications under the replication folder in SQL Server Management Studio. Right click on the publication you created and click on Properties -> Subscription Options and make sure replicate schema changes is enabled.

Now add table “schema_test” to the merge publication created earlier. Execute below stored procedure on publisher database.

Once the table is added to publication, create a snapshot agent for the publication “test”.

Here I used a snapshot agent to run under the SQL Server Agent process account. Make sure the account you to run the snapshot agent have the permission on the snapshot folder you used earlier.

Also, Replace the publisher_login and publisher_password with your own credentials which are used to connect publisher. This script should be executed on publisher database.

Right click on the publication you just created, launch SQL Server replication monitor Click on your publication (test) navigate to Agents Tab. Make sure the snapshot is completed.

Once the snapshot is completed add the subscriber to publication. Here in my case I used push subscription and the merge agent is scheduled to run for every one minute. This script needs to be run on publisher database.

Merge agent is configured to run under SQL Server Agent service account and SQL Server login “sa” was used to connect subscriber. Make sure the logins used for connecting subscriber has access to subscriber database.

Add subscription script varies in case of pull subscription.

You can also use SQL Server Management GUI to configure publication, add tables to replication and to create subscribers.

Once the initial snapshot is applied on subscribers please refer to below steps.

SQL Server Replication (Merge) tracks the data changes using triggers. For each table added to merge replication three triggers one for insert, one for update and one for delete were created on the table. Also, few procedures, views and conflict table were created for each table added to merge publication. You can view them using table dependencies or you can query them using article id.

Navigate to the replicated table and right click on the table and click on View Dependencies.

sysmergearticles has an entry of each table added to merge publication. This table has information about the procedures, conflict table, filters if configured, conflict table, conflict resolvers and identity ranges if auto identity management is chosen. Query this table on publisher database to find the article id of the table. Query sys.objects using the first part of the article id to find the objects related to replicated table.

Now, on the publisher database, let us add new column to the table “schema_test”.

After adding new column, the schema change commands were inserted in sysmergeschemachange table and all the procedures, triggers, views related to the article (table) were modified to incorporate newly added column.

From the below image we can see all the objects related to replicated table were modified.

When the add new column command gets replicated to the subscriber these procedures, views, triggers were again modified at the subscriber as well to incorporate new column.

In my case, we received the deployment/upgrade script from developers with multiple ALTER TABLE statements to add new columns on same table as shown in below script.

Now these changes were considered as individual DDL changes and all the objects related to the article were modified for each alter table statement issued on the replicated table.

For example, if we have 18 SQL Server Replication internal objects related to the article “schema_test” and we added 10 columns using 10 alter table statements. All these 18 objects are modified 10 times (Total modifications will be 180).

On the publisher database it took 15 seconds to add 10 columns if new columns were added in multiple ALTER TABLE statements:

Launch the SQL Server replication monitor and we can see it took 13 seconds to sync these changes to subscriber. This may vary if your table structure is complex. Sync time would go high if you have lot of tables involved merge replication and adding/dropping columns using multiple alter table statements more tables.

Now let us compare timings by adding 10 more columns to table “schema_test” using single ALTER TABLE statement as shown in below T-SQL script.

Now it took one second to add 10 new columns to the table “schema_test” because the SQL Server Replication internal objects were modified only once instead of 10 times.

Launch the SQL Server replication monitor and check the sync timings. It took around four seconds to sync the schema changes to subscriber. Please refer to the below snapshot from replication monitor.

In this case the SQL Server Replication internal objects are modified only once at publisher and once at subscriber when the schema changes synced to subscriber. So, avoid using multiple ALTER TABLE statements to add columns or drop columns on the same table which is involved in merge replication.

When I performed same tests using transactional replication results are as below.

Using multiple ALTER TABLE statements in transactional SQL Server replication

At the publisher, adding 10 new columns with multiple ALTER TABLE statements took 3 seconds (As there are no systems objects created related to the replicated table like in merge replication)

To replicate these schema changes to subscriber it took 3 seconds.

Using single ALTER TABLE statement in transactional SQL Server replication

At the publisher, adding 10 new columns using single ALTER TABLE took less than a second and to replicate these schema changes to subscribers it took 1 second.

Using a single ALTER TABLE statement to add new columns or drop existing columns on same existing table is always better either in case of SQL Server merge replication or transactional replication.

Table of contents

SQL Server Replication with a table with more than 246 columns
Foreign key issues while applying a snapshot in SQL Server merge replication
SQL Server Replication (Merge) – What gets replicated and what doesn’t
SQL Server Replication (Merge) – Performance Issues in replicating schema changes
Merge SQL Server replication parameterized row filter issues
SQL Server Replication on a mirrored database
Log shipping on a mirrored database
Ranga Babu
Replication

About Ranga Babu

SQL Server DBA, Developer with good experience in SQL Server administration, development, performance tuning, monitoring, high availability and disaster recovery technologies

168 Views