Repository navigation
Avoid quadratic time in merge() over tables with many columns - #124115
Conversation
`StorageMerge::getColumnsDescriptionFromSourceTablesImpl` inserts each source column with `ColumnsDescription::add(column, prev_column_name)`, and `getNameRange` resolved the after-column by a linear scan of the sequenced index, so building the columns of a `merge()` table function or a `Merge` table was quadratic in the column count. Look the exact name up in the existing `ordered_unique` name index instead and keep the linear prefix scan only for the Nested-prefix fallback. Names are unique, so the result is the same element the old loop found. Also stop copying the whole `ColumnsDescription` (which deep-clones every default expression) where it is only read: the source table's columns in `getColumnsDescriptionFromSourceTablesImpl` and the child's columns in `ReadFromMerge::getModifiedQueryInfo`. Add a performance test with a 2049-column source table. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review: my own cold review plus an independent model (engine: codex, 1 round, 0 findings).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261006-004600 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20261006-000110 |
|
Workflow [PR], commit [1a3c94f] Summary: ✅
AI ReviewSummaryThis PR removes the quadratic exact-name lookup from Final Verdict
LLVM Coverage ReportMeasured on commit 1a3c94f.
Changed lines: Changed C/C++ lines covered: 15/15 (100.00%) · Uncovered code |
|
📊 Cloud Performance Report ✅ AI verdict: no significant changes detected. K_source=6 K_base=30 flagged=0/65 clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
Build profile diff (arm_release)Commit See the job log for details. |
A single merge() query over the 2049-column table runs in about 10 ms on a release build, below the 0.1 s minimum of the performance tests. Run 20 of them from a query file, as one measured entry. They are separate queries because identical table functions in one query are resolved only once. With the CI amd_binary builds and tests/performance/scripts/perf.py on two local servers: master 0.337 s, this PR 0.174 s, p = 1e-16 (previously 19.2 ms and 10.0 ms). Co-Authored-By: Claude Opus 5.5 <[email protected]>
merge over tables with many columns linear in the column count
Related: #118156
Related: #118318
Related: #123926
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Queries to the
mergetable function and creation ofMergetables are faster when the source tables have many columns: combining the source columns took time quadratic in the number of columns.Description
StorageMerge::getColumnsDescriptionFromSourceTablesImpladds each source column withColumnsDescription::add(column, prev_column_name)to keep the source order, andgetNameRangefound theaftercolumn by a linear scan of the sequenced index, although the container has anordered_uniqueindex on the name. So everymerge(...)query paid N^2/2 name comparisons (2.4M for the 2202 columns ofsystem.metric_log, which 97system.dashboardscharts read throughmerge('system', '^metric_log')).getNameRangenow looks the exact name up in the name index and keeps the linear prefix scan only for the Nested-prefix fallback. Names are unique, so it returns the same column as before. Two read-only whole-ColumnsDescriptioncopies on the same path (each clones every default expression) become references.This is the
StorageMergepart of the slowdown asked about in #118156; the analyzer part is #123926.Debug build, median of 30 queries over an N-column table with one row:
merge(), beforemerge(), afterWhat
merge()adds over the direct read grew 3.1-3.3x per doubling before and 2.0-2.1x after. Oversystem.metric_logthat overhead drops from 20 to 12 ms (wide schema) and from 29 to 19 ms (bucketed). The new performance testmerge_table_function_many_columnsuses 2049 columns and times 20merge()queries as one.Workflow [PR]
Sync PR [sync-upstream/pr/124115]
Version info
26.10.1.1667-master(included in26.10and later)