Repository navigation
Conversation
|
@zoomxi Please explain in the PR message what this optimization is doing (yes, I can and will read the code, but still). Please include a SQL example. Also, there should ideally be a xml performance test that demonstrates that performance improves. |
Sorry for the late reply, and thanks for the review! I'll make time to work on your suggestions soon. |
c4e9db4 to
2a72ec7
Compare
|
Added performance test
|
|
@zoomxi I'm looking at above measurements, the queries with the optimization turned off run so fast already that I think they are already answered from metadata. My theory is that such queries are already answered from the per-part minmax statistics (which are different than the min-max skip index and the |
@rschu1ze I double-checked, and the baseline queries are not answered from metadata. |
|
Will check later, no time this week |
|
Workflow [PR], commit [0e67f61] Summary: ✅
AI ReviewSummaryThis PR adds a Findings
Final VerdictChanges required: preserve the existing named/preferred projection-selection contract when LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 364/407 (89.43%) · Uncovered code |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 338/379 (89.18%) · Uncovered code |
Review feedback on ClickHouse#105197: - `getColumnStatisticsAggregationBlock` now loads statistics for the aggregate argument columns only, via `loadStatistics(needed_columns)`, instead of materializing and caching the full per-part statistics map through `getEstimates` (which deserializes every column's statistics and pins them in the part arena). - Explicitly require `StatisticsType::Basic` as the source of min/max bounds, since `minmax` statistics are deprecated and superseded by `basic` (ClickHouse#106048).
|
The per-part min-max statistics exist unconditionally for all parts. |
The shortcut fully replaces `ReadFromMergeTree` with `ReadFromPreparedSource` and never runs `selectRangesToRead` / `AnalysisResult::checkLimits`, which silently skipped the exceptions enforced there: `force_primary_key`, `force_index_by_date`, `force_data_skipping_indices`, and `max_partitions_to_read`. Decline the shortcut when any of these settings apply (mirroring the guards of the text-index count shortcut), so the normal read path keeps enforcing the same contract. `max_concurrent_queries` is treated the same way since its `QueryIdHolder` cannot outlive the optimization.
|
|
||
| if (!candidates.minmax_projection) | ||
| /// Try to use column statistics for min/max aggregation. | ||
| if (!candidates.minmax_projection |
There was a problem hiding this comment.
This shortcut runs before explicit aggregate projections are analyzed, and once it fills column_statistics_projection the branch at if (!candidates.minmax_projection && !candidates.column_statistics_projection) is skipped entirely. That changes the projection-selection contract for min/max queries on tables that also have basic statistics: preferred_optimize_projection_name is ignored, and force_optimize_projection_name = 'p' now raises INCORRECT_DATA from optimizeTree.cpp:765-770 even when aggregate projection p is applicable.
Please either decline _column_statistics_aggregation when a named/preferred projection is requested, or analyze the real aggregate projections first and let them win when one matches.
@rschu1ze True that the per-part min-max metadata exists unconditionally — but it currently includes only partition key columns, so for min()/max() on non-key columns, basic statistics is the only part-level min/max metadata that can cover them. Given that #115738 pursues the same goal with exactly this layering, I'm closing this PR in favor of it. |

Summary
This PR adds an optimization that answers filterless
min()andmax()aggregation queries from per-part column statistics without reading column data, avoiding full scans.How it works
When a query like
SELECT min(x), max(x) FROM truns on aMergeTreetable with column statistics configured (viaauto_statistics_types = 'basic'), the optimizer checks whether the aggregate results can be obtained from the per-partStatisticsBasicmetadata. If all conditions are met, the query plan replaces theReadFromMergeTreestep with aReadFromPreparedSourcethat produces the answer directly from statistics without reading column data.The optimization applies when:
min()ormax()auto_statistics_types = 'basic'and statistics have been materialized for the relevant columnsIf any check fails the query silently falls back to a normal
ReadFromMergeTreeread.SQL Examples
The optimization can be disabled via
SET use_statistics_for_min_max_aggregation = 0.Related PRs
Related: #93037
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Optimize
min()andmax()aggregate functions by utilizing per-part column statistics (basic) metadata when available, avoiding full data scans for queries. Controlled by the new settinguse_statistics_for_min_max_aggregation.Workflow [PR]
Sync PR [sync-upstream/pr/105197]