Repository navigation
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) <[email protected]>
…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 <[email protected]>
This disables (without reverting) the feature that enabled the query condition cache (QCC) for queries going through the TopK dynamic-filtering plan (`ORDER BY <column> LIMIT n`), added in PR ClickHouse#104478 and extended in PR ClickHouse#110507. A TopK read can drop granules during execution depending on the running `__topKFilter` threshold, which is derived from the rows of all parts the query reads. QCC entries are keyed by `(table_uuid, part_name, condition_hash)` and are threshold-oblivious, so a granule-skip decision recorded under one threshold (or one set of parts) can be reused when it no longer holds. The feature added salting of the cache key with the TopK plan parameters and a part-set snapshot to make this sound, but out of caution we turn the feature off in the stable release until its soundness is fully established. Rather than reverting, this restores the pre-ClickHouse#104478 behavior at each QCC touch point while keeping all of the added infrastructure (the salting helpers, `TopKFilterInfo::condition_hash`, the part-set snapshot hashing, the predicate-only reuse path) in place but inert: - `updateQueryConditionCache`: skip the QCC write for the WHERE filter of a TopK-filtered read (early return, as before ClickHouse#104478). - `ReadFromMergeTree::setTopKColumn`: turn off `use_query_condition_cache` in the reader settings, so execution-time QCC writes/consults are disabled for TopK reads. - `ReadFromMergeTree::selectRangesToRead`: do not record index-analysis QCC exclusions for TopK reads (their excluded ranges depend on the threshold). - `MergeTreeDataSelectExecutor::filterPartsByQueryConditionCache`: consult the WHERE condition with the plain (unsalted) hash and do not take the predicate-only reuse path. The stateless and performance tests that assert QCC caching for TopK reads are removed; a revert of this change cleanly re-enables the feature together with its tests. The change is behavior-only for TopK reads: for non-TopK reads none of these code paths differ from current master. Related: ClickHouse#104478 Related: ClickHouse#110507 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…y default `use_query_condition_cache_for_top_k` was introduced by ClickHouse#111492 and defaulted to `false` as a precaution while the soundness of the cache entries written by a TopK read was being established. Those entries are partitioned by the TopK plan parameters and by a snapshot of the set of parts read, so only a query with the same plan over the same parts reuses them; the gate is no longer needed and the default becomes `true`. The setting and all of the gating it drives are kept, so the cache can still be kept out of TopK reads with `use_query_condition_cache_for_top_k = 0`, and the tests covering that configuration are kept as well (they already pinned the setting explicitly). The tests of the feature itself no longer have to enable it. The settings-history entry keeps `previous_value = false`, because the gate was backported to 26.7: `compatibility` with 26.7 or earlier still turns the cache off for TopK reads, while `compatibility = '26.8'` keeps it on. Reverts: ClickHouse#111492 Related: ClickHouse#104478 Related: ClickHouse#110507 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…lickbench Better query condition cache coverage for ORDER BY ... LIMIT k queries
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)