clickhouse-conventions
Step 6 of the pipeline (WORKFLOW.md), for ClickHouse tables and the writers that feed them. The premise:
ClickHouse is built for large, immutable, sorted parts, so the ordering key is fixed at creation, every insert
makes a part, and every update rewrites parts. A habit from a row-oriented database (a surrogate key first, a
row per insert, an update in place) works against the engine instead of failing loudly.
When
- A MergeTree table is created, or its
ORDER BY, PARTITION BY or column types are chosen.
- An application inserts rows, often in small batches, or the server reports "too many parts".
- Rows must be corrected, deduplicated or deleted.
- A query is slow and a skipping index,
FINAL or OPTIMIZE is proposed.
Steps
Read only the section the task meets. They are independent.
| § |
Covers |
Read it when |
File |
| 1 |
Schema: ordering key, partitioning, Nullable, LowCardinality, numeric width |
a table is created or its key, partition or types change |
01-schema.md |
| 2 |
Inserts: batch size, async inserts, retries |
a writer is built, or "too many parts" appears |
02-inserts.md |
| 3 |
Mutations and merges: updates, deletes, ReplacingMergeTree, OPTIMIZE FINAL, skipping indexes |
rows must change or disappear, or a query is tuned |
03-mutations-and-merges.md |
Output / checkpoint
Each rule was exercised on the target server version: the ordering key was chosen from the real WHERE clauses
and the rows read were compared with EXPLAIN indexes = 1 (§1.1), a load test inserted at the intended rate
and the part count stayed bounded (§2.1), and a delete or correction path was run and observed in
system.mutations (§3.1). A rule checked by reading documentation only is not verified.
Guardrails
- Never create a MergeTree table without deciding the ordering key from the queries;
ORDER BY tuple() means
full scans (§1.1).
- Never use a high-cardinality partition key, and never partition to speed up queries (§1.2).
- Never insert row by row; batch on the client or use asynchronous inserts that wait for the flush (§2.1, §2.2).
- Never run frequent
ALTER TABLE ... UPDATE or DELETE as part of normal writes (§3.1).
- Never schedule
OPTIMIZE ... FINAL (§3.3).
- Versions: each rule names its version where one is stated; the documentation read was current at 2026-10-08.
Nothing was run while writing this block.
Origin
Rewritten from the ClickHouse documentation (facts only, no text copied) and the ClickHouse agent skills
(Apache-2.0), read 2026-10-08. 🟡: never run by us; open points are in
references/origin.md. A new engine block with no same-topic block. Overlaps with skills/sql-conventions (2026-10-19); cross-referenced, not merged. Pipeline-level ingestion
design is in data-pipeline-conventions.
1---2name: clickhouse-conventions3description: "Use when designing or reviewing a ClickHouse MergeTree table or the code that writes to it: choosing ORDER BY (the ordering key), PARTITION BY and its cardinality, avoiding Nullable, LowCardinality and narrow numeric types, batch size and async_insert for inserts, the too-many-parts error, why ALTER TABLE UPDATE and DELETE mutations hurt, lightweight DELETE, ReplacingMergeTree duplicates, OPTIMIZE FINAL, and data skipping indexes."4metadata:5 source: https://skillmd.com/skills/techmefr/clickhouse-conventions6---78# clickhouse-conventions910Step 6 of the pipeline (`WORKFLOW.md`), for ClickHouse tables and the writers that feed them. The premise:11**ClickHouse is built for large, immutable, sorted parts, so the ordering key is fixed at creation, every insert12makes a part, and every update rewrites parts**. A habit from a row-oriented database (a surrogate key first, a13row per insert, an update in place) works against the engine instead of failing loudly.1415## When16- A MergeTree table is created, or its `ORDER BY`, `PARTITION BY` or column types are chosen.17- An application inserts rows, often in small batches, or the server reports "too many parts".18- Rows must be corrected, deduplicated or deleted.19- A query is slow and a skipping index, `FINAL` or `OPTIMIZE` is proposed.2021## Steps2223**Read only the section the task meets.** They are independent.2425| § | Covers | Read it when | File |26|---|---|---|---|27| 1 | Schema: ordering key, partitioning, Nullable, LowCardinality, numeric width | a table is created or its key, partition or types change | [`01-schema.md`](./references/01-schema.md) |28| 2 | Inserts: batch size, async inserts, retries | a writer is built, or "too many parts" appears | [`02-inserts.md`](./references/02-inserts.md) |29| 3 | Mutations and merges: updates, deletes, ReplacingMergeTree, OPTIMIZE FINAL, skipping indexes | rows must change or disappear, or a query is tuned | [`03-mutations-and-merges.md`](./references/03-mutations-and-merges.md) |3031## Output / checkpoint32Each rule was exercised on the target server version: the ordering key was chosen from the real `WHERE` clauses33and the rows read were compared with `EXPLAIN indexes = 1` (§1.1), a load test inserted at the intended rate34and the part count stayed bounded (§2.1), and a delete or correction path was run and observed in35`system.mutations` (§3.1). A rule checked by reading documentation only is not verified.3637## Guardrails38- Never create a MergeTree table without deciding the ordering key from the queries; `ORDER BY tuple()` means39 full scans (§1.1).40- Never use a high-cardinality partition key, and never partition to speed up queries (§1.2).41- Never insert row by row; batch on the client or use asynchronous inserts that wait for the flush (§2.1, §2.2).42- Never run frequent `ALTER TABLE ... UPDATE` or `DELETE` as part of normal writes (§3.1).43- Never schedule `OPTIMIZE ... FINAL` (§3.3).44- Versions: each rule names its version where one is stated; the documentation read was current at 2026-10-08.45 Nothing was run while writing this block.4647## Origin48Rewritten from the ClickHouse documentation (facts only, no text copied) and the ClickHouse agent skills49(Apache-2.0), read 2026-10-08. 🟡: never run by us; open points are in50[`references/origin.md`](./references/origin.md). A new engine block with no same-topic block. Overlaps with `skills/sql-conventions` (2026-10-19); cross-referenced, not merged. Pipeline-level ingestion51design is in `data-pipeline-conventions`.