Skip to main content

Merge-on-Write

Merge-on-write and merge-on-read are the two storage implementations of the Unique Key Model. Both keep Key columns unique and return the same query results, but they resolve duplicate Keys at different times, and that affects read and write performance. Merge-on-write is the default and the recommended starting point for most workloads.

Comparison​

ImplementationMerge timingWrite performanceQuery performanceApplicable scenarios
Merge-on-write (default)At write timeModerateHighMost scenarios, balancing query and write performance
Merge-on-readAt query or compaction timeHighLowerWrite-heavy, read-light scenarios

How merge-on-write works​

With merge-on-write, the query engine resolves duplicate keys as data is written. For each incoming row, it checks whether the Key already exists and marks any previous version as deleted in a delete bitmap. Queries use the bitmap to skip the marked rows, so they read only the latest version. Compaction later removes those rows from disk.

Because duplicates are resolved at write time:

  • Queries read a single version per Key, with no read-time merge.
  • The query engine can push filters down to the storage layer, so scans skip data that can't match.
  • Queries avoid the read-time merge of older row versions. Actual performance still depends on the workload and compaction.

The cost is on the write side. Each upsert looks up the primary key to find and mark the previous version, which adds some write overhead compared with merge-on-read. Use the default merge-on-write implementation when query performance and update support matter.

How merge-on-read works​

With merge-on-read, writes append data without resolving duplicate keys. Queries merge the stored versions of each key to return the latest version. Compaction consolidates the stored versions.

Because duplicates are resolved at read time:

  • Writes are lightweight, because they skip the primary-key lookup.
  • Queries must resolve versions of each key before applying filters on Value columns, which limits predicate pushdown and adds read-time work.
  • Additional versions between compactions can increase query-time merge work and latency.

It fits write-heavy, read-light pipelines, where write throughput matters more than query speed.

Choose an implementation​

  • Use merge-on-write (default) for most workloads, including real-time updates, dimension synchronization, and any case where query performance matters. It also enables features such as partial column updates.
  • Use merge-on-read only when you have far more writes than reads and you need to minimize write overhead.

The implementation is fixed at table creation and cannot be changed later through schema change, so decide before you create the table.

Set the implementation at table creation​

Set the enable_unique_key_merge_on_write table property when creating the table. The examples below use the same table name to show alternative definitions. Run only the definition for the implementation you choose. Because they use IF NOT EXISTS, running the second definition after the first does not change the existing table.

Merge-on-write (default):

CREATE TABLE IF NOT EXISTS example_tbl_unique
(
user_id LARGEINT NOT NULL,
user_name VARCHAR(50) NOT NULL,
city VARCHAR(20),
age SMALLINT,
sex TINYINT
)
UNIQUE KEY(user_id, user_name)
DISTRIBUTED BY HASH(user_id) BUCKETS 10
PROPERTIES (
"enable_unique_key_merge_on_write" = "true"
);

Merge-on-read (set the property to false):

CREATE TABLE IF NOT EXISTS example_tbl_unique
(
user_id LARGEINT NOT NULL,
user_name VARCHAR(50) NOT NULL,
city VARCHAR(20),
age SMALLINT,
sex TINYINT
)
UNIQUE KEY(user_id, user_name)
DISTRIBUTED BY HASH(user_id) BUCKETS 10
PROPERTIES (
"enable_unique_key_merge_on_write" = "false"
);

Capabilities that require merge-on-write​

Partial column update requires merge-on-write. It changes selected columns while preserving the existing values in omitted columns.