Skip to content

Choosing the apply method: Batch, Merge or Sequence

An incremental group's DML Replication setting decides how the Replicator Service writes the Capture Agent's changes into the destination: how many statements, in what order, how many tables at once, and where it commits. Every method gives the same result; they differ in speed, load on the destination, transaction behaviour, and how apply options and errors are handled.

See Group properties: Apply. Saving pauses the group; the new method is used when you resume it.

At a glance

Method How it applies changes Transactions Parallelism Destinations Best for
Batch (default) Table by table: one INSERT, UPDATE or DELETE per changed row. All tables of a batch are committed together, with the group's position. One table at a time per group. Any destination. Moderate volumes, members that need exact per-row checking or options Merge doesn't handle.
Merge Per table: loads the changes into a merge table in blocks of rows, then runs one MERGE (or the destination's equivalent) that applies only the last change of each row. Each table is committed on its own; the position is recorded when all tables of the batch are done. Up to Maximum Subscriptions tables at once. Supported destinations only. High volumes and backlogs, tables whose rows change many times.
Sequence Row by row, like Batch, but across all tables in the order the changes happened at the source. All tables of a batch are committed together. One row at a time per group. Any destination; not all sources can provide the order. Only when destination foreign keys require the original order.

Choosing a method

Start with Merge when the destination supports it (see Destinations and methods) and throughput matters: large volumes, backlogs, or rows that change many times between polls. Merge sends one statement per block of rows, collapses repeated changes to the same row, lets the destination apply them as a set, and works on several tables at once. First check that:

  • every member has key columns (a member without one gets inserts only, and a baseline after each update or delete);
  • no member uses Only Stream Change Data, and you don't rely on Change Data Logging or on exact Soft Delete behaviour;
  • you can accept that, while a batch is applied, some tables already have its changes and others don't yet.

Use Batch when:

  • the destination doesn't support Merge;
  • members only stream changes, log them through change data processing, or use Soft Delete;
  • the destination must be an exact copy, with each mismatch flagged with its row (Strict Apply Rules);
  • all tables must be committed together at each transaction boundary;
  • volumes are small and changes should arrive quickly: for a handful of rows per poll, Merge's fixed work per table (preparing and loading the merge table, running the MERGE) can cost more than the rows.

Use Sequence only when the destination has enforced foreign keys between members that you can't drop or disable, and the source can provide the order. It's the slowest method; put only the related tables in it.

The method is a group setting, so you can mix: put tables that need Batch or Sequence in their own groups.

How a batch of changes is fetched

All three methods start the same way. Each time the group polls (every Poll Interval, or earlier after a change data notification):

  1. The Replicator Service calls the Capture Agent's GETCHANGE3 procedure in the staging database with the group's last committed position, Row Limit and Transaction Limit.
  2. The Capture Agent returns a summary: one row per table with changes, each with the SELECT that reads its staged rows. A call's transactions are combined per table, so each table normally appears once, as one change set. A batch always ends on a transaction boundary: the Capture Agent stops adding transactions when the next would exceed the Row Limit or the Transaction Limit is reached, but never splits one. A call returns at most 100 change sets; a larger transaction continues in the next call.
  3. The Replicator Service opens a destination connection only if there are changes, applies them with the group's method, and records the new position in the control database.
  4. It acknowledges the position to the Capture Agent, which can then prune the staged changes: straight away when the Poll Interval is 30 seconds or longer, otherwise with its next request.
  5. It asks for the next batch until nothing is left, so one poll can apply several batches, each committed on its own.

The position is written in two steps around the destination commit. If the service stops in between, the next run applies the last batch again without flagging rows that are already there (or already gone).

How Batch works

Batch applies each batch table by table, one row at a time, and commits the whole batch at once.

  1. For each table in the summary, in turn, run its SELECT against the staging database.
  2. For each staged row, in capture order:
    1. Build the statement: INSERT for an insert; UPDATE ... WHERE <key> for an update, setting the columns the Capture Agent reports; DELETE ... WHERE <key> for a delete.
    2. Bind the row's values to the parameter markers (?) and execute it (SQLBindParameter, then SQLExecDirect): one statement and one round trip per row.
    3. Check the rows affected: an update or delete must touch exactly one row.
  3. When every table is done, commit the destination and record the position.

Batch doesn't use ODBC parameter arrays. That keeps error handling exact (the Replicator Service knows which row failed and why, see Errors), but every change costs a round trip, and the destination maintains its indexes and log row by row.

Within a batch, changes to different tables aren't applied in their original order, but they're committed together: at every commit, the destination reflects the source at a transaction boundary.

For sources other than SQL Server, LOB values aren't in the staged rows: the Replicator Service reads them from the source table, by key, for each changed row.

How Merge works

Merge copies each table's changes into a merge table in the destination database, then runs a single MERGE (or, where the destination has no suitable MERGE, a short sequence of set-based statements) into the destination table.

  1. Collect the batch. Read the whole summary (continuing across calls when a transaction spans more than 100 change sets).
  2. Hand out the tables. Each change set goes to a worker. Up to Maximum Subscriptions workers run at once, each with its own destination and staging connections.
  3. Prepare the merge table. Create it the first time (dropping any table with that name first), and empty it before later loads. See The merge table.
  4. Load it with parameter arrays. A fetch thread reads the staged rows in blocks, and an insert thread writes each block into the merge table with one execution of a prepared INSERT while the next block is read. The load is committed every BCP commit interval rows (20000 when that setting is 0).
  5. Merge. Run the MERGE (see The MERGE statement) and commit it.
  6. Clean up. With Merge In Place, empty the merge table again, unless the applyRetainMergeContent service property keeps its rows for troubleshooting. The worker then closes its destination connection, which removes a temporary merge table (or its rows).
  7. Record the position once every table of the batch is merged, and acknowledge it.

ODBC parameter arrays. Instead of executing an INSERT once per row, the Replicator Service binds a column of values to each parameter marker, sets the row count (SQL_ATTR_PARAMSET_SIZE) and executes the prepared statement once (SQLExecute): the driver sends all the rows in one go. The staged rows are read into row arrays (SQL_ATTR_ROW_ARRAY_SIZE) and the same buffers are bound to the merge table's INSERT, so each block is read once and written once. A block holds Replication Buffer Size divided by the row width: thousands of rows for narrow tables, fewer for wide tables and LOB columns (each LOB takes up to the Indeterminable-size column limit).

The load falls back to one INSERT per row when:

  • the table has LOB columns and the source isn't SQL Server, because the LOB values are then read from the source table row by row;
  • the useMultiRowApply service property is false, for example to work around a driver's problems with parameter arrays.

The merge table

The merge table has the destination table's columns, all nullable, plus SQ47114711TYPE (the kind of change) and SQ47114711SEQ (its sequence number at the source).

Every member has its own merge table, named after the member (the subscription), so two members writing to the same destination table, or two groups or services writing to tables with the same name, never share one.

Destination Merge In Place off Merge In Place on
SQL Server Global temporary table ##SQDRWT_<id> in tempdb Permanent table named with the Decoration
Oracle Global temporary table SQDRWT_<id> in the default schema of the destination connection Permanent table named with the Decoration
Db2 for LUW, Db2 for z/OS, Db2 for i Declared global temporary table SESSION.SQDRWT_<id> Permanent table named with the Decoration
MySQL Permanent table SQDRWT_<id> in the default database of the destination connection Permanent table named with the Decoration
Informix Temporary table SQDRWT_<id> (CREATE TEMP TABLE ... WITH NO LOG) Permanent table named with the Decoration
PostgreSQL Temporary table SQDRWT_<id> Permanent table named with the Decoration
Snowflake, Google BigQuery Temporary table _SQDRWT_<id> Permanent table named with the Decoration
SAP HANA Local temporary row table _SQDRWT_<id> Permanent table named with the Decoration
Exasol Permanent table named with the Decoration The same

<id> is 16 hexadecimal characters taken from the member's subscription ID.

  • Temporary tables belong to the worker's connection, which is closed after each table to remove them; they're created again for the next batch. Oracle's global temporary table stays in the schema (only its rows belong to the connection), so it's dropped and re-created the first time the table is merged after the group starts or its apply settings change, and otherwise only its rows are deleted.
  • Permanent tables stay in the destination database. MySQL always uses one, because a MySQL temporary table can't be read twice in one statement, which its MERGE equivalent needs. A permanent table is dropped (if it exists) and re-created the first time the table is merged after the group starts or its apply settings change, and emptied before each load; with Merge In Place also after each merge.
  • Emptying uses TRUNCATE TABLE (TRUNCATE TABLE ... IMMEDIATE on Db2 for LUW and Db2 for z/OS), or DELETE on Db2 for i and on the Oracle and Db2 for z/OS temporary tables.
  • SQL Server collation. tempdb can have another collation than the destination database, so the character key columns of a ## merge table use the destination database's collation, letting the MERGE compare the keys.

With Merge In Place, and always for Exasol, the merge table is named from the destination table, an eight-character hexadecimal tag from the member's subscription ID (1a2b3c4d below) and the Decoration:

Decoration Merge table for SALES.ORDERS
_M (default): a suffix SALES.ORDERS_1a2b3c4d_M
STG_: a prefix, because it ends with _ SALES.STG_ORDERS_1a2b3c4d
STAGE.: a schema, because it ends with . (the default suffix is added) STAGE.ORDERS_1a2b3c4d_M
STAGE.STG_: a schema and a prefix STAGE.STG_ORDERS_1a2b3c4d

An empty Decoration means the suffix _M (_m for PostgreSQL and Exasol). Without Merge In Place, the Decoration isn't used, except for Exasol.

A table that already has the merge table's name is dropped and re-created. The member's tag makes a clash with your own tables unlikely; don't create tables with these names, and keep a schema used in the Decoration for merge tables.

Merge tables from earlier releases

Earlier releases named the Merge In Place table without the tag (for example SALES.ORDERS_M). The Replicator Service no longer uses those tables and doesn't drop them: drop them yourself once the groups run on this release.

The MERGE statement

With the default applyCompressTier4 service property (true), the MERGE reads only the last change of each row: it groups the merge table by the key columns and keeps the row with the highest SQ47114711SEQ. A row inserted, updated five times and deleted within one batch costs one DELETE, or nothing if it never reached the destination. Then, matching on the key columns:

Last change of the row Row exists at the destination Row doesn't exist
Insert or update UPDATE of every non-key column INSERT
Delete (or the old key of a key change) DELETE Nothing

For SQL Server the statement looks like this (simplified):

WITH a AS (SELECT ID, MAX(SQ47114711SEQ) SQ47114711MAXSEQ FROM ##SQDRWT_<id>
           WHERE SQ47114711TYPE IN ('I','U','D','K','B') GROUP BY ID)
MERGE "SALES"."dbo"."ORDERS" AS tgt
USING (SELECT src.* FROM ##SQDRWT_<id> src JOIN a ON src.ID = a.ID AND src.SQ47114711SEQ = a.SQ47114711MAXSEQ) src
ON (src.ID = tgt.ID)
WHEN MATCHED AND src.SQ47114711TYPE IN ('B','D','K') THEN DELETE
WHEN MATCHED AND src.SQ47114711TYPE IN ('U','I') THEN UPDATE SET "QTY" = src."QTY", "STATUS" = src."STATUS"
WHEN NOT MATCHED AND SQ47114711TYPE IN ('I','U') THEN INSERT ("ID","QTY","STATUS") VALUES (src."ID",src."QTY",src."STATUS");

Other destinations get the same result with the statements their SQL allows:

Destination Statements run for each table
SQL Server, PostgreSQL, Snowflake, Google BigQuery, Db2 for LUW, Db2 for z/OS, Db2 for i One MERGE, as above, in the destination's syntax.
SAP HANA, Exasol Two MERGE statements, because they allow only one WHEN MATCHED clause: one for the deletes, then one for the inserts and updates.
Oracle, Informix A DELETE ... WHERE EXISTS for the rows whose last change is a delete, then a MERGE for the inserts and updates. Oracle's MERGE can only delete rows it has just updated; Informix's allows only one WHEN MATCHED clause.
MySQL MySQL has no MERGE. If the destination table has a primary key or unique index on the key columns: a DELETE joined to the merge table for the deletes, then INSERT ... SELECT ... ON DUPLICATE KEY UPDATE for the inserts and updates. Without one: the DELETE also removes the rows about to be inserted or updated, then a plain INSERT ... SELECT adds them, so that no duplicate rows are created.

For MySQL, the Replicator Service checks for that primary key or unique index when it sets up the member for incremental replication. Two statements are committed together, after the second.

When applyCompressTier4 is false, the Capture Agent keeps only the last change of each row in the SELECT it returns, moving that work to the staging database, and the MERGE uses every row of the merge table. Exasol always keeps only the last change of each row in the destination, whatever this property says.

Because the MERGE matches rows on the key, every member of a Merge group should have key columns (a unique index or primary key, or the RRN or ROWID), and the destination should keep the index the Replicator Service creates on them after a baseline; without it, each MERGE scans the destination table. A member without key columns is applied row by row: its inserts are applied, and an update or delete marks it as needing a baseline (see Tables without a usable key).

How Sequence works

Sequence applies a batch's changes in the order they happened at the source, across all its tables. Use it when the destination has foreign keys between members: in a Batch, a child row can arrive before its parent, because tables are applied one after the other, and an enforced foreign key rejects it.

  1. Open every table at once. Open each table's SELECT on the staging database: one cursor per table, on a single staging connection.
  2. Take the next change in order. Pick the table whose next staged row has the lowest sequence number, and apply its rows up to the sequence number of the next table's row.
  3. Apply each row as Batch does: one statement per row, row count checked.
  4. Repeat until every cursor is exhausted, then commit the batch and record the position.

Sequence is the slowest method: Batch's cost per row, plus switching between tables row by row. The order depends on the source providing sequence numbers; some sources, such as SQL Server, can't, whatever this setting says.

Destinations and methods

  • Merge needs statements the Replicator Service can write for the destination (see The MERGE statement): SQL Server, Oracle, Db2 for LUW, Db2 for z/OS (version 12 and later), Db2 for i (IBM i 7.1 and later), MySQL, Informix, PostgreSQL (version 15 and later, the first with MERGE), Snowflake, Google BigQuery, SAP HANA and Exasol. For other destinations, use Batch or Sequence. The Data Replicator Manager offers Merge only for these destinations and those with a Java apply connector.
  • Informix destinations need DELIMIDENT=Y in the connection.
  • Oracle with the bundled driver. The merge table load needs the driver to describe statement parameters, so the Replicator Service adds EDP=1 (EnableDescribeParam) to the connection, unless it already sets EDP or EnableDescribeParam.
  • Destinations with a Java apply connector show only Merge, selected for you; Merge In Place and Decoration aren't available.
  • Destinations with remote apply. The Capture Agent applies the changes itself: the Replicator Service asks it to push the next changes (the PUSHCHANGES procedure) with the group's Row Limit and Transaction Limit, waits for the result, and records the statistics it reports. The row-by-row and merge steps on this page then happen in the Capture Agent. With a Row Limit of 0, one request is made per poll.
  • Streaming destinations with an Apply extension receive the changes through the extension; see Destination properties: Apply extensions.

How apply options behave under each method

Under Merge, some apply options behave differently or aren't applied: for example, Strict Apply Rules behaves like Allow UPSERT, and Only Stream Change Data isn't applied. See Under each apply method for every option.

When a change can't be applied

What goes wrong Batch Sequence Merge
An update or delete matches no destination row The change is skipped and the subscription flagged ("Subscription ... flagged due to unexpected row count ..."). The batch carries on and is committed. As Batch. Not an error.
A value doesn't fit the destination column (SQLSTATE 22xxx) The change is skipped and the subscription flagged, with an analysis of the failing columns. As Batch. The table's load fails: the group reports a polling error, the table is rolled back and the batch is tried again at the next poll.
Duplicate key or other constraint (SQLSTATE 23xxx) With the incrementalDebug service property at 0 (default): polling error; the whole batch is rolled back and retried. Any other value flags the subscription and carries on; a negative value then pauses the group. As Batch. A failed MERGE is reported in an event naming the subscription, the table and the statement; the group reports a polling error and retries the batch at the next poll.
Any other SQL error Polling error; the batch is rolled back and retried after 60 seconds (applyMaxExceptionRetryInterval). The group pauses after Error Retry Limit consecutive errors. As Batch. Polling error for that table; the other tables of the batch stay committed; the batch is tried again at the next poll.
The staging SELECT fails (for example, a derived column can't be computed) Polling error, retried. The Replicator Service stops (see Known limits). Polling error, retried.

"Flagged" means the change is not applied, the subscription's flagged count goes up ("flagged a total of n times since the last baseline snapshot"), and the event shows the statement, the key and the values. A subscription's first three errors are logged in full (flaggedSubscriptionEventLogLimit). The destination row stays wrong until the member is baselined again. See Understanding replication error messages.

With Merge, the unit that fails is a table's block of changes, so the event can't name the row. To see the statements and values, record the group's runs with Start Tracing (see Tracing).

One statement that moves key values

Some databases check a key's uniqueness at the end of a statement, so one statement can swap the keys of two rows, or shift a range of keys (UPDATE ... SET SEQ = SEQ + 1). Applied one change at a time, such a statement passes through states where two rows hold the same key.

For Db2 for i, Oracle and Db2 for LUW sources, whose Capture Agent stages each row's identifier (on Db2 for LUW, Stelo Capture 7.0 or later), Batch, Sequence and Merge apply such a change set by the net change of each source row: the key and values each row ends with, and the keys no row holds any more. The destination then matches the source.

  • Members it doesn't apply to: members that archive all DML activity, only stream change data, ignore deletes or have criteria, and members keyed by the row identifier (which find the row by it, so a key change is an ordinary update).
  • Size limit: the change set is held in memory while it's netted, up to the netByRowMaxKB service property (50,000 KB by default, about 120,000 moved rows).
  • Otherwise (a larger change set, or another source), the changes are applied one at a time: a swap or shift then stops on a duplicate key at the destination, and the batch is retried at each poll. Raise netByRowMaxKB if the change set is larger than it; otherwise run a new baseline of the member.

Tuning with commit limits and Maximum Subscriptions

Setting Effect with Batch and Sequence Effect with Merge
Row Limit (default 20000) About how many rows each batch holds, so how big each destination transaction is. How many rows each table's merge table and MERGE handle per batch. Bigger batches spread Merge's fixed cost and collapse more repeated changes, but each MERGE is a bigger transaction, and a bad query plan has more to scan.
Transaction Limit (default 0) 1 commits each source transaction on its own at the destination. Limits how many source transactions a batch holds; each table is still committed separately.
Row Limit and Transaction Limit both 0 with Use Fixed Schedule Each poll applies everything staged. Each poll applies one batch holding everything staged, then waits for the next interval: the destination is updated once per Poll Interval.
Maximum Subscriptions (default 5) Not used for applying changes. How many tables are merged at the same time. Each uses a destination connection and a staging connection.
Poll Interval How often the group asks for changes when no notification arrives. The same; a longer interval gives Merge bigger batches.

Other settings used by Merge (group Replication Settings or service properties):

  • Replication Buffer Size: the size of each block loaded into the merge table.
  • BCP commit interval (rows): how often the merge table load is committed.
  • Indeterminable-size column limit (KB): the space reserved for each LOB value, which reduces the rows per block.
  • useMultiRowApply: false loads the merge table one row at a time.
  • applyCompressTier4: where the last change of each row is picked, in the destination (true) or in the staging database (false).

Practical guidance:

  • Keep the defaults until you have a reason to change them.
  • To catch up a large backlog on a destination that supports Merge, switch the group to Merge and raise the Row Limit (support has used 100,000 to 400,000), with the Poll Interval at a few minutes.
  • If the destination's log fills up or its transactions take too long, lower the Row Limit.
  • If one busy or slow table holds up the others, move it to a group of its own.
  • Lower Maximum Subscriptions if parallel merges overload the destination or its disks; raise it for groups with many tables that change at the same time.

Known limits

  • Sequence and a failing staging query. If a member's staging SELECT fails in a Sequence group (for example a derived column can't convert a value), the Replicator Service stops, and stops again each time it starts while that change is staged. To recover, start the service, switch the group to Batch right away, and baseline the member.
  • Merge commits table by table. A failure part-way through a batch leaves some tables with its changes and others without; the whole batch is merged again at the next poll, which gives the same result for the tables already done.
  • Merge errors don't count toward the Error Retry Limit. A table that fails to merge is reported as a polling error and retried at the next poll or change data notification, without the usual 60-second wait, and the group isn't paused by Error Retry Limit.
  • Merge statistics count every change as an inserted row.
  • Leftover merge tables. Oracle and MySQL merge tables (SQDRWT_<id>) and Merge In Place tables stay in the destination database; drop by hand those left by members you delete.