Skip to content

Track used skip indices - #128223

Merged
yariks5s merged 77 commits into
masterfrom
yarik/query-log-used-skip-indices
Nov 18, 2026
Merged

yariks5s merged 77 commits into
masterfrom
yarik/query-log-used-skip-indices

Conversation

@yariks5s

@yariks5s yariks5s commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

Related: #26778

This change adds:

  • system.query_log.used_data_skipping_indicesб data skipping indexes that the index analysis applied, as database.table.index, next to the existing projections column
  • ProfileEvents GranulesDroppedByPartitionKey, GranulesDroppedByPrimaryKey, GranulesDroppedBySkipIndexes- how many marks each index layer excluded during index analysis of MergeTree tables. Indexes applied at read time (use_skip_indexes_on_data_read) are not counted, the same as in EXPLAIN indexes = 1

The walk over the per-query index layers is shared with system.predicate_statistics_log, and the synthetic <Combined skip indexes> entry gets a named constant.

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Add the column used_data_skipping_indices to system.query_log and the ProfileEvents GranulesDroppedByPartitionKey, GranulesDroppedByPrimaryKey and GranulesDroppedBySkipIndexes, which show which data skipping indexes a query applied and how many marks each index layer excluded.


Workflow [PR]
Sync PR [sync-upstream/pr/128223]

Version info

  • Merged into: 26.11.1.2708-master (included in 26.11 and later)

@clickhouse-gh

clickhouse-gh Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [620afc8]

Summary: ✅


AI Review

Summary

Adds system.query_log.used_data_skipping_indices and per-layer pruning counters for MergeTree index analysis. No actionable findings at 620afc8fc1069f8c5e042f083822c44843f8c385. All 13 applicable focused SQL tests matched their references using the PR's Fast test binary.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-improvement Pull request with some product improvements comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Oct 30, 2026
…rt stats, distributed analysis and unusable indexes
The counters were differences between `index_stats` entries, so they credited query condition cache exclusions to the primary key, lost skip-index totals with `per_part_index_stats`, counted the combined distributed analysis entry as primary key, and listed indexes that no part could use. `filterPartsByPrimaryKeyAndSkipIndexes` now measures the drops per layer and marks an index as used when it passes `canUseIndex` and its file exists in a part. A replica of distributed index analysis records its share in its own `query_log` entry.

Proof: 05460 failed before (warm cache 0/7/0, per-part skip 0, distributed []/7/0, unusable idx_c and idx_b listed), passes now; 05459 still passes.
A vector similarity index passed canUseIndex and its file existed, so it was listed in used_data_skipping_indices, but filterMarksUsingIndex skips it when the primary key excluded some granules of the part. filterMarksUsingIndex now reports whether it evaluated the index. Test 05462 failed before (idx_vec listed with USearchSearchCount 0) and passes now.
…hole read

optimizeTrivialCountFromTextIndex drops ReadFromMergeTree when the text index is materialized in all parts, so its index usage and pruning events were lost. The replacement step now reports the analysis of the replaced read once, from recordIndexUsage, which also counts the partition-key drops now. With a partially materialized index ReadFromMergeTree stays and reports as before. Test 05461 failed before ([] and 0 with the optimization) and passes now.
…ead time

With a WHERE clause the analysis-time top-K step does not run, but with `use_skip_indexes_on_data_read` the dynamic top-K filter still reads the `minmax` index in `MergeTreeIndexReadResultPool`. The index was not in `used_data_skipping_indices` when the WHERE clause did not use it. Index analysis now lists it for a part that keeps ranges and has the index file, the same rule as for other read-time indexes. Drop counters are not changed. Test 05464 gave [] before and gives ['idx_a'] now.
When a normal or aggregate projection, or the implicit `_minmax_count_projection`, serves all parts, the parent `ReadFromMergeTree` is detached without `initializePipeline`, so its used skip indexes and the parts it excluded were lost. The step that replaces it now reports that analysis when its pipeline is built (`ISourceStep::setOnInitializePipeline`, set by `ReadFromMergeTree::reportIndexUsageWith`); `ReadFromTextIndexCount` uses the same mechanism instead of its own copy. Index analysis keeps the primary-key and skip-index drops of each part in `RangesInDataPart`, and `filterPartsByProjection` removes them for parts that the chosen projection reads, because the projection read reports its own analysis of them. `getMinMaxCountProjectionBlock` returns the marks of the parts that the partition key excluded. Test 05463 gave [] 0 3 0 (normal and aggregate projection) and partition drops 0 (minmax count) before; now ['idx_a'] 0 3 4 and 4.
…ta read are off

The CI test profile sets `max_rows_to_read`, which turns off skip indexes on data read, so the dynamic top-K filter did not read `idx_a` in Fast test, but the index was still listed. The test now resets `max_rows_to_read` and adds a case with a throwing limit, which gives ['idx_a'] instead of [] on the current code.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=128223&sha=05c0f392edb5e57bb6d78a55a29acda9397a6906&name_0=PR&name_1=Fast%20test
PR: #128223
… read are in effect

`skip_indexes.threshold_tracker` is set from the `use_skip_indexes_on_data_read` setting, but `MergeTreeSkipIndexReader` is created only when `supportsSkipIndexesOnDataRead` is true; a throwing `max_rows_to_read` or `max_partitions_to_read`, FINAL with exact mode or pending mutations turn it off. The dynamic top-K filter then does not read the index, but `used_data_skipping_indices` still listed it. Index analysis now also requires the effective `use_skip_indexes_on_data_read_`, the same flag that decides read-time filtering for the other skip indexes. Test 05464 gives [] for the case with `max_rows_to_read = 100000` now (before: ['idx_a']).

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=128223&sha=05c0f392edb5e57bb6d78a55a29acda9397a6906&name_0=PR&name_1=Fast%20test
PR: #128223
yariks5s and others added 5 commits October 30, 2026 19:56
…d distributed analysis under projections

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`assert_cast<ISourceStep &>` checks the exact type in debug and sanitizer builds, so every projection and text index count replacement threw `Bad cast from type DB::ReadFromTextIndexCount to DB::ISourceStep`.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=128223&sha=434d9a14d1d3f9096e95862b8e9d6bb09df73704&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20parallel%29
PR: #128223
Index analysis listed the indexes that it leaves to the read (`use_skip_indexes_on_data_read`), so a projection that replaces the read reported an index that is never read. The join runtime filter indexes were not listed at all. `ReadFromMergeTree::initializePipeline` now lists the indexes of `MergeTreeSkipIndexReader`, the top-K index of the dynamic filter and `runtime_skip_indexes` if a part that is read has the index. `ReadFromTextIndexCount` lists the text index that it reads. Tests 05463 (cases 8, 9) and 05465 give the expected lists now.

PR: #128223
… analysis

The reconstruction of `parts_with_ranges` copied the parts before analysis, so `filterPartsByProjection` subtracted nothing and the parent drops in parts that a projection reads were counted twice. Test 05463 case 11 gives `0 3 4` now (before: `0 3 6`).

PR: #128223
…ections

The AI review of a57a8d3 found two reporting errors. 05466: when the first read-time index excludes every granule, `MergeTreeSkipIndexReader::read` does not evaluate the next ones, but `used_data_skipping_indices` lists them (static and join runtime filter indexes). 05467: a chosen normal or aggregate projection that excludes every granule loses its analysis (`GranulesDroppedByPrimaryKey` 0 instead of 8), and the drops of the table key in the parts it excludes are counted with it. Both tests fail on the a57a8d3 binary: 05466 cases 1 and 4 list `idx_b`, 05467 gives `0 0 0`, `4 3 0` and `0 0 0` instead of `0 8 0`, `4 4 0` and `0 4 0`.

PR: #128223
`initializePipeline` listed every index of `MergeTreeSkipIndexReader` whose file exists in a part that is read. `MergeTreeSkipIndexReader::read` stops when an earlier index excludes every granule, so the later indexes were listed but never evaluated. The reader now reports each index once through `setOnIndexApplied` when `filterMarksUsingIndex` evaluates it (static and join runtime filter indexes), or when the top-K index is loaded for granules that are read. Test 05466 passes (before: cases 1 and 4 listed `idx_b`); 05459-05465 still pass.

PR: #128223
When the chosen normal or aggregate projection selects no ranges, `readFromParts` returns nothing and a `ReadFromPreparedSource` replaces it, so no projection read reported its analysis. The replacing step now reports it too: `ISourceStep` keeps a list of callbacks (`addOnInitializePipeline`), so the step can also report the parent read that it replaces. Also, the parent drops were removed only for the parts that the projection reads, not for the parts that it excludes entirely, so those parts were counted twice. `analyzeProjectionCandidate` now records the parent drops of every part that the projection analyzes, and `filterPartsByChosenProjection` removes them for the chosen projection. Test 05467 passes (cases 1, 3-6 before: `0 0 0`, `4 3 0`, `0 0 0`, `4 3 0`, `0 0 0`); 05459-05466 still pass.

PR: #128223
@clickhouse-gh clickhouse-gh Bot added comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. comp-skip-index Skipping indexes (minmax, set, bloom) and their evaluation. and removed comp-skip-index Skipping indexes (minmax, set, bloom) and their evaluation. comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Nov 13, 2026
…d-skip-indices

# Conflicts:
#	src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp
@clickhouse-gh clickhouse-gh Bot added comp-skip-index Skipping indexes (minmax, set, bloom) and their evaluation. and removed comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Nov 16, 2026
A concurrent test can clear the whole query condition cache between the
first two queries, then the second query reads all granules through the
index and lists it. Seen with a refreshable materialized view refresh,
which calls `Context::clearCaches` (issue #131985); tables created with an
explicit UUID and tables without a UUID do the same.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=128223&sha=b5bb04051009f970c06ea996461d1f574bc8442d&name_0=PR&name_1=Stateless%20tests%20%28amd_asan_ubsan%2C%20db%20disk%2C%20distributed%20plan%2C%20targeted%29
#128223
#131985
@yariks5s
yariks5s added this pull request to the merge queue Nov 18, 2026
Merged via the queue into master with commit 0bfadae Nov 18, 2026
193 checks passed
@yariks5s
yariks5s deleted the yarik/query-log-used-skip-indices branch November 18, 2026 12:06
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-skip-index Skipping indexes (minmax, set, bloom) and their evaluation. pr-improvement Pull request with some product 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.

4 participants