Skip to content

Optimize analysis of text index with using of generic exclusion search - #110530

Merged
CurtizJ merged 11 commits into
ClickHouse:masterfrom
CurtizJ:optimize-text-index-condition
Jul 20, 2026
Merged

CurtizJ merged 11 commits into
ClickHouse:masterfrom
CurtizJ:optimize-text-index-condition

Conversation

@CurtizJ

@CurtizJ CurtizJ commented Jul 15, 2026

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Performance Improvement

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

Optimized analysis of the text index.

Additional context

Depends on #109886.
A couple of optimizations: use exclusion search instead of iterating all granules; avoid creation of temporary roaring bitmaps for each range check.

CurtizJ and others added 4 commits July 13, 2026 23:49
Optimize the hot path of MergeTreeIndexConditionText::mayBeTrueOnGranule,
which is called for every mark of a part:

- Replace the construction of a temporary roaring bitmap for the mark's row
  range and and_cardinality with roaring_bitmap_intersect_with_range, an
  allocation-free intersection check. The per-mark checks are moved from
  MergeTreeIndexGranuleText to MergeTreeIndexConditionText next to the RPN
  evaluation.
- Resolve pointers to TextIndexAnalyzer::QueryBuilder for all RPN queries
  once in the analyzer's constructor instead of a hash lookup by the query
  hash per RPN element per mark.
- Use an inlined vector for the RPN stack to avoid a heap allocation per mark.
- Check the condition once on the whole row span of the requested ranges
  before the per-mark loop. The per-query checks are monotone with respect
  to the row range, so a false result proves the whole part can be skipped.

Co-Authored-By: Claude Fable 5 <[email protected]>
Look up the query builder in mayBeTrueOnGranule by the query hash
instead of the index in the flattened list of RPN queries. The hash of
TextSearchQuery is precomputed in its constructor, so the lookup is a
single hash-map probe anyway, and the extra vector of resolved pointers
(along with the hidden requirement that query_builders is never
rehashed after the constructor) is not worth the complexity.

Co-Authored-By: Claude Fable 5 <[email protected]>
Replace the per-mark loop for the text index in filterMarksUsingIndex
with genericExclusionSearch, the same algorithm that is used for the
analysis of the primary key. The per-query checks in mayBeTrueOnGranule
are monotone with respect to the row range: if the condition cannot be
true on a range, it cannot be true on any of its subranges. So a range
proven to have no matches is dropped as a whole without checking its
marks one by one. It also generalizes the previous check of the
condition on the whole row span of the requested ranges. The disjunction
analysis keeps the per-mark loop because it needs partial results for
every mark.

filterMarksUsingIndex now takes the query Settings to read
merge_tree_coarse_index_granularity, and MergeTreeSkipIndexReader
stores the query context to pass the settings at read time.

Co-Authored-By: Claude Fable 5 <[email protected]>
Mirror merge_tree_coarse_index_granularity and
merge_tree_generic_exclusion_search_max_steps into
MergeTreeReaderSettings instead of passing the query Settings to
filterMarksUsingIndex, so MergeTreeSkipIndexReader does not have to
store the query context. The step budget now also applies to the
exclusion search over the text index.

Co-Authored-By: Claude Fable 5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [cd0ae27]

Summary: ❌

job_name test_name status info comment
Stress test (arm_release) FAIL
Logical error: Unexpected null _row_number (STID: 2487-2f7e) FAIL cidb IGNORED

AI Review

Summary

This PR replaces the per-mark text-index analysis loop with generic exclusion search, adds the corresponding observability, and removes some reader-side overhead. The core algorithm looks sound, but the new text-index pruning now depends on exclusion-search settings that are not reflected in the skip-index query-condition-cache key, so different budgets/granularities can reuse stale cached mark ranges and lose the pruning the query asked for.

Findings

⚠️ Majors

  • [src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp:2634-2635] Cached skip-index exclusions are no longer partitioned by all range-shaping inputs. The new text-index path now depends on merge_tree_coarse_index_granularity and merge_tree_generic_exclusion_search_max_steps, but getSkipIndexProfiledConditionHash still ignores both. Two otherwise identical queries can therefore share one QueryConditionCache entry even when they asked for different pruning budgets, so a coarse result cached by a low-budget run can be silently reused by a later unlimited-budget run. Suggested fix: salt the skip-index cache key with these exclusion-search settings (and any other knobs that intentionally change the accepted mark ranges for skip-index analysis), or avoid caching this path until the key is complete.
Final Verdict

⚠️ Changes requested.

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Jul 15, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

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

This change is confined to the full-text (text) skip-index code path — it swaps the per-mark scan for a generic exclusion search and reworks the text-index reader's cursor bookkeeping. ClickBench Q15 exercises no text index, so it cannot plausibly benefit from this diff. The lone flagged row (Q15, clickbench, -16.5%) shows source and base medians both sitting inside master's current variance band with essentially unchanged CPU (2595 vs 2602 ms), so the deterministic envelope suppression to no_change is correct: this is run-to-run variance, not a PR effect. No real regressions or improvements to report; the benchmark's own text-index perf test is the relevant coverage for this change.

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 2caac0c3-625b-4387-8924-f09834a4b792
  • MIRAI run: 9a260076-ac41-4b0c-9049-4db1611c5503
  • PR check IDs:
    • clickbench_443395_1784558968
    • clickbench_443401_1784558968
    • clickbench_443407_1784558968
    • tpch_adapted_1_official_443413_1784558968
    • tpch_adapted_1_official_443419_1784558968
    • tpch_adapted_1_official_443441_1784558968

@CurtizJ

CurtizJ commented Jul 17, 2026 •

Copy link
Copy Markdown
Member Author

A synthetic test for text index analysis with 1M granules:
Screenshot 2026-07-17 at 23 05 39

CurtizJ and others added 2 commits July 18, 2026 13:47
Mirror the primary-key instrumentation in the text-index branch of the
generic exclusion search: new profile events
`TextIndexGenericExclusionSearchAlgorithm` and
`TextIndexGenericExclusionSearchStepLimitReached`, and report the
reached step limit in the trace log. Add a test that runs the same
text-index predicates under several `merge_tree_generic_exclusion_search_max_steps`
budgets and `merge_tree_coarse_index_granularity` values and checks
identical results and the expected profile events.

Resolves review comments:
ClickHouse#110530 (comment)
ClickHouse#110530 (comment)

Co-Authored-By: Claude Fable 5 <[email protected]>
{
auto skip_index_read_result = index_read_result->skip_index_read_result;
chassert(mark < skip_index_read_result->granules_selected.size());
const auto & skip_index_read_result = *index_read_result->skip_index_read_result;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was an unnecessary copy of std::shared_ptr. It's not very important, but I've seen related traces in perf top, and it looks like a perf test related to skip index analysis has sped up.

Image

main_reader_->all_mark_ranges,
main_reader_->settings)
, index(std::move(index_))
, condition_text(assert_cast<const MergeTreeIndexConditionText *>(index.condition_template->generateUnsubstituted().get()))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was a regression because generateUnsubstituted locks a mutex, and it was called for each granule.

Comment on lines +75 to +79
search_queries.push_back(condition_text->getSearchQueryForVirtualColumn(column.name));
}

lazy_cursors.resize(columns_.size());
prebuilt_cursors.resize(columns_.size());

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A small optimization to avoid lookups into the hash map for each granule.

@clickhouse-gh

clickhouse-gh Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 80.10% 86.20% +6.10%
Functions 91.00% 92.00% +1.00%
Branches 72.30% 78.20% +5.90%

Changed lines: Changed C/C++ lines covered: 268/279 (96.06%) · Uncovered code

Full report · Diff report

@CurtizJ CurtizJ self-assigned this Jul 20, 2026
@CurtizJ
CurtizJ added this pull request to the merge queue Jul 20, 2026
Merged via the queue into ClickHouse:master with commit 3881f8d Jul 20, 2026
176 of 178 checks passed
@CurtizJ
CurtizJ deleted the optimize-text-index-condition branch July 20, 2026 17:52
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 20, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Mirror the primary-key instrumentation in the text-index branch of the
generic exclusion search: new profile events
`TextIndexGenericExclusionSearchAlgorithm` and
`TextIndexGenericExclusionSearchStepLimitReached`, and report the
reached step limit in the trace log. Add a test that runs the same
text-index predicates under several `merge_tree_generic_exclusion_search_max_steps`
budgets and `merge_tree_coarse_index_granularity` values and checks
identical results and the expected profile events.

Resolves review comments:
ClickHouse#110530 (comment)
ClickHouse#110530 (comment)

Co-Authored-By: Claude Fable 5 <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…-condition

Optimize analysis of text index with using of generic exclusion search
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

2 participants