ClickHouse skip index not skipping? The 8,192-row math behind bloom_filter, set and minmax

A value in just 1 of every 10,000 rows still lands in 56% of 8,192-row blocks. Why ClickHouse skip indexes read almost everything, and what fixes them.

You add a bloom_filter index, run MATERIALIZE INDEX, rerun the slow query, and EXPLAIN indexes = 1 says Granules: 11982/12208. The index works. It just has almost nothing to skip.

The ClickHouse docs give the reason in one sentence: “if a value occurs even once in an indexed block”, the whole block must be read (docs). This post puts a number on that sentence. A value that occurs in 1 of every 10,000 rows, at random positions, appears in 56% of the default 8,192-row blocks. At 1 in 1,000 rows it is 99.97%. A skip index on such a column reads the whole table, plus the index.

This is day 6 of the ClickHouse series. I read the index code at tag v26.10.2.103-stable and the official docs. The percentages below are arithmetic on those definitions, not benchmarks; the measured numbers are other people’s and are linked.

The short answer

Your query Works with a skip index when Doesn’t skip when
WHERE id = 'x' (bloom_filter) the value appears in fewer than about 1 row per 12,000, or its rows sit together the value is common (a status, a country), or you filter with !=
WHERE code IN (...) (set(N)) each block holds at most N distinct values the column has more than N distinct values per 8,192 rows: the set is dropped and every block is read
WHERE x > t (minmax) few rows pass the filter, or x follows the sorting key many rows pass, or you test equality on a random column
Anything the column is correlated with ORDER BY the column is random with respect to ORDER BY

The rest of the post shows where each row comes from.

What a skip index actually stores

The primary index of a MergeTree table has one entry per granule: 8,192 rows by default (index_granularity), fewer when 8,192 rows would exceed 10 MiB (index_granularity_bytes) (MergeTreeSettings.cpp). A skip index adds one small summary per index block of GRANULARITY granules. “The GRANULARITY clause can be omitted, the default value of granularity_value is 1” (MergeTree docs; DEFAULT_INDEX_GRANULARITY = 1 in ASTIndexDeclaration.h). So by default, one summary covers 8,192 rows.

At query time ClickHouse asks each summary one question: could this block contain a matching row? If the answer is no, it skips the block. If it is maybe, it reads all 8,192 rows. The summary depends on the type:

Type Summary per block Answers “no” when
minmax the smallest and largest value the filter range misses [min, max]
set(N) up to N distinct values; if a block has more, the set is stored empty the value is not in the set (an empty set always answers “maybe”)
bloom_filter(p) a Bloom filter of the block’s distinct values, default p = 0.025 the filter has no bit pattern for the value (false positives possible)

Two details from the source that the docs don’t spell out:

  • set(N) overflow is total. A block with more than N distinct values is serialised as an empty set, and the condition code returns “maybe” for it: if (size == 0 || (max_rows != 0 && size > max_rows)) return true; (MergeTreeIndexSet.cpp). One distinct value too many and the block is always read.
  • bloom_filter is sized by distinct values, not rows. The default rate of 0.025 is turned into 8 bits and 4 hash functions per value by a lookup table (calculationBestPractices in BloomFilterHash.h; the table’s entry for 8 bits and 4 hashes is 0.024). Each block’s filter gets bits_per_row × distinct values bits (MergeTreeIndexBloomFilter.cpp). That is 1 byte per distinct value per block.

The one formula

Take a value that occurs in a fraction p of the rows, at random positions with respect to the sorting key. A block of B rows contains none of them with probability (1 − p)B. So the share of blocks the index must read is:

read share = 1 − (1 − p)B, about 1 − e−pB

pB is the number of matching rows you expect in one block. The index starts to pay off only when pB is well below 1: the value must be rarer than one per block. Half the blocks are read when pB = ln 2, which means one match in 11,819 rows at the default B = 8,192.

Value occurs in GRANULARITY 1 (8,192 rows) GRANULARITY 4 (32,768) GRANULARITY 16 (131,072)
1 in 100 rows 100% 100% 100%
1 in 1,000 99.97% 100% 100%
1 in 3,333 91.4% 99.99% 100%
1 in 10,000 55.9% 96.2% 100%
1 in 33,333 21.8% 62.6% 98.0%
1 in 100,000 7.9% 27.9% 73.0%
1 in 1,000,000 0.8% 3.2% 12.3%

Blocks read, before any Bloom filter false positives. For a bloom_filter add about 2.4% of the blocks without a match: at 1 in a million rows, 0.8% becomes 3.2%.

Share of index blocks read when one value is scattered at random0%25%50%75%100%1 in 1k1 in 10k1 in 100k1 in 1M1 in 10Mhow often the searched value occurs (random positions)GRANULARITY 16: half the blocks read at 1 in 189,097 rowsGRANULARITY 4: half the blocks read at 1 in 47,274 rowsGRANULARITY 1 (default): half the blocks read at 1 in 11,819 rowsGRANULARITY 1 (default), 8,192 rows: half at 1 in 11.8kGRANULARITY 4, 32,768 rows: half at 1 in 47kGRANULARITY 16, 131,072 rows: half at 1 in 189k
Arithmetic, not a measurement: 1 − (1 − 1/N)rows per block. A skip index only skips when the value is rarer than about one per block. Dots mark the point where half the blocks are read. A bloom filter adds about 2.4% false positives on top.

Two things follow that surprise people.

High cardinality is not what breaks a Bloom filter. Common values are. Say a table holds 1 billion events from 1 million users, about 1,000 events each, in random order. One user is 1 row in a million: the index reads 0.8% of blocks (3.2% with false positives). The same index filtering status = 'error', where errors are 0.1% of rows, reads 99.97%. Same column type, same index, opposite result. What decides it is how often the searched value occurs, not how many distinct values the column has.

A larger GRANULARITY makes the index skip less, not more. It multiplies B. For a column where every value is distinct, it doesn’t even make a bloom_filter smaller: the filter costs 1 byte per distinct value, and a block of 4 granules has 4 times the values. Raise GRANULARITY when the index costs too much to read, and expect to read more data in exchange.

Where the matching rows sit matters more than how many there are

The formula assumes random positions. Real tables are sorted. When the matching rows sit next to each other, because the column is part of the sorting key or follows it (an order ID that grows with time in a table sorted by time), 6 matching rows hit 1 block instead of 6.

The same 6 matching rows, 12 index blocksscatteredcolumn not tied to ORDER BY: 6 blocks read, 6 skippedclusteredcolumn follows the sorting key: 1 block read, 11 skippedblock must be read (holds a match or a false positive)block skipped
A skip index can only rule out whole blocks. How many blocks it rules out depends on where the matching rows sit, which the table's ORDER BY decides. Not to scale.

The two published examples in the ClickHouse docs both work because of that:

Example (ClickHouse docs) Index Granules read Rows processed Time
100M-row table, my_value IN (125, 700) set(100) GRANULARITY 2 4 of 12,208 (“dropped 6102/6104” index blocks) 100.00 million → 32.77 thousand 0.079 → 0.051 s
Stack Overflow posts, ViewCount > 10000000 minmax GRANULARITY 1 23 of 8,513 59.55 million → 39.11 thousand 0.720 → 0.012 s

In the first, the docs built the data so the indexed value is correlated with the sorting key (it changes every 4,096 rows). In the second, the filter is extreme: posts with more than 10 million views are so rare that very few blocks contain one. That is pB well below 1 again, through rarity this time.

How each type fails

minmax on a random column

For a random column, each block’s [min, max] covers almost the whole value range. The expected span of 8,192 uniform random values is (B − 1)/(B + 1) = 99.98% of the range. So:

  • WHERE x = 42 on a random column skips almost nothing: 42 is inside nearly every block’s range, whether it appears there or not.
  • WHERE x > t reads exactly the blocks that hold at least one row above t, the formula above with p = share of rows above t. The top 0.1% of values (p = 0.001) still appears in 99.97% of blocks. The top 0.001% (1 in 100,000) appears in 7.9%. That is the Stack Overflow case.

set(N) that overflows

For a column with D distinct values spread uniformly at random, a block of B rows holds on average D × (1 − (1 − 1/D)B) distinct values:

Distinct values in the column Expected distinct per 8,192-row block set(100) set(1000)
100 100 fits fits
1,000 1,000 overflows: always read fits (and contains nearly every value, so skips little)
10,000 5,592 overflows overflows
100,000 7,865 overflows overflows

set(N) has a narrow sweet spot: few distinct values per block, many in the table. That is exactly the “high-cardinality columns that have low cardinality within each block” the best-practices page recommends, which in practice means a column correlated with the sorting key. set(0) (unlimited) never overflows, but then stores every distinct value of every block.

bloom_filter that is all cost

A Bloom filter has three limits on top of the formula:

  1. A floor of ~2.4%. Even a value that doesn’t exist reads about 2.4% of blocks with the default rate. A lower false_positive_rate buys a smaller floor with more bits per value (the lookup table goes up to 19 bits and 13 hash functions).
  2. No negative filters. bloom_filter cannot serve != / notEquals or LIKE: Bloom filters have false positives, so they “can not be used for optimizing queries where the result of a function is expected to be false” (MergeTree docs).
  3. Size close to the data. For a column of mostly distinct 8-byte IDs, 1 byte per distinct value is 1 byte per row: an eighth of the raw column. With 8 bits and 4 hashes per value, about 39% of a full filter’s bits are set (1 − e−4/8), which leaves little pattern for LZ4 or ZSTD to find. Expect the index to stay near that size on disk. (That is the textbook property of a well-filled Bloom filter, not something I measured.)

Reading the index costs time too

Until ClickHouse 25.9, every skip index entry was evaluated before any data was read, which could cost more than the read itself. 25.9 added evaluation per granule during the read (use_skip_indexes_on_data_read), and it has been on by default since 26.1 (Settings.cpp). The ClickHouse blog’s example (Tom Schreiber, 2026-01-09): a LIMIT 1 query over 1 billion rows with a 2.21 GiB Bloom filter took 10.173 s with up-front analysis and 2.471 s streaming, max_threads = 1. For a query without LIMIT, an index that answers “maybe” for most blocks is still read in full, and its cost is pure overhead.

How to check yours

-- Granules read vs total, per index
EXPLAIN indexes = 1
SELECT count() FROM events WHERE user_id = 'u-123';
--   Skip
--     Name: user_bf
--     Description: bloom_filter GRANULARITY 1
--     Parts: 12/40
--     Granules: 310/9876        <- 3%: the index works

-- What each index costs on disk
SELECT name, type_full, granularity,
       formatReadableSize(data_compressed_bytes)   AS on_disk,
       formatReadableSize(data_uncompressed_bytes) AS raw
FROM system.data_skipping_indices
WHERE table = 'events';

-- Share of rows with the value: compare with 1 / (8192 * GRANULARITY)
SELECT countIf(status = 'error') / count() FROM events;

The last query is the cheap test. If the share is above about 1 / 12,000 (0.008%) and the column isn’t correlated with the sorting key, the index cannot skip half the blocks, whatever its type.

What to do instead

  • Put the column in the sorting key, or choose a key that clusters it. That fixes skip indexes and often makes them unnecessary: the primary index works with the same granules. Day 1 shows what the key does to rows read.
  • Lower GRANULARITY for a rare-value Bloom filter. GRANULARITY 1 is already the default; check that your DDL doesn’t set 3 or 4 from an old tutorial.
  • Index only values that are rare. Bloom filters fit IDs (users, traces, orders); a minmax fits extreme thresholds. Common values (status, log level, country) need a different sort order, a projection, or a materialized view sorted by that column.
  • Drop the index if EXPLAIN keeps showing more than about 90% of granules read. It costs space and insert time (“a meaningful cost both on data ingest and on queries”, docs) and returns almost nothing.
  • Note the newer defaults (26.10): use_skip_indexes_if_final = 1, so skip indexes are used with FINAL; and add_minmax_index_for_numeric_columns exists but is off (MergeTreeSettings.cpp). Turning it on adds minmax indexes that follow the same arithmetic, so they help only on columns that follow the sorting key.

FAQ

Why does my ClickHouse bloom_filter index not skip any granules? Most likely the value you search for appears at least once in most 8,192-row blocks. With random placement that happens once it occurs more often than about 1 in 12,000 rows. Check with EXPLAIN indexes = 1.

Is a bloom_filter index good for high-cardinality columns? Yes for point lookups of individual IDs: a value in 1 of a million rows reads under 1% of blocks (about 3% counting false positives). The cost is size, about 1 byte per distinct value per block.

What GRANULARITY should a skip index use? Start at the default of 1. Each step up multiplies the rows per block and the share of blocks that contain your value. Raise it only if reading the index itself is the bottleneck.

Why is my set index useless? Probably because blocks have more distinct values than N. Such a block is stored as an empty set and always read.

Can a skip index replace the ORDER BY key? No. The primary key decides where rows sit; a skip index can only exploit that layout. On a column random with respect to the key, it skips only values rarer than about one per block.

What I could not verify

  • All percentages assume uniformly random placement. Real data sits between random and clustered. Correlated columns do better, and bursty values (an error storm in one minute) do better too, because their rows cluster in time.
  • I did not measure index sizes or compression ratios. The “close to its raw size” point for a Bloom filter rests on the fill ratio, not on a test.
  • The docs’ set(100) example processed 32.77 thousand rows (4 granules) for “dropped 6102/6104” index blocks; the docs explain the 4 granules but not the exact data layout. I quote them as published.
  • Settings and defaults are from the 26.10.2.103 source. The two docs examples don’t name a version.

Sources

Next in the series: when a projection pays off.