Repository navigation
Description
Wrapping a single-column ORDER BY in parentheses makes MergeOnKey patch application ~480x slower and stops patch ranges from being pruned. ORDER BY k and ORDER BY (k) are semantically identical, but only the second is affected.
Reproducer:
DROP TABLE IF EXISTS lwu_bare;
DROP TABLE IF EXISTS lwu_paren;
CREATE TABLE lwu_bare (k UInt64, v UInt64, s UInt64) ENGINE = MergeTree ORDER BY k
SETTINGS enable_block_number_column = 1, enable_block_offset_column = 1, index_granularity = 1024;
CREATE TABLE lwu_paren (k UInt64, v UInt64, s UInt64) ENGINE = MergeTree ORDER BY (k)
SETTINGS enable_block_number_column = 1, enable_block_offset_column = 1, index_granularity = 1024;
INSERT INTO lwu_bare SELECT number, 0, rand64(number) FROM numbers(2000000);
INSERT INTO lwu_paren SELECT number, 0, rand64(number) FROM numbers(2000000);
OPTIMIZE TABLE lwu_bare FINAL;
OPTIMIZE TABLE lwu_paren FINAL;
UPDATE lwu_bare SET v = 1 WHERE s % 7 = 0;
UPDATE lwu_paren SET v = 1 WHERE s % 7 = 0;
ALTER TABLE lwu_bare DELETE WHERE v = 999999999 SETTINGS mutations_sync = 2;
ALTER TABLE lwu_paren DELETE WHERE v = 999999999 SETTINGS mutations_sync = 2;
SYSTEM FLUSH LOGS part_log;
SELECT
table,
(SELECT sorting_key FROM system.tables WHERE name = pl.table) AS sorting_key,
rows AS base_rows,
ProfileEvents['PatchesReadRows'] AS patch_rows_read,
round(ProfileEvents['ApplyPatchMergeOnKeyMicroseconds'] / 1000) AS apply_ms,
duration_ms
FROM system.part_log AS pl
WHERE table IN ('lwu_bare', 'lwu_paren')
AND event_type = 'MutatePart'
AND part_name NOT LIKE 'patch%'
AND rows > 1000
ORDER BY table;Result:
┌─table─────┬─sorting_key─┬─base_rows─┬─patch_rows_read─┬─apply_ms─┬─duration_ms─┐
│ lwu_bare │ k │ 2000000 │ 285718 │ 38 │ 321 │
│ lwu_paren │ (k) │ 2000000 │ 286481 │ 18271 │ 18925 │
└───────────┴─────────────┴───────────┴─────────────────┴──────────┴─────────────┘
Keys are strictly unique in both tables.
Pruning is also lost. With a separate 1-row part added before the UPDATE, mutating that part reads 1024 patch rows with ORDER BY k, but the entire patch part (42955 of 42955 rows) with ORDER BY (k), independent of where the part's key sits in the range.
ORDER BY (k, v) is not affected: multi-column tuples are normalised to k, v, while a one-element tuple stays (k).
index_granularity scales the effect: at the 8192 default the same reproducer gives 33 ms vs 2167 ms.
Version 26.9.1.510 (official build), plain MergeTree, single node.
Possibly related: #115732
Version info
- Resolved by: Fix patch pruning for parenthesized sorting keys #118878
- Merged into:
26.9.1.1287(included in26.9and later)
Activity
Preserve effective patch sorting keys with redundant parentheses