Query result caching for sub-queries#76252
Query result caching for sub-queries#76252nbarannik wants to merge 37 commits intoClickHouse:masterfrom
Conversation
…o query_cache_for_subqueries
…o query_cache_for_subqueries
…ckHouse into query_cache_for_subqueries
|
Dear @rschu1ze, this PR hasn't been updated for a while. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
|
@rschu1ze Hi, are you still working on this PR? I really need this PR as it can improve query performance. |
|
@fastio Not at the moment but it is still on my radar. |
|
I also need this PR, it is an amazing feature! |
|
Any progress on this one? It looks very promising! |
|
+1, very eager to see it merged! It is in the ClickHouse Roadmap 2025. |
|
+1 a very useful feature. Looking forward to using this in production |
|
It appears that this does not offer flexibility for selectively enabling caching for one or another subquery. Let's do it this way:
|
|
Hey @rschu1ze can I continue this if you're busy? I'd like to complete this feature implementation. |
|
@kiran-4444, yes, this will be amazing! You can create a new pull request that contains all the commits from this one and adds your commits. Also, take the comment above about the logic of this feature. |
Implement CTO feedback on PR ClickHouse#76252: use_query_cache on the outer query no longer auto-propagates to subqueries. Instead: 1. No auto-propagation: use_query_cache on outer query does NOT propagate to subqueries by default 2. Explicit per-subquery opt-in: SETTINGS use_query_cache = true on a specific subquery enables caching for that subquery only 3. Bulk propagation: query_cache_for_subqueries = true enables propagation of use_query_cache into ALL subqueries (existing behavior) Changes: - Planner.cpp: Add shouldUseQueryCacheForSubquery() helper implementing the three-rule decision logic for cache read/write - ExecuteScalarSubqueriesVisitor.cpp: Add shouldCacheScalarSubquery() checking explicit subquery SETTINGS before falling back to propagation - PreparedSets.cpp: Gate IN-subquery cache finalization on query_cache_for_subqueries setting - New test: 03381_query_result_cache_subquery_settings_propagation covering all 4 scenarios (no propagation, explicit opt-in, bulk propagation, explicit opt-out override) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Implement CTO feedback on PR ClickHouse#76252: `use_query_cache` on the outer query no longer auto-propagates to subqueries. Instead: 1. No auto-propagation: `use_query_cache` on outer query does NOT propagate to subqueries by default 2. Explicit per-subquery opt-in: `SETTINGS use_query_cache = true` on a specific subquery enables caching for that subquery only 3. Bulk propagation: `query_cache_for_subqueries = true` enables propagation of `use_query_cache` into ALL subqueries Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
@alexey-milovidov I too would love this feature (example: metadata table in a logs UI to fetch a list of projects: can be cached as a subquery like where projects IN ...). I fired #99804 which is based on your comment in #76252 (comment), let me know what you think 🙏 |
Implement CTO feedback on PR ClickHouse#76252: `use_query_cache` on the outer query no longer auto-propagates to subqueries. Instead: 1. No auto-propagation: `use_query_cache` on outer query does NOT propagate to subqueries by default 2. Explicit per-subquery opt-in: `SETTINGS use_query_cache = true` on a specific subquery enables caching for that subquery only 3. Bulk propagation: `query_cache_for_subqueries = true` enables propagation of `use_query_cache` into ALL subqueries Key implementation details: - Gate subquery cache on `select_query_options.is_subquery` to prevent outer queries from going through the subquery cache path - Set context `canUseQueryResultCache` flag for explicit per-subquery opt-in so `checkCanWriteQueryResultCache` works - Always call `finalizeWriteInQueryResultCache` to support subqueries that explicitly opt in independently of the outer query Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Resolves #51656
Settings
use_query_cache = true, query_cache_for_subqueries = truenow enable caching of subquery results in the query cache.Example:
The field
is_subquerywas added to thesystem.query_cachetable. It is set to true for all records related to sub-queries (including the main query that was cached as a sub-query for future use)Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Add subqueries caching in query cache
Documentation entry for user-facing changes