Overview
A data skipping index stores a small summary per block of granules and lets ClickHouse skip blocks that cannot satisfy a predicate on a column that is not in the sorting key. They are not B-trees: they never point to rows, they only exclude granules, so they work when matching rows are clustered, or when the value is rare enough that most blocks contain none.
Core concepts
| Type | Stores | Good for |
|---|---|---|
minmax | min and max per block | range filters on loosely ordered columns |
set(n) | up to n distinct values per block | low-cardinality equality or IN |
bloom_filter(p) | Bloom filter with false-positive rate p | equality/IN on high-cardinality columns |
ngrambf_v1, tokenbf_v1 | n-gram or token filters | substring or token search on text |
text (newer versions) | full-text inverted index | token search |
GRANULARITY Ngroups N primary-index granules per index entry; larger values give a smaller index with coarser skipping.- Add to an existing table with
ALTER TABLE ... ADD INDEXthenMATERIALIZE INDEXfor old parts.SET use_skip_indexes = 0|1toggles use per query.
Worked example
5,000,000 rows ordered by (toDate(ts), user_id); session is a random 16-character hex string, not in the key, with INDEX idx_session session TYPE bloom_filter(0.01) GRANULARITY 4:
SELECT count() FROM events_s WHERE session = 'D3F7CA7EE1A4EC54';| Setting | Rows read | Bytes read |
|---|---|---|
use_skip_indexes = 0 | 5,000,000 | 90.6 MiB |
use_skip_indexes = 1 | 24,576 | 455.8 KiB |
EXPLAIN indexes=1 showed Skip Name: idx_session Granules: 4/611: only 4 of 611 granules survived. The bloom filter index took 5.79 MiB (the data is ~90 MiB), and a minmax index on amount only 409 bytes (but would skip nothing on random amounts).
Pitfalls and production notes
- An index only helps if blocks can be excluded. A
setorminmaxindex on a column whose values are spread uniformly over all blocks skips nothing and costs write time. - Bloom filters add storage and insert/merge cost; check
system.data_skipping_indicesfor sizes andEXPLAINfor effectiveness. - Prefer fixing the sorting key or adding a projection (ch-projections) when the filter is common.
- Skipping indexes are evaluated after the primary index; combine both (ch-sorting-key-sparse-index).
- Test with
log_commentandsystem.query_log.read_rowsbefore and after.
Practice questions
- Why can a
minmaxindex on a random column be useless? - How does
GRANULARITYchange index size and skipping? - When would you choose a projection instead of a skipping index?
References
- ClickHouse documentation, Understanding ClickHouse data skipping indexes
Related
ch-sorting-key-sparse-index · ch-projections · ch-query-optimization · indexing-theory