Skip to content

Lesson 2 of 9 · 24m

MergeTree, Parts, and Sorting Keys

The storage model behind nearly every serious ClickHouse table: immutable parts, background merges, primary-key ordering, partitions, and why small inserts hurt.

Nearly everything operationally important in ClickHouse comes back to the MergeTree family. A MergeTree table is not one big sorted file. It is a set of immutable parts, each written once, merged in the background, and eventually replaced by larger parts. Once you understand parts, you understand insert batching, replication pressure, mutations, TTLs, and half the error messages in the logs.

An insert creates a part

CREATE TABLE events
(
    event_time DateTime,
    user_id UInt64,
    country LowCardinality(String),
    bytes UInt64
)
ENGINE = MergeTree
PARTITION BY toYYYYMM(event_time)
ORDER BY (country, event_time, user_id);

Every insert creates at least one new part on disk. Parts are sorted by the ORDER BY key and contain column files plus small metadata structures. Background merge threads combine smaller parts into bigger ones. This is why ClickHouse loves batches: one insert of 100,000 rows creates one healthy part; 100,000 inserts of one row create a parts explosion and a long queue of merge work.

The classic symptom is Too many parts (300) or a large number in system.parts with active = 1. The database did not suddenly become slow because SQL is hard; it became slow because you forced it to manage dust instead of bricks.

ORDER BY is the physical layout, not query sugar

The sorting key controls which rows sit near each other inside a part and which granules can be skipped. If most queries filter on tenant and time, the key should usually start with the selective dimensions in that order:

ORDER BY (tenant_id, event_time)

Then a query like this can skip most granules:

SELECT count()
FROM events
WHERE tenant_id = 42
  AND event_time >= today() - 7;

This is not a B-tree lookup in the OLTP sense. ClickHouse uses sparse primary indexes and granules — blocks of rows, commonly 8192 rows per granule — to decide what not to read. It is a pruning mechanism, not a point lookup machine.

Partitioning is for lifecycle management

PARTITION BY toYYYYMM(event_time) is common because partitions are coarse administrative units. You can drop, move, freeze, or apply TTLs to them efficiently:

ALTER TABLE events DROP PARTITION '2024-01';

Partitioning is not primarily a query accelerator. Do not create one partition per day unless your retention and operational needs demand it. Thousands of tiny partitions create metadata and merge overhead; dozens of monthly partitions are usually easier to live with.

Merges are a feature, not background noise

The merge process is how ClickHouse maintains write-optimized ingestion and read-optimized storage at the same time. It also means storage capacity is not just final data size. You need headroom for merges, because the new larger part exists before the older smaller parts can be removed.

Field notes

  • Batch inserts by thousands or tens of thousands of rows. If the producer cannot batch, put Kafka, a buffer table, or a small ingestion service in front of ClickHouse.
  • Pick the sorting key from real query patterns. Copying the primary key from Postgres is usually wrong.
  • Use system.parts, system.merges, and system.mutations before guessing about storage behavior.
  • OPTIMIZE TABLE events FINAL is a maintenance hammer, not something application code should call on a schedule. ReplacingMergeTree, SummingMergeTree, and AggregatingMergeTree obey the same parts-and-merges physics.

Was this lesson helpful?

Sign in to rate