Better query condition cache coverage for ORDER BY ... LIMIT k queries#110507
Conversation
|
Workflow [PR], commit [8137b02] Summary: ✅
AI ReviewSummaryThis PR reattaches query-condition-cache keys after lazy materialization rewrites the surviving storage Final Verdict✅ No blocking findings. |
|
Waiting for perf run with ClickBench. |
|
📊 Cloud Performance Report 🟢 AI verdict: This PR fixes query-condition-cache population and reuse for TopK dynamic filtering plus lazy materialization (WHERE + ORDER BY LIMIT reads). Only clickbench Q23 shows a work-backed win: CPU fell from ~1545 to ~504 ms alongside a ×2.2 faster wall time, consistent with the fixed granule-skip path. Q4, Q15 and Q31 improved in wall time only, with flat or rising CPU and no selective WHERE/TopK to exercise the change, so they were kept as no change or downgraded to not sure as co-location/cache variance. The envelope suppression on Q4 looks correct. clickbench🟢 1 improved · Flagged queries (3 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🟢 No significant changes Debug info
|
Q23 was the motivating factor for this PR. |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 88/94 (93.62%) · Uncovered code |
|
Rerunning the failed jobs. |
|
Green CI right now at commit 8137b02 |
bd20703
Replace the hard-coded disable from the previous commit with a proper setting, `use_query_condition_cache_for_top_k` (default `false`), that enables/disables the query condition cache (QCC) for queries going through the TopK dynamic-filtering plan (`ORDER BY <column> LIMIT n`). The feature (added in PR #104478 and extended in PR #110507) is thus disabled by default without removing it or its tests. When the setting is off, each QCC touch point for TopK reads restores the pre-#104478 behavior; when it is on, the current master behavior (salted QCC entries) is used. The gated points are: - `updateQueryConditionCache`: skip the QCC write for the WHERE filter of a TopK read. - `ReadFromMergeTree::setTopKColumn`: turn off `use_query_condition_cache` in the reader settings for the read. - `ReadFromMergeTree::selectRangesToRead`: do not record index-analysis QCC exclusions for TopK reads. - `MergeTreeDataSelectExecutor::filterPartsByQueryConditionCache`: consult the WHERE condition with the plain (unsalted) hash and skip the predicate-only reuse path. The stateless and performance tests for the feature are kept; each now sets `use_query_condition_cache_for_top_k = 1` so it still exercises the feature. The new setting is registered in `SettingsChangesHistory` for 26.7. Related: #104478 Related: #110507 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…condition-cache behavior The 26.7 stable release (v26.7.1.1315-stable) shipped #104478/#110507 with TopK (`ORDER BY ... LIMIT n`) reads participating in the query condition cache unconditionally, so the `SettingsChangesHistory` entry for `use_query_condition_cache_for_top_k` must record `previous_value = true`: `SET compatibility = '26.7'` on 26.8+ then re-enables the gate instead of silently disabling the behavior for pinned compatibility profiles. Adds `04631_query_condition_cache_topk_compatibility` asserting the default is off and that `compatibility = '26.7'` flips it on while `compatibility = '26.8'` keeps it off. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This PR makes two improvements to how the query condition cache (QCC) interacts with the TopK dynamic filtering optimization for
ORDER BY ... LIMIT kqueries.Enable query condition cache for
ORDER BY ... LIMIT nqueries #104478 enabled the QCC for queries that go through TopK dynamic filtering, but it did not handle queries that are also rewritten by lazy materialization — the QCC entry was never populated in that case. Lazy materialization replaces theFilterStepthat carried the QCC key, so this PR re-runs theupdateQueryConditionCachepass after lazy materialization to re-attach the key to the survivingFilterStep.A query selected for TopK dynamic filtering can now reuse QCC entries written by a non-TopK query that had the same predicate. The TopK query's
WHEREcondition isand(__topKFilter(...), <predicate>); it now also probes the cache under the bare<predicate>hash, so entries written by a plainSELECT ... WHERE <predicate>are reused. The TopK query still writes its own entries under a key salted with the TopK plan parameters, so a plain query (or a TopK query with a differentLIMIT/ sort key) never reads a TopK-salted entry.Related: #104478
Changelog category
Changelog entry
ORDER BY ... LIMIT k) now make fuller use of the query condition cache: the cache is populated even when lazy materialization is applied to the query, and such queries can reuse entries previously written by an ordinary query that had the sameWHEREpredicate....
Version info
26.7.1.1131(included in26.7and later)