FINAL clause speed

SELECT * FROM table FINAL

History

Partitioning

Proper partition design could speed up FINAL processing.

For example, if you have a table with Daily partitioning, you can:

  • After day end + some time interval during which you can get some updates run OPTIMIZE TABLE xxx PARTITION 'prev_day' FINAL
  • or add table SETTINGS min_age_to_force_merge_seconds=86400,min_age_to_force_merge_on_partition_only=1

In that case, using FINAL with do_not_merge_across_partitions_select_final will be cheap or even zero.

Example:

DROP TABLE IF EXISTS repl_tbl;

CREATE TABLE repl_tbl
(
    `key` UInt32,
    `val_1` UInt32,
    `val_2` String,
    `val_3` String,
    `val_4` String,
    `val_5` UUID,
    `ts` DateTime
)
ENGINE = ReplacingMergeTree(ts)
PARTITION BY toDate(ts)
ORDER BY key;

​
INSERT INTO repl_tbl SELECT number as key, rand() as val_1, randomStringUTF8(10) as val_2, randomStringUTF8(5) as val_3, randomStringUTF8(4) as val_4, generateUUIDv4() as val_5, '2020-01-01 00:00:00' as ts FROM numbers(10000000);
OPTIMIZE TABLE repl_tbl PARTITION ID '20200101' FINAL;
INSERT INTO repl_tbl SELECT number as key, rand() as val_1, randomStringUTF8(10) as val_2, randomStringUTF8(5) as val_3, randomStringUTF8(4) as val_4, generateUUIDv4() as val_5, '2020-01-02 00:00:00' as ts FROM numbers(10000000);
OPTIMIZE TABLE repl_tbl PARTITION ID '20200102' FINAL;
INSERT INTO repl_tbl SELECT number as key, rand() as val_1, randomStringUTF8(10) as val_2, randomStringUTF8(5) as val_3, randomStringUTF8(4) as val_4, generateUUIDv4() as val_5, '2020-01-03 00:00:00' as ts FROM numbers(10000000);
OPTIMIZE TABLE repl_tbl PARTITION ID '20200103' FINAL;
INSERT INTO repl_tbl SELECT number as key, rand() as val_1, randomStringUTF8(10) as val_2, randomStringUTF8(5) as val_3, randomStringUTF8(4) as val_4, generateUUIDv4() as val_5, '2020-01-04 00:00:00' as ts FROM numbers(10000000);
OPTIMIZE TABLE repl_tbl PARTITION ID '20200104' FINAL;

SYSTEM STOP MERGES repl_tbl;
INSERT INTO repl_tbl SELECT number as key, rand() as val_1, randomStringUTF8(10) as val_2, randomStringUTF8(5) as val_3, randomStringUTF8(4) as val_4, generateUUIDv4() as val_5, '2020-01-05 00:00:00' as ts FROM numbers(10000000);
​

SELECT count() FROM repl_tbl WHERE NOT ignore(*)

┌──count()─┐
│ 50000000 │
└──────────┘

1 rows in set. Elapsed: 1.504 sec. Processed 50.00 million rows, 6.40 GB (33.24 million rows/s., 4.26 GB/s.)

SELECT count() FROM repl_tbl FINAL WHERE NOT ignore(*)

┌──count()─┐
│ 10000000 │
└──────────┘

1 rows in set. Elapsed: 3.314 sec. Processed 50.00 million rows, 6.40 GB (15.09 million rows/s., 1.93 GB/s.)

/* more that 2 time slower, and will get worse once you will have more data */

set do_not_merge_across_partitions_select_final=1;

SELECT count() FROM repl_tbl FINAL WHERE NOT ignore(*)

┌──count()─┐
│ 50000000 │
└──────────┘

1 rows in set. Elapsed: 1.850 sec. Processed 50.00 million rows, 6.40 GB (27.03 million rows/s., 3.46 GB/s.)

/* only 0.35 sec slower, and while partitions have about the same size that extra cost will be about constant */

Since 26.2, enable_automatic_decision_for_merging_across_partitions_for_final=1 (default) auto-enables this when partition key columns are included in PRIMARY KEY

Light ORDER BY

All columns specified in ORDER BY will be read during FINAL processing, creating additional disk load. Use fewer columns and lighter column types to create faster queries.

Example: UUID vs UInt64

CREATE TABLE uuid_table (id UUID, value UInt64)    ENGINE = ReplacingMergeTree() ORDER BY id;
CREATE TABLE uint64_table (id UInt64,value UInt64) ENGINE = ReplacingMergeTree() ORDER BY id;

INSERT INTO uuid_table SELECT generateUUIDv4(), number FROM numbers(5E7);
INSERT INTO uint64_table SELECT number, number         FROM numbers(5E7);

SELECT sum(value) FROM uuid_table   FINAL format JSON;
SELECT sum(value) FROM uint64_table FINAL format JSON;

Results :

		"elapsed": 0.58738197,
		"rows_read": 50172032,
		"bytes_read": 1204128768

		"elapsed": 0.189792142,
		"rows_read": 50057344,
		"bytes_read": 480675040

Vertical FINAL Algorithm (24.1+)

When enable_vertical_final=1 (default since 24.1, except 24.5 where it was disabled due to a bug), ClickHouse uses a different deduplication strategy:

  • Marks duplicate rows as deleted instead of merging them immediately
  • Filters deleted rows in a later processing step
  • Reads different columns from different parts in parallel

This improves performance for queries that read only a subset of columns, as non-ORDER BY columns can be read independently from different parts.

PREWHERE and Row Policies with FINAL (25.12+)

Row policies are applied after FINAL by default (apply_row_policy_after_final=1; 25.12 and 26.2+, 26.1 had 0). PREWHERE is by default applied before FINAL deduplication. This can cause incorrect results when:

  • PREWHERE references columns that differ across duplicate rows
  • Row policies should filter based on the “winning” row values after deduplication

Use these settings when needed:

  • apply_prewhere_after_final=1 - Apply PREWHERE after deduplication
  • apply_row_policy_after_final=1 - Apply row policies after deduplication

Example problem: if you have ReplacingMergeTree with a deleted column and PREWHERE filters on it, without apply_prewhere_after_final=1 you may get wrong results because PREWHERE sees rows before FINAL picks the winner.

FINAL with skip indexes

  • use_skip_indexes_if_final and use_skip_indexes_if_final_exact_mode are enabled by default since 25.6 (exact mode was added in 25.5).
  • Skip indexes on PRIMARY KEY columns have lower overhead (no extra primary-key intersection check since 25.11), see PR 88368 .
  • Exact mode adds a recovery pass that also reads granules of other parts overlapping the selected ones, so a newer row version is never missed. Before 26.10 its cost grew with the total number of granules; since 26.10 it grows with the selected granules (PR 121799 ). Results and selected granules are identical. In our test (8M rows, 40 unmerged parts, selective index) it gave 4x at index_granularity=8 (417 → 103 ms), 1.3x at 64 and nothing at the default 8192; the index type does not matter.
  • Check that the index pays off: when it matches about 1 row in 200, exact mode was slower than use_skip_indexes_if_final=0. Compare both with EXPLAIN indexes = 1.

Lazy FINAL and lazy materialization

Two different optimizations are easy to confuse:

  • Lazy column materialization (query_plan_optimize_lazy_materialization, on by default): read only the filter and FINAL-merge columns first, fetch the rest for the rows that survive LIMIT. Since 26.7 it also works for FINAL + WHERE/PREWHERE + LIMIT without ORDER BY. It needs a filter and LIMIT up to query_plan_max_limit_for_lazy_materialization (10000); look for JoinLazyColumnsStep in EXPLAIN. On wide tables (3 x String(200), SELECT * ... WHERE f = 7 LIMIT 10) it cut the data read from 899 MB to 98 MB and the time from ~106 ms to ~28 ms; no gain with narrow columns, one part, or LIMIT 100000.
  • Lazy FINAL (query_plan_optimize_lazy_final, off by default since 26.4) re-reads the table by a primary key set built from the filtered rows. It helps only when the filter prunes marks (a contiguous key range or a unique id, no small LIMIT: ~430 → ~25 ms) and is slower when many rows match (+30-45% at 10-50% selectivity without LIMIT, up to 5x with LIMIT 1). Keep it off otherwise.

Tested on 26.4, 26.7, 26.8 and 26.10 with identical results in all combinations (including Nullable primary key, is_deleted with PREWHERE, text index filter). Caches were warm, so cold-disk gains may be larger.

Gotchas

  • 24.5: enable_vertical_final was off by default due to a bug; fixed in 24.6.
  • SummingMergeTree: SELECT ... FINAL keeps rows whose summed columns are all zero, while background merges and OPTIMIZE ... FINAL remove them. In 25.8 the row count also depended on the columns selected (count() returned 2, SELECT a returned 1 for the same data); this is consistent in 26.8+ (changelog entry for 26.10: PR 115032 ).
  • Partition pruning (26.3+): WHERE on the partition key is not used to prune partitions under FINAL, see defer_partition_pruning_after_final above.

Settings reference

SettingDefaultSinceDescription
do_not_merge_across_partitions_select_final020.10Skip cross-partition merging when partitions are pre-optimized
max_final_threads0 (auto)20.5Thread limit for FINAL processing
enable_vertical_final124.1Read columns in parallel from different parts
use_skip_indexes_if_final125.6Allow skip indexes with FINAL
use_skip_indexes_if_final_exact_mode125.5 (default 25.6)Rescan newer parts to ensure correctness with skip indexes
apply_prewhere_after_final025.12Apply PREWHERE after deduplication (needed when PREWHERE references non-PK columns)
apply_row_policy_after_final125.12 (0 in 26.1)Apply row policies after deduplication
optimize_move_to_prewhere_if_final123.2 (default 1 since 26.10)Move sorting-key conditions to PREWHERE for FINAL queries
defer_partition_pruning_after_final126.3Skip partition pruning under FINAL; set 0 only if a key never spans partitions
analyzer_compatibility_apply_final_to_all_joined_tables026.4 (default 0 since 26.6)Apply FINAL on the left-most JOIN table to all joined tables
query_plan_optimize_lazy_final026.4Re-read FINAL data by a primary key set from filtered rows; helps for clustered key ranges / unique ids only
query_plan_optimize_lazy_materialization126.7 for FINAL + LIMIT without ORDER BYFetch non-filter columns only for rows that survive LIMIT
enable_automatic_decision_for_merging_across_partitions_for_final126.2Auto-enable do_not_merge_across_partitions_select_final when partition key is in PK