Home > SQL Server Tips > Microsoft SQL Server > Optimize merge replication performance
SQL Server Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

MICROSOFT SQL SERVER

Optimize merge replication performance


By Hilary Cotter, Contributor
04.11.2007
Rating: -4.50- (out of 5)


Expert advice on database administration
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


Merge replication has been a feature of SQL Server since SQL Server 7.0. It is designed for clients who are frequently offline and need to bi-directionally replicate with a publisher.

Merge replication works by logging changes that occur on the publisher or subscriber(s) in your merge replication topology between synchronizations. During synchronizations, the publisher and subscriber compare what has changed since the last synchronization. Then they use stored procedures to synchronize the subscriber with changes that occurred on the publisher, and the publisher with changes that occurred on the subscriber. If changes occur on the same row(s) between synchronizations, during synchronization these changes are logged as conflicts and either the publisher's or the subscriber's change will be persisted on either side, depending on how you choose to resolve these conflicts.

Microsoft has published an article on how to improve merge replication performance, which is a helpful reference.

Here are some additional tips to consider:

Is merge replication the best replication choice?
Merge replication is not the only bi-directional replication solution Microsoft offers. When considering which choice to make, consider the following factors:

  • Does it modify the underlying schema?
  • Does it scale?
  • Is there added latency?
  • Is there a limitation on where the majority of the DML (Data Manipulation Language) should occur?
  • How does it handle conflicts?
  • Does it replicate DDL (Data Definition Language) statements, i.e., can you modify the schema without breaking replication?
  • Are there any version/edition limitations?

The following table illustrates the choices and their limitations:

Replication TypeSchema ChangesScaleLatencyDML limitationsConflict HandlingReplicate DDLVersion Edition Limitations
Merge ReplicationAdds a GUID colYes, you may need hierarchies.Adds latency to each DML, sync times can be lengthy.None.Logs conflicts and allows you to roll them back. Rich conflict handling.Yes.SQL 2005 limits the number of subscribers depending on your version. MSDE can act as a publisher; Express can only be a subscriber.
Bi-directional Transactional replication.NoneOffers very good performance, but does not scale well beyond 2 to 3 subscribers.Offers excellent performance.None.No conflict handling.None.No version dependencies. Not supported on SQL Server Express or MSDE.
Immediate UpdatingAdds a GUID columnScales up to 10 subscribers. It is crtical that the link be very stable. If there is any network interruption the subscribers will become read only.Latency is added to all DML originating at the subscriber.The majority of the DML should originate on the publisher.Conflicts are logged.Yes.No version dependencies. Not supported on MSDE or SQL Server Express.
Queued UpdatingAdds a GUID column.Scales up to 10 subscribers.Adds some latency to all DML originating at the subscriber.The majority of the DML should originate on the publisher.Conflicts are logged.Yes.No version dependencies. Not supported on MSDE or SQL Server Express.
Peer to PeerNoneOffers very good performance, but does not scale well beyond 10 subscribers.Offers excellent performance.None. However, all updates should occur on a single node to limit possible update conflicts.No conflict handling.Yes, however all nodes should be quiesced while DDL changes occur.SQL 2005 only, supported on the Enterprise Edition.
RDANone.Can offer better synchronization performance on PDAs than merge replication.Can be shorter than merge.None.None.No. Works on PDAs running SQL CE only.

Evaluate whether some of the other bi-directional replication options will work for you, despite the limitations.

Choose the appropriate profile
Most of the fine tuning of merge replication topologies is done through the agent properties. Microsoft has bundled collections of these properties together in groups called profiles, with each profile dedicated to a specific topology. For example, SQL 2005 has the following profiles:

  • Default agent
  • High volume server-to-server
  • Rowcount and checksum validation
  • Rowcount validation
  • Slow link agent
  • Verbose history agent
  • Windows Synchronization Manager

Choose the appropriate profile for each subscriber. Only use the Rowcount and checksum validation profile, Rowcount validation profile, and Verbose history agent profile while you are debugging or performance tuning. If you are doing server to server merge replication, use the High volume server-to-server profile, instead of the Default agent profile.

Similarly if you are replicating to clients over low bandwidth links -- like phone lines -- use the slow link agent profile for these subscribers.

Minimize conflicts
A conflict occurs during a synchronization when:

  • A row on the publisher has the same pk value as a row on the subscriber.
  • An attempt is made to delete a row on the publisher which has been updated on the subscriber.
  • An attempt is made to delete a row on the subscriber which has been deleted on the publisher.
  • An attempt is made to update a row on the publisher which has been deleted on the subscriber.
  • An attempt is made to update a row on the subscriber which has been deleted on the publisher.
  • If you are using column level tracking and the same column is updated on the publisher and subscriber.
  • If you are using row-level tracking and the same row is updated on both the publisher and subscriber.

When doing a sync, the synchronization process attempts to merge all changes on both sides. Depending on the type of conflict, a retry attempt is made after each batch is processed. These re-tries and the subsequent conflict logging are somewhat expensive, and anything you can do to minimize conflicts will shorten synchronization times. In some cases the merge agent will fail after a conflict and only succeed during the next synchronization.

Partitioning your data to avoid conflicts will increase the efficiency of your synchronizations.

Retention period
Merge replication has to do extensive tracking to know what it has sent and received from each subscriber, and to know what it has to send to each new subscriber in addition to the snapshot. On high volume systems, this tracking data (or replication metadata) can become very large and can increase the amount of time required to synchronize. This is especially acute with mobile subscribers. Drop the retention period to the smallest value possible. The limit should be a time when you know all subscribers will synchronize within; otherwise you will need to reinitialize these subscribers.

Snapshot
When synchronization times become lengthy, they may exceed the time period required to send a new snapshot. In this case, consider sending a new snapshot to these subscribers. This is especially acute with SQL CE subscribers.

Hierarchies
When you have large numbers of subscribers, there will be considerable locking when the merge agents run. In some cases, it will make sense to create hierarchies where groups of subscribers will synchronize with one publisher. This will in turn synchronize with another publisher further upstream. The topology might consist of a top level publisher replicating to four down stream subscriber/publishers, and from each of those four downstream subscriber/publishers 20 or more subscribers replicate to.

Creating such hierarchies will improve the overall performance of all nodes in the topology.

Join filters
Join filters are a merge only feature that treats related data as a unit or partition. Consider a case where you have a salesman table, which has a client table related by a common SalesPersonID key. Further, you have an order table which is related to the client table by a clientID key. If the clients were assigned to a new Sales Person and you had a filter on SalesPersonID, when you did the update of the SalesPersonID from let's say one to two, the rows in
Read more about replication in SQL Server:
  • Replicated stored procedure options in SQL Server 2005

  • Learning center: Replication techniques in SQL Server
  • the client table would move to the subscription that matched the filter value. But the order rows that correspond to that client table would not move. In order to extend that partition to the orders table you either add the SalesPersonID to each the order table and update the value of the SalesPersonID column here as well, or use join filters.

    Join filters require a lot of processing during synchronization to figure out all rows that should be added or removed from a partition. In SQL 2005, partitions can be pre-computed so the partitions will not have to be dynamically built during the synchronization process. However, this does add even more latency to all DML originating at the Publisher or Subscriber, so the impact using pre-computed partition may be greater than if you do not use them.

    If you can make your join filters as shallow as possible. In our case, our join filters were two levels deep. You will get dramatic performance improvements by moving from four to five levels to ywo to one level(s). This will require re-architecting your schema however, and considerable de-normalization of your tables.

    Another point about join filters and filters in general is to ensure that all columns you are filtering on have an index on them.

    Summary
    In this article we had a look at some not-so-obvious methods of improving merge replication performance. It is important to evaluate whether merge replication is the best solution. I find it is frequently implemented for server to server replication as a DR solution, when bi-directional transactional replication is a better fit. Properly tuned merge replication is highly scalable and does perform exceptionally well.


    ABOUT THE AUTHOR:   
    Hilary Cotter has been involved in IT for more than 20 years as a Web and database consultant. Microsoft first awarded Cotter the Microsoft SQL Server MVP award in 2001. Cotter received his bachelor of applied science degree in mechanical engineering from the University of Toronto and studied economics at the University of Calgary and computer science at UC Berkley. He is the author of a book on SQL Server transactional replication and is currently working on books on merge replication and Microsoft search technologies.
    Copyright 2007 TechTarget


    Rate this Tip
    To rate tips, you must be a member of SearchSQLServer.com.
    Register now to start rating these tips. Log in if you are already a member.




    Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


    RELATED CONTENT
    SQL Server replication
    SQL Server replication methods: Snapshot, merge or transactional
    Licensing a standby server for SQL Server replication
    Upgrade live applications to SQL Server 2005 for high availability
    Tool to synchronize two SQL Server databases
    Synchronize databases on two separate active servers
    SQL Server Blog Watch
    Simplify SQL Server replication
    Replication techniques in SQL Server
    Podcast: SQL Server high availability options
    Managing identity columns with replication in SQL Server

    SQL Server performance and tuning
    A guide to advanced new features in SQL Server Management Studio 2008, part 2
    Top 10 SQL Server Tips of 2008
    Tutorial: Performance tuning SQL Server via queries, indexes and more
    A guide to basic new features in SQL Server Management Studio 2008, part 1
    Tutorial: SQL Server indexing tips to improve performance
    SQL Server virtualization pros and cons: Weigh the performance impact
    How to configure storage in SQL Server DB with more writes than reads
    Avoid cursors in SQL Server with these methods to loop over records
    Solve SQL Server errors and more from the DBA trenches -- part 2
    Tutorial: Learn SQL Server basics from A-Z

    SQL Server availability
    How to configure storage in SQL Server DB with more writes than reads
    SQL Server replication methods: Snapshot, merge or transactional
    Licensing a standby server for SQL Server replication
    Get SQL Server log shipping functionality without Enterprise Edition
    Monitor database mirroring and replication after a SQL Server upgrade
    Upgrade live applications to SQL Server 2005 for high availability
    SQL Server high availability when upgrading to SQL Server 2005
    Tutorial: Migrating to SANs from local SQL Server disk storage
    How to restore SQL Server database to transition server during upgrade
    SQL Server memory configurations for procedure cache and buffer cache
    SQL Server availability Research

    RELATED GLOSSARY TERMS
    Terms from Whatis.com − the technology online dictionary
    replication  (SearchSQLServer.com)

    RELATED RESOURCES
    2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
    Search Bitpipe.com for the latest white papers and business webcasts
    Whatis.com, the online computer dictionary

    DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



    SQL Server Development - .NET, C#, T-SQL, Visual Basic
    HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
    About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
    SEARCH 
    TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

    TechTarget Corporate Web Site  |  Media Kits  |  Site Map




    All Rights Reserved, Copyright 2005 - 2009, TechTarget | Read our Privacy Policy
      TechTarget - The IT Media ROI Experts