Part merges

The diagram below illustrates how parts in a standard [MergeTree](/docs/engines/table-engines/mergetree-family/mergetree) table are merged:

  

The DDL statement in the diagram above creates a `MergeTree` table with a sorting key `(town, street)`, [meaning](/docs/parts#what-are-table-parts-in-clickhouse) data on disk is sorted by these columns, and a sparse primary index is generated accordingly.

The ① decompressed, pre-sorted table columns are ② merged while preserving the table's global sorting order defined by the table's sorting key, ③ a new sparse primary index is generated, and ④ the merged column files and index are compressed and stored as a new data part on disk.

### Replacing merges[​](#replacing-merges "Direct link to Replacing merges")

Part merges in a [ReplacingMergeTree](/docs/engines/table-engines/mergetree-family/replacingmergetree) table work similarly to [standard merges](/docs/merges#standard-merges), but only the most recent version of each row is retained, with older versions being discarded:

  

The DDL statement in the diagram above creates a `ReplacingMergeTree` table with a sorting key `(town, street, id)`, meaning data on disk is sorted by these columns, with a sparse primary index generated accordingly.

The ② merging works similarly to a standard `MergeTree` table, combining decompressed, pre-sorted columns while preserving the global sorting order.

However, the `ReplacingMergeTree` removes duplicate rows with the same sorting key, keeping only the most recent row based on the creation timestamp of its containing part.

  

### Summing merges[​](#summing-merges "Direct link to Summing merges")

Numeric data is automatically summarized during merges of parts from a [SummingMergeTree](/docs/engines/table-engines/mergetree-family/summingmergetree) table:

  

The DDL statement in the diagram above defines a `SummingMergeTree` table with `town` as the sorting key, meaning that data on disk is sorted by this column and a sparse primary index is created accordingly.

In the ② merging step, ClickHouse replaces all rows with the same sorting key with a single row, summing the values of numeric columns.

### Aggregating merges[​](#aggregating-merges "Direct link to Aggregating merges")

The `SummingMergeTree` table example from above is a specialized variant of the [AggregatingMergeTree](/docs/engines/table-engines/mergetree-family/aggregatingmergetree) table, allowing [automatic incremental data transformation](https://www.youtube.com/watch?v=QDAJTKZT8y4) by applying any of [90+](/docs/sql-reference/aggregate-functions/reference) aggregation functions during part merges:

  

The DDL statement in the diagram above creates an `AggregatingMergeTree` table with `town` as the sorting key, ensuring data is ordered by this column on disk and a corresponding sparse primary index is generated.

During ② merging, ClickHouse replaces all rows with the same sorting key with a single row storing [partial aggregation states](https://clickhouse.com/blog/clickhouse_vs_elasticsearch_mechanics_of_count_aggregations#-multi-core-parallelization) (e.g. a `sum` and a `count` for `avg()`). These states ensure accurate results through incremental background merges.

- [What are part merges in ClickHouse?](#what-are-part-merges-in-clickhouse)- [Monitoring merges](#monitoring-merges)- [Concurrent merges](#concurrent-merges)- [Memory optimized merges](#memory-optimized-merges)- [Merge mechanics](#merge-mechanics)
          - [Standard merges](#standard-merges)- [Replacing merges](#replacing-merges)- [Summing merges](#summing-merges)- [Aggregating merges](#aggregating-merges)

Was this page helpful?