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

TypeStoresGood for
minmaxmin and max per blockrange filters on loosely ordered columns
set(n)up to n distinct values per blocklow-cardinality equality or IN
bloom_filter(p)Bloom filter with false-positive rate pequality/IN on high-cardinality columns
ngrambf_v1, tokenbf_v1n-gram or token filterssubstring or token search on text
text (newer versions)full-text inverted indextoken search
  • GRANULARITY N groups 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 INDEX then MATERIALIZE INDEX for old parts. SET use_skip_indexes = 0|1 toggles 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';
SettingRows readBytes read
use_skip_indexes = 05,000,00090.6 MiB
use_skip_indexes = 124,576455.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 set or minmax index 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_indices for sizes and EXPLAIN for 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_comment and system.query_log.read_rows before and after.

Practice questions

  1. Why can a minmax index on a random column be useless?
  2. How does GRANULARITY change index size and skipping?
  3. When would you choose a projection instead of a skipping index?

References

  • ClickHouse documentation, Understanding ClickHouse data skipping indexes

ch-sorting-key-sparse-index · ch-projections · ch-query-optimization · indexing-theory