Repository navigation
Conversation
When a query with FINAL sorts in reverse order of the sorting key (e.g. ORDER BY key DESC LIMIT n), the read-in-order optimization now applies instead of falling back to a full read. ReplacingSortedAlgorithm learns a read_in_reverse mode: a row with a strictly higher version always replaces the selected one; among rows with equal (or absent) versions, the previously selected row is kept unless the current row comes from a newer data part. This mirrors the "last written row wins" rule of the direct reading order, because in the reverse reading order rows within one part arrive backwards while parts still arrive from the oldest to the newest one. The other engines keep the old behavior, since their merging algorithms rely on the direct order of rows: the sequence of sign rows in CollapsingMergeTree, the order of rows fed to order-dependent aggregate functions in AggregatingMergeTree, etc. On a 110M row ReplacingMergeTree table, `SELECT x FROM t FINAL ORDER BY x DESC LIMIT 1` reads 1.71 million rows instead of 110.1 million, in 0.22s instead of 4.5s. Closes: ClickHouse#58035 Co-Authored-By: Claude Fable 5 <[email protected]>
|
Workflow [PR], commit [8fade83] Summary: ✅
AI ReviewSummaryThis PR enables reverse-order read-in-order for Final Verdict✅ No new findings. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 140/142 (98.59%) · Uncovered code |
The page stated that the read-in-order optimization does not work with the FINAL modifier at all, which was already inaccurate for the direct order, and is now inaccurate for the reverse order of a ReplacingMergeTree table. The new setting is named without a link, because the settings pages are regenerated by the nightly documentation workflow, so its anchor does not exist yet and the blocking link check would fail on it. Co-Authored-By: Claude Fable 5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units119 translation units recompiled, 1321 s compile time in total, 119 of them have a recent master baseline. |
|
📊 Cloud Performance Report 🟢 AI verdict: PR #114479 adds the optimize_read_in_reverse_order_final optimization, which only affects FINAL reads of ReplacingMergeTree tables (the rest of the diff is a mechanical extraction of the range-splitting helper). None of the flagged ClickBench or TPC-H queries use FINAL or ReplacingMergeTree, so the diff cannot plausibly move their runtimes, and Q13/16/18/28/30/31 were downgraded as off-path variance. Three large improvements — ClickBench Q23 (×2.3 faster) and TPC-H Q7 (×8.6 faster) and Q8 (×2.2 faster) — remain flagged because their large, consistent deltas are locked by the trust override and match CPU drops, but given the unrelated code path they most likely reflect an environment/build difference. The low source-run count (6 vs 30) reinforces reading all of these with caution rather than as real effects of this change. clickbench🟢 1 improved · Flagged queries (8 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🟢 2 improved Flagged queries (2 of 22)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. Debug info
|
alexey-milovidov
left a comment
There was a problem hiding this comment.
Looks good and clear.
# Conflicts: # src/Core/Settings.cpp
|
🕵 Superseded by #115345. I could not push the clean merge resolution to the fork here: |
|
I could have merged master... |
Closes: #58035
Related: #58361
Related: #111609
When a query with
FINALsorts in reverse order of the sorting key, for exampleORDER BY key DESC LIMIT n, the read-in-order optimization was disabled and the query read the whole table. It now applies forReplacingMergeTree.ReplacingSortedAlgorithmlearns aread_in_reversemode. A row with a strictly higher version always replaces the selected one; among rows with equal (or absent) versions, the previously selected row is kept unless the current row comes from a newer data part. That mirrors the "last written row wins" rule of the direct reading order, because in the reverse reading order rows within one part arrive backwards while the parts still arrive from the oldest to the newest one. This is what makes the reverse read select the same row of a duplicate key group as a direct read, which is the correctness concern that stopped #58361.The other engines keep the previous behavior, since their merging algorithms depend on the direct order of rows: the sequence of sign rows in
CollapsingMergeTree, the order of rows fed to order-dependent aggregate functions inAggregatingMergeTree, and so on.On a 110 million row
ReplacingMergeTreetable with two overlapping parts,SELECT x FROM t FINAL ORDER BY x DESC LIMIT 1(measured on aRelWithDebInfobuild):Trade-off: as with the already existing direct-order in-order reads with
FINAL, an in-order plan disables verticalFINALand the splitting of parts ranges into intersecting and non-intersecting ones. A query that reads the full result withORDER BY key DESCand no smallLIMITmay therefore become slower on a wide or mostly merged table. The new settingoptimize_read_in_reverse_order_final(default enabled) turns the optimization off, andcompatibilitywith an earlier version restores the previous plans.Out of scope, to keep this change reviewable:
Mergetables overReplacingMergeTree, and the interaction withtopKThroughJoin, which keeps its own optimization for... FINAL LEFT JOIN ... ORDER BY key DESC LIMIT n. Both can follow separately.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Enable the read-in-order optimization for queries with the
FINALmodifier that sort in reverse order of the sorting key onReplacingMergeTreetables, so that queries such asSELECT ... FROM t FINAL ORDER BY key DESC LIMIT nread only the relevant tail of the data instead of the whole table. Can be disabled with the new settingoptimize_read_in_reverse_order_final.Workflow [PR]
Sync PR [sync-upstream/pr/114479]