Skip to main content
Background merges of a ReplacingMergeTree table remove old row versions eventually, so at any moment the table can still contain several versions of a row. SELECT ... FINAL returns the deduplicated result at query time. Its cost depends on how many rows it has to read and merge, so the way to keep it fast is to let the query skip data before deduplication starts: filter on the leading columns of the sorting key, and make the partition key part of the sorting key.

Example table

The table now holds 1,100,000 rows in 12 parts (two per month), and SELECT count() FROM orders FINAL returns 1,000,000.

Filter on the leading sorting key columns

FINAL only has to deduplicate the ranges that the primary index selects. EXPLAIN ESTIMATE shows how much a query reads. A filter on a column that is not in the sorting key reads the whole table:
A filter on the first column of the sorting key reads one granule per part:
Choose the sorting key so that it starts with the columns your FINAL queries filter on, followed by the columns that identify a row.

Keep the partition key inside the sorting key

In the example, the partition key toYYYYMM(created_at) uses only a column of the sorting key. Rows with the same sorting key therefore always land in the same partition, and ClickHouse uses this in two ways. Partitions are pruned before FINAL:
Each partition is deduplicated on its own instead of merging all parts of the table together. A partition that consists of a single merged part is read without any deduplication work, so a month that has already been merged into one part costs the same as without FINAL. ClickHouse decides this automatically when enable_automatic_decision_for_merging_across_partitions_for_final is on (the default). For this decision it looks at the primary key, not at the whole sorting key: if the table has an explicit PRIMARY KEY that is shorter than ORDER BY, the columns of the partition key must be in the PRIMARY KEY as well. With PRIMARY KEY customer_id on the example table, partitions are still pruned, but the parts of all the remaining partitions are merged together. If the partition key uses a column that is not in the sorting key, neither happens. On a copy of this table with ORDER BY (customer_id, order_id), the query above reads all 12 parts and 1,100,000 rows: since version 26.3, partitions are not pruned for FINAL on such a table, because two versions of a row could be in different partitions. If you know that all versions of a row always stay in one partition, you can change that by hand: Both settings can return duplicates or outdated versions if that assumption does not hold. The partition key of an existing table cannot be changed; to repartition, create a new table and copy the data.
Add a partition column to the sorting key only if its value never changes between the versions of a row. The sorting key is what identifies a row in ReplacingMergeTree: two rows that differ in any sorting key column are different rows and are never deduplicated. With PARTITION BY month ORDER BY (id, month), an order that gets a new version with another month stays in the result of FINAL twice, once for each month. With ORDER BY id, the query returns only the newer version.

What ClickHouse already does for you

Upgrading is often the cheapest optimization. These changes need no action, unless the compatibility setting pins an older version: Two more settings are worth knowing:
  • max_final_threads limits the number of threads that read data for a FINAL query. The default is the number of CPU cores, so check that a settings profile does not lower it.
  • query_plan_optimize_lazy_final (added in 26.4, off by default) can help queries whose WHERE condition matches few rows. ClickHouse first collects the primary keys of the rows that match the condition, uses them to narrow down the ranges that FINAL has to process, and reads the remaining columns only for the rows that are left. It falls back to the regular FINAL when the condition is not selective enough.