Skip to content

Cache missing text index tokens - #112742

Merged
CurtizJ merged 5 commits into
ClickHouse:masterfrom
rorylshanks:negative-text-index-token-cache
Aug 3, 2026
Merged

CurtizJ merged 5 commits into
ClickHouse:masterfrom
rorylshanks:negative-text-index-token-cache

Conversation

@rorylshanks

@rorylshanks rorylshanks commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

Missing tokens were looked up in every part on every query. This caches those misses in the existing token cache, behind use_text_index_negative_tokens_cache (on by default).

ClickBench benchmark: 10 million rows, 1,280 parts, 200 queries per mode.

  • Query time: 139 ms → 111 ms
  • CPU: 27.9 s → 22.2 s
  • Dictionary reads: 256,000 → 0

Tested with a full build and the text-index cache stateless tests.

Changelog category (leave one):

  • Performance Improvement

Changelog entry:

Add optional caching for tokens missing from text indexes.

Repeated searches for a token that is absent from many text-index parts otherwise read each part dictionary again on every query. Store that result in the existing `TextIndexTokensCache` so negative entries share its capacity, eviction policy, and clear operation instead of introducing a parallel cache.

Gate negative entries behind the independently controlled `use_text_index_negative_tokens_cache` setting, disabled by default. When the setting is disabled, ignore an already cached negative entry and evaluate the dictionary, preserving query-level setting semantics. Separate profile events expose negative cache population and reuse.

A 10-million-row ClickBench `SearchPhrase` workload with 1,280 active parts measured about 20% lower server query time and CPU after warm-up; dictionary reads fell from 256,000 to zero across 200 measured queries. Keep this evidence in the pull request rather than adding a recurring performance test for the narrow opt-in optimization.

Tests: `ninja -C build clickhouse`; `04658_text_index_negative_tokens_cache`; `02346_text_index_dictionary_cache`; `03999_stateless_settings_history`.
@rorylshanks
rorylshanks requested a review from a team as a code owner July 31, 2026 12:56
@CurtizJ CurtizJ added the can be tested Allows running workflows for external contributors label Jul 31, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ec1e54b]

Summary: ✅


AI Review

Summary

This PR adds caching for text-index tokens that are absent from a part and enables that path by default. The performance motivation and changelog metadata are fine, but the current implementation still breaks the existing cache-disable and cache-size contracts for the token cache, so it is not ready to merge as-is.

Findings

❌ Blockers

  • [src/Storages/MergeTree/TextIndexCache.h:84] Negative entries are stored as a default TokenPostingsInfo, so TextIndexTokensWeightFunction charges each one as 0 bytes. CacheBase therefore keeps these cells even when text_index_tokens_cache_size = 0, and the per-query fallback cache created when use_text_index_tokens_cache = 0 uses max_count = 0, so miss-heavy queries can accumulate an unbounded number of (part, token) negative cells outside the configured cache-size limit. This violates the existing “cache disabled / cache bounded by size” contract. A fix needs to give negative entries a real weight or skip setNotFound when the backing token cache is effectively disabled.
Final Verdict

❌ Changes requested.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.70% 78.70% +0.00%

Changed lines: Changed C/C++ lines covered: 57/57 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Jul 31, 2026
@CurtizJ CurtizJ self-assigned this Jul 31, 2026

@CurtizJ CurtizJ left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

Comment thread src/Core/Settings.cpp Resolved
Enable the cache by default and isolate the profile-event test from parallel replicas.

Related: ClickHouse#112742
private:
static const MappedPtr & notFoundEntry()
{
static const auto entry = std::make_shared<TokenPostingsInfo>();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This sentinel makes every negative-cache cell weigh 0 bytes because TextIndexTokensWeightFunction delegates to TokenPostingsInfo::bytesAllocated(). CacheBase/SLRUCachePolicy only evict when total weight or max_count exceeds the limit, so these entries survive even when text_index_tokens_cache_size = 0 is supposed to disable the cache, and they also bypass the per-query local_cache_max_size fallback created in MergeTreeIndexConditionText.cpp when use_text_index_tokens_cache = 0 (max_count = 0 there).

A miss-heavy query can therefore keep adding (part, token) negative cells without honoring the configured cache-size bound. Could we give negative entries a non-zero weight or skip storing them when the backing token cache is effectively disabled?

@clickhouse-gh

clickhouse-gh Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.70% 78.80% +0.10%

Changed lines: Changed C/C++ lines covered: 57/57 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh

clickhouse-gh Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

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

This PR only adds a negative-tokens cache for full-text (text) indexes, gated behind a new default-on setting, and touches only text-index code paths (MergeTreeIndexText, TextIndexCache) plus profile events and docs. None of the flagged ClickBench queries use text indexes or hasAnyTokens, so their deltas cannot be attributed to this change. Q9 and Q17 both show an identical +12.4% offset, which points to a systematic artifact consistent with the small source sample count and co-located runs rather than a real per-query effect. All three flags are downgraded to not_sure.

clickbench

⚠️ 3 inconclusive

Flagged queries (3 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 9 not_sure 347 390 +12.4% <0.0001 ClickBench Q9 is a GROUP BY aggregation and uses no text index. This PR only touches full-text index token caching, so the +12.4% is off-path run-to-run variance.
⚠️ 17 not_sure 549 617 +12.4% <0.0001 Q17 exercises no text-index path, which is all this PR changes. Despite the trending master signal, its +12.4% mirrors Q9 exactly and reads as systematic variance.
⚠️ 18 not_sure 1250 1328 +6.2% 0.0230 Q18 uses no text index, so the PR's negative-tokens cache cannot affect it. The two tests disagree and the +6.2% is off-path variance.

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: 60dba66e-ae87-43d8-8e77-394477dd4806
  • MIRAI run: c75292e0-47b2-4456-b67b-d16a2a20b86f
  • PR check IDs:
    • clickbench_1420287_1785711448
    • clickbench_1420294_1785711448
    • clickbench_1420305_1785711448
    • tpch_adapted_1_official_1420316_1785711448
    • tpch_adapted_1_official_1420332_1785711448
    • tpch_adapted_1_official_1420357_1785711448

@CurtizJ
CurtizJ added this pull request to the merge queue Aug 3, 2026
Merged via the queue into ClickHouse:master with commit 09c5f70 Aug 3, 2026
180 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors 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