Skip to content

Read text index virtual columns right before the first PREWHERE step that uses them - #124880

Merged
CurtizJ merged 9 commits into
ClickHouse:masterfrom
CurtizJ:text-index-better-prewhere-order
Oct 14, 2026
Merged

CurtizJ merged 9 commits into
ClickHouse:masterfrom
CurtizJ:text-index-better-prewhere-order

Conversation

@CurtizJ

@CurtizJ CurtizJ commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

Related: #124595

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Speed up queries that filter by a text index together with other PREWHERE conditions, such as the dynamic filter of ORDER BY ... LIMIT queries.


Workflow [PR]
Sync PR [sync-upstream/pr/124880]

Version info

  • Merged into: 26.10.1.2396-master (included in 26.10 and later)

…that uses them

The steps that read the `__text_index_*` virtual columns were always placed before all PREWHERE
steps, so the text index was read for every selected row, ahead of cheaper and more selective
conditions and of the `__topKFilter` dynamic filter. `getPrewhereActions` now inserts each index
read step right before the first step that uses its columns, and reads the columns that no step
uses after the last PREWHERE step. Steps for index tasks without columns are no longer created.

Adjacent conditions on the virtual columns of one index share a step (like conditions on
subcolumns of one column, via a storage name override in `tryBuildPrewhereSteps`), so that the
index reader fills them in one pass and keeps the locality of the query-local postings cache.

On 30M log rows sorted by `(service, ts)` with `... ORDER BY ts DESC LIMIT 100`, the text index
reader reads ~16x fewer rows; queries are 6-17% faster when the text predicate is not trivial.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c3d2f83]

Summary: ✅


AI Review

Summary

This PR moves text-index virtual-column reads to the latest possible read step and teaches PREWHERE step splitting to track index usage separately from physical-column usage, which is the right shape for the intended optimization. I did not find a remaining correctness issue in the current diff or in the previously discussed threads, but I cannot approve it yet because the PR is categorized as Performance Improvement and the review artifacts still do not show the actual before/after result of the newly added dedicated text-index benchmark.

Requests for evidence
  • ⚠️ Please attach the measured before/after result for tests/performance/text_index_prewhere_read_step_order.xml (or the CI artifact link). The public cloud-benchmark comment on the PR reports no_change and explicitly says those suites do not exercise text indexes, so it does not substantiate this PR's Performance Improvement claim.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required action: add the benchmark result for text_index_prewhere_read_step_order to the PR discussion so the Performance Improvement changelog claim is backed by data.

@clickhouse-gh clickhouse-gh Bot added pr-performance Pull request with some performance improvements comp-text-index Full-text search: inverted/text index in MergeTree (tokenizers, posting lists, GIN-style index bu... labels Oct 9, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit c3d2f83a237422e9035be9f380f6e89c999d297a was not compared: the CI logs cluster did not answer (every POST attempt failed with an exception).

See the job log for details.

@CurtizJ
CurtizJ marked this pull request as ready for review October 11, 2026 13:34
The test runs two queries over a log table with a text index. In one, a manual
`PREWHERE` on `host` is much cheaper than reading the index. In the other, the
dynamic top-K filter on `ts` rejects most granules before the index is read.
`ts` is in the sorting key only through an expression, so the primary key does
not prune granules by the top-K threshold, and the `PREWHERE` filter does the
work.

Local perf.py, base vs this PR, 20M rows, three runs: the manual `PREWHERE`
query is 46-48% faster and the top-K query 35-37% faster.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 31 queries analysed

This PR changes how text-index columns are read during PREWHERE: they are now read right before the first step that needs them. None of the flagged queries can reach that code, because the ClickBench hits table has no text index. In that case the new step-placement code exits right away, and the step-splitting code only scans an empty index list. The ClickBench Q33 (-8.1%) and Q34 (-8.0%) improvements are downgraded to not sure: both are unfiltered GROUP BY queries, noisy on master and inside master's recent range. Q28 (+6.8%) stays not sure, with flat CPU time and mixed signals from the two tests. TPC-H showed no flagged changes, and the PR's own new text-index perf test is the right place to judge its effect.

clickbench

⚠️ 3 inconclusive

Flagged queries (3 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 28 not_sure 1001 1070 +6.8% 0.0048 cpu: Already marked not sure. The +6.8% shift is borderline, CPU time is flat and ClickBench defines no text indexes, so the reordered index read steps never run here.
⚠️ 33 not_sure 1127 1036 -8.1% <0.0001 aggregation: This is a GROUP BY with no filter, and the hits table has no text index, so the PREWHERE index-step code never runs. The -8.1% falls inside master's recent range on a noisy query.
⚠️ 34 not_sure 1123 1033 -8.0% <0.0001 aggregation: This is a GROUP BY with no filter, and the hits table has no text index, so the PREWHERE index-step code never runs. The -8.0% falls inside master's recent range on a noisy query.

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

🟢 No significant changes

Debug info
  • StressHouse run: d0a71f2b-7c1b-4350-a41f-486dc641eadb
  • MIRAI run: 483c59f3-35ac-461f-b6a7-f5a54efc5f8f
  • PR check IDs:
    • clickbench_38530_1791926583
    • clickbench_38648_1791926602
    • clickbench_38807_1791926674
    • tpch_adapted_1_official_38843_1791926676
    • tpch_adapted_1_official_39147_1791926777
    • tpch_adapted_1_official_39259_1791926809

Walk the PREWHERE steps once and, before each step, add a read step per index
for the virtual columns that the step uses and that are not read yet. This
replaces the precomputed inputs of all steps and the map keyed by position and
index name, and does nothing when there are no index read tasks.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
CurtizJ and others added 5 commits October 12, 2026 12:43
`tryBuildPrewhereSteps` takes `IndexReadTasks` instead of a map from virtual
columns to index file names. `resolveNodeInfo` puts a virtual column of an
index into `NodeInfo::required_indexes` instead of passing the index name off
as a storage column, and adjacent conditions share a step when
`NodeInfo::readsSameColumns` holds for both storage columns and indexes.

`addIndexReadSteps` again resolves the first step that uses each virtual
column once, tracking only the columns of indexes, and places the read steps
by `(position, index name)` instead of scanning every step for every index
through a callback.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Replace the map keyed by `(position, index name)` with a vector of
`steps.size() + 1` elements, one per step position, each holding the
columns to read for every index read task. Empty elements are skipped.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@CurtizJ

CurtizJ commented Oct 14, 2026

Copy link
Copy Markdown
Member Author

Perf tests:
Screenshot 2026-10-14 at 13 53 30

@KochetovNicolai KochetovNicolai self-assigned this Oct 14, 2026
@CurtizJ
CurtizJ added this pull request to the merge queue Oct 14, 2026
Merged via the queue into ClickHouse:master with commit aabf43b Oct 14, 2026
669 of 696 checks passed
@CurtizJ
CurtizJ deleted the text-index-better-prewhere-order branch October 14, 2026 17:54
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 14, 2026
thevar1able pushed a commit to actueleai/ClickHouse that referenced this pull request Nov 24, 2026
… runtime filter in the query condition cache"

This reverts commit 64da8a0, reversing
changes made to be89302.

Conflicts resolved by keeping the later changes in the same lines:
`is_query_prewhere` (ClickHouse#130645), the `last_reader_evaluating_defaults`
argument (ClickHouse#128452), `readsSameColumns` (ClickHouse#124880), `follows_top_k_filter`
(ClickHouse#129882), and the PREWHERE query condition cache write of ClickHouse#115522 in
its own form before it merged ClickHouse#122380.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-text-index Full-text search: inverted/text index in MergeTree (tokenizers, posting lists, GIN-style index bu... pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants