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
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:
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 keytoYYYYMM(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:
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:
do_not_merge_across_partitions_select_final = 1prunes partitions and deduplicates every partition separately. On the copy of the table, the query then reads 2 parts and 172,205 rows again.defer_partition_pruning_after_final = 0only brings back partition pruning.
What ClickHouse already does for you
Upgrading is often the cheapest optimization. These changes need no action, unless thecompatibility setting pins an older version:
Two more settings are worth knowing:
max_final_threadslimits the number of threads that read data for aFINALquery. 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 whoseWHEREcondition matches few rows. ClickHouse first collects the primary keys of the rows that match the condition, uses them to narrow down the ranges thatFINALhas to process, and reads the remaining columns only for the rows that are left. It falls back to the regularFINALwhen the condition is not selective enough.