At the end of Episode 7, I said I would look at data skipping indexes for log search. Most log searches look for a word or a phrase: an error code, a request ID, a class name, a complete or partial phrase. Before the text index, ClickHouse had no index that points at single rows, so the only way to speed up such a search was to skip blocks of rows that cannot match. This episode is about how well that worked for us with different variations of bloom filters. The text index, which we added later, gets its own episode.
What a data skipping index does
ClickHouse stores rows in sorted order and cuts each part into granules of 8,192 rows, the default index_granularity. The primary index keeps one entry per granule, so a filter on the sort key skips granules cheaply. Our default log tables are sorted by time and a few Kubernetes fields. A word inside a log message is not in the sort key, so the primary index cannot help with it.
A data skipping index stores a small summary of each granule, or of every few granules. ClickHouse has several types: minmax, set, text, vector_similarity, and three kinds of bloom filter. For log search we mainly used the bloom filters:
- bloom_filter: can this exact value be in the granule?
- tokenbf_v1: a bloom filter of the words in a text column. Can this word be in the granule?
- ngrambf_v1: a bloom filter of all substrings of length n. Can this substring be in the granule?
The answer is always "no" or "maybe". A "no" skips the granule. A "maybe" means ClickHouse reads the granule and checks every row. Checking is not free either: ClickHouse reads and checks the index entry of every granule the primary index left. EXPLAIN indexes=1 shows how many granules each index kept, and we ran it on almost every slow search in this post.
A search that never used the index
Our first slow searches did not use the index at all. Our query translator turned a free-text search into LIKE on the message column. A search for a class name such as ValkeyCartStore looked like this:
SELECT ... FROM logs
WHERE message LIKE '%ValkeyCartStore%'
AND timestamp BETWEEN ...
The table had a token bloom filter on message, but ClickHouse could not use it here. The bloom filter stores whole words, split at spaces and punctuation. The % on both sides lets the query match a longer word, such as ValkeyCartStoreV2. So a "no" for ValkeyCartStore would not prove the granule has no match. EXPLAIN showed 76,424 granules to read. With the filesystem cache off, the query took 274 seconds.
The same search with hasToken, which matches a whole word, kept 671 granules:
WHERE hasToken(message, 'ValkeyCartStore')
AND timestamp BETWEEN ...
So we changed the translator to send hasToken for each word. For a phrase, it also keeps a LIKE on the whole phrase, which runs only on the granules the filter leaves.
That changed what a search means, so we changed the search syntax too. A search now matches whole words by default. A search for part of a word, such as "Cart" inside "ValkeyCartStore", has to specify it: with a * at the start or end (*Cart*), or with the .partialWordMatch suffix on the field name. Those searches run LIKE and read every granule.
There was one more catch. Our users query views, not tables, and with the new query analyzer ClickHouse did not use data skipping indexes through a view, a known issue at the time. We turned the analyzer off for those queries for a while.
Common words do not skip anything
With hasToken, rare words got fast. Common words did not. One user searched over 30 days for a phrase of five common words, such as "could not charge the card". The translator sent:
WHERE hasToken(message, 'could')
AND hasToken(message, 'not')
AND hasToken(message, 'charge')
AND hasToken(message, 'the')
AND hasToken(message, 'card')
AND message LIKE '%could not charge the card%'
If any one word is missing from a granule, ClickHouse skips it. The filter still kept 12,543 of 92,725 granules. With a cold cache the query took 77.8 seconds and returned no rows.
Each of the five words was somewhere in most granules, so each check said "maybe". The phrase was in none of them, but only the LIKE could tell, by reading every row of those granules.
A word does not need to be very common for this to happen, because a granule is large. If a word appears in 1 of every 1,000 lines, the chance that 8,192 lines have none of it is 0.999 to the power of 8,192, about 0.03%. Even a word in 1 of every 10,000 lines is in more than half of the granules. A token bloom filter skips most granules only for rarer things: an ID, an unusual class name or error code.
Adding more common words does not help. The filter stores no positions, so words next to each other in one line look the same as words spread across a granule. What helps is the rarest part of a search: one rare word, a filter on a sort key column such as the service, or a shorter time range. The text index works differently, and the next episode covers how it did with common words.
Filters that were the wrong size
A bloom filter's size is fixed when it is created. Put too many distinct values in it and nearly every bit is set, so every check says "maybe". Put too few in it and it is far larger than needed, so every check reads more bytes than it should. We ran into both.
One table had ngrambf_v1(4, 1024) on a URL column: a 1,024-byte filter for all 4-character substrings in a granule. A granule of URLs holds on the order of 100,000 of them. Even if only a tenth are distinct, a filter that size has a false positive rate close to 100%. EXPLAIN agreed: it kept 42,429 of 42,478 granules. We tried a 16 KiB token bloom filter on the same column, about 1.5 times the size of the compressed URL data. It skipped nothing either, because the path segments people searched for were in every granule. We dropped it, and stopped expecting an index to help with that column.
The token bloom filter on message had the opposite problem: it was far bigger than it needed to be. Our template used tokenbf_v1(524288, 3, 0), which means a 512 KiB filter per granule with 3 hash functions. One analysis found 10,000 to 20,000 distinct words per granule. A 32 KiB filter can hold 20,000 words with a false positive rate under 1%, so ours was 16 times larger than necessary.
The extra size cut the false positive rate to about 0.0003%, but that barely helped. A bloom filter can skip a granule only when the searched word is not in it. Common search terms appear in most granules, and for those granules any filter, large or small, has to answer "maybe". In practice, the filter skipped only about 18% of granules for typical searches.
The extra size did raise the cost. For every granule, ClickHouse loaded and checked 512 KiB of filter data instead of 32 KiB. We later removed the filter, as the next section explains.
You do not have to do this math by hand. An online Bloom filter calculator takes the number of distinct values and the false positive rate you want, and returns the size in bytes and the number of hash functions. For tokenbf_v1 and ngrambf_v1, the number of values is the distinct words or n-grams in one index block (a granule times the index GRANULARITY). The plain bloom_filter index takes the false positive rate directly, as in bloom_filter(0.01), and sizes itself.
Two more patterns kept showing up in EXPLAIN:
- A bloom filter on a column already in the sort key does almost nothing. The container_name filter skipped 10 of 41,344 granules; the primary index had already done that work.
- A bloom filter on a column with few values, such as environment or log level, does almost nothing either, because every granule holds every value. One environment filter kept 3,827 of 3,888 granules.
When the indexes made searches slower
On one of our tables, searches filtered on columns outside the sort key, so the primary index skipped very little. We added bloom filters on the columns searched most. Later, some of those searches took minutes. With data skipping indexes on and off:
- A search for one rare word: 32.6 seconds with indexes, 5.4 seconds without.
- A phrase search: 26.7 seconds with indexes, reading about 1 million rows. 9.7 seconds without, reading about 856 million rows.
Reading almost a thousand times fewer rows took almost three times longer. The time went into checking indexes on hundreds of thousands of granules, and the indexes skipped too little to pay for that. We turned them off for that customer with use_skip_indexes = 0, and the slowest searches got faster. We did not yet know which index cost the most.
Later after text index became available, we ran one slow search on the same table:
- All data skipping indexes: 23.4 seconds.
- Only the 512 KiB token bloom filter on message: 29.8 seconds.
- Only the text index on message, which we had added by then: 10.0 seconds.
The token bloom filter cost more than it saved. We dropped it from that table and from our template, kept the text index for message search, and turned data skipping indexes back on for that customer.
What I check now
Before adding a data skipping index, I run EXPLAIN indexes = 1 on the searches people actually send, with and without the index, with a cold cache. A bloom filter helps when the value is rare and the column is not in the sort key, such as a trace ID or a customer ID. It does little for columns with few values, columns already in the sort key, or common words. It has to be sized for the distinct values in a granule, because a filter that is too large costs time on every granule. And the search has to be written so the index can be used: hasToken for whole words, not LIKE alone.
The recommendations have also changed since we started. Since the text index became generally available in ClickHouse 26.2, the ClickHouse documentation no longer recommends tokenbf_v1 or ngrambf_v1 for full-text search. If I set up these tables today, I would use bloom_filter for exact values such as IDs, and the text index for words.
What's next?
In the next episode, I will write about ClickHouse's text index: an inverted index for full-text search, how it compared with the token bloom filters on the same searches, and what it cost us in storage.



