ClickHouse index_granularity 1024 vs 8192: smaller granules, same 64 KiB to decompress

Lower ClickHouse index_granularity to 1,024 or 256 and a lookup still decompresses 8,192 rows of every UInt64 column: the 64 KiB compression block floor.

The standard advice for point lookups in ClickHouse is to lower index_granularity. The key-value FAQ says it “might be useful to reduce the value of N”, and many guides then promise that at granularity 256 “a lookup reads at most 256 rows instead of 8192”.

For most columns that promise is wrong. Compression blocks are not granules. ClickHouse closes a compressed block only at a granule boundary, and only once the block holds at least 64 KiB of uncompressed data (min_compress_block_size). A block can’t be partly decompressed. So whatever the granularity, one lookup decompresses at least 64 KiB per column: 8,192 rows of a UInt64, 16,384 of a UInt32, 65,536 of a UInt8. Smaller granules shrink what ClickHouse filters and copies. For narrow columns they don’t shrink what it decompresses.

This is day 9 of the ClickHouse series. Everything below comes from the source at tag v26.10.2.103-stable, the ClickHouse docs and published measurements. The tables are arithmetic on those rules, not benchmarks.

The short answer

index_granularity Granules per 1B rows Rows decompressed per lookup, UInt64 column ~100-byte String column ~1 KiB payload column Primary index, UInt64 key
65,536 15,259 65,536 65,536 ~10,000 (capped) 0.12 MB
8,192 (default) 122,071 8,192 8,192 8,192 0.98 MB
1,024 976,563 8,192 1,024 1,024 7.8 MB
256 3,906,250 8,192 768 256 31 MB
128 7,812,500 8,192 768 128 62.5 MB

The decompressed rows assume the default min_compress_block_size of 65,536 bytes and a lookup that lands in one granule. The “capped” cell is adaptive granularity (index_granularity_bytes, 10 MiB) stepping in. The index column counts one 8-byte key per granule, uncompressed in memory.

The rule of thumb: a smaller granularity only cuts decompression for columns where one granule is already bigger than 64 KiB, meaning 8 bytes × 8,192 rows. Below that, you also have to lower min_compress_block_size, per column if you like (see the fix).

Granules and compressed blocks are different things

A granule is the unit of the sparse primary index: one index entry and one mark per column every index_granularity rows (day 1 of this series covered the index itself). A compressed block is the unit of LZ4/ZSTD: a 9-byte header (method, compressed size, uncompressed size; CompressionInfo.h) plus a 16-byte checksum, then the compressed bytes.

A mark ties the two together. It holds two offsets: where the compressed block starts in the .bin file, and where the granule starts inside the decompressed block. The .mrk2 format adds a third number, the granule’s row count (sparse primary indexes guide). The second offset only exists because one compressed block can hold several granules.

One UInt64 column, index_granularity = 1024: 8 granules share each compressed blockid.bin, one compressed block (64 KiB uncompressed)granule 0: 1,024 rows, 8 KiBg08 KiBgranule 1: 1,024 rows, 8 KiBg18 KiBgranule 2: 1,024 rows, 8 KiBg28 KiBgranule 3: 1,024 rows, 8 KiBg38 KiBgranule 4: 1,024 rows, 8 KiBg48 KiBgranule 5: 1,024 rows, 8 KiBg58 KiBgranule 6: 1,024 rows, 8 KiBg68 KiBgranule 7: 1,024 rows, 8 KiBg78 KiBmark 5 = (block offset in file, 40 KiB into the block)The lookup needs 1,024 rows (g5), but LZ4/ZSTD can only decompress the whole block:64 KiB = 8,192 rows decompressed, the same as at the default index_granularity = 8192.
Why smaller granules don't shrink decompression for narrow columns. Block rule from MergeTreeDataPartWriterWide.cpp (min_compress_block_size = 65,536 bytes) at v26.10.2.103.

Where the 64 KiB floor comes from

The writer for Wide parts decides where a block ends in one place. Before it writes the next granule of a column, MergeTreeDataPartWriterWide.cpp runs:

/// There could already be enough data to compress into the new block.
if (stream.compressed_hashing.offset() >= min_compress_block_size)
    stream.compressed_hashing.next();

offset() is the number of uncompressed bytes buffered so far. So a block is closed at a mark only once it holds at least min_compress_block_size bytes, 65,536 by default. The setting’s own description says the same thing in one sentence: the block size “is no less than this value and no less than the volume of data for one mark”. At the other end, the buffer is max_compress_block_size (1 MiB), so a granule bigger than 1 MiB is split across several blocks.

On the read side, CompressedReadBufferFromFile::seek jumps to the block’s offset in the file. It decompresses the whole block (size_decompressed bytes, CompressedReadBufferBase.cpp) and then skips to the granule’s offset inside it. LZ4 and ZSTD frames have no random access inside them, so there is no cheaper path.

Put the two together for a UInt64 column:

index_granularity Bytes per granule Granules per block Rows in the block a lookup decompresses
8,192 64 KiB 1 8,192
1,024 8 KiB 8 8,192
256 2 KiB 32 8,192
128 1 KiB 64 8,192

The default is a coincidence that’s easy to miss: 8,192 rows × 8 bytes is exactly 65,536 bytes, so at the default settings one block is one granule of a 64-bit column. Narrower columns already share blocks at the default. A UInt32, Date32 or Float32 granule is 32 KiB, so two granules go in each block. A UInt8 granule is 8 KiB, so eight go in each block.

Rows decompressed per column by a one-row lookupUInt648 B per valuegranularity 8,192UInt64, index_granularity 8192: 8,192 rows decompressed8,192granularity 1,024UInt64, index_granularity 1024: 8,192 rows decompressed8,192granularity 256UInt64, index_granularity 256: 8,192 rows decompressed8,192granularity 128UInt64, index_granularity 128: 8,192 rows decompressed8,192String~100 B per valuegranularity 8,192String, index_granularity 8192: 8,192 rows decompressed8,192granularity 1,024String, index_granularity 1024: 1,024 rows decompressed1,024granularity 256String, index_granularity 256: 768 rows decompressed768granularity 128String, index_granularity 128: 768 rows decompressed768payload~1 KiB per valuegranularity 8,192payload, index_granularity 8192: 8,192 rows decompressed8,192granularity 1,024payload, index_granularity 1024: 1,024 rows decompressed1,024granularity 256payload, index_granularity 256: 256 rows decompressed256granularity 128payload, index_granularity 128: 128 rows decompressed128= the granule (smaller granules help)more than the granule: the 64 KiB block floorlog scale · arithmetic: max(granule, whole granules filling 64 KiB), default min_compress_block_size
Arithmetic, not a measurement: a block closes at the first granule boundary after 64 KiB of uncompressed data, and the whole block is decompressed. Narrow columns hit the floor at every granularity below the default.

So what does a smaller granularity buy?

Not nothing. Two real costs scale with granule rows rather than block size:

  • Filtering. The WHERE id = x comparison runs on every row of the granules the index selects. That’s 8,192 rows at the default and 256 rows at 256.
  • Deserialising and copying. After decompression, ClickHouse copies only the granule’s rows into a column, then evaluates expressions and moves rows through the pipeline. With PREWHERE, which ClickHouse applies to most WHERE filters on its own (optimize_move_to_prewhere), the other columns are read only for rows that survive.

And for wide columns, the floor stops mattering. A 100-byte String granule is 800 KiB at 8,192 rows, so granularity 1,024 really does cut it to about 100 KiB. At 256 or 128 rows the floor comes back: 64 KiB is about 650 such values, so the block holds three or six granules. A JSON or payload column of about 1 KiB per row never hits the floor down to 64 rows.

That matches the only published measurement of granularity for lookups we could find, Altinity’s ClickHouse in the Storm, part 2 (May 2019, 4-core i5-2400, ClickHouse version not stated, results normalised in charts):

Table Columns Best index_granularity
int20M, 20M rows id UInt64, one Float64 “128 & 256”
fs12M, 12M rows id FixedString(16), 2× Float64, UInt32, UInt64, String “64 & 128”

The table with the wider rows preferred the smaller granularity. Altinity’s own explanation: “because key is bigger and more columns are involved”. The same post found that turning on the uncompressed block cache “gives up to 50% improvement”. That fits the floor: the cache removes the decompression a smaller granularity can’t. That connection is our reading. The post doesn’t break down where the time went. It also tops out at about 4,000 lookups per second “when data is in filesystem cache”, which is why the FAQ’s first answer to “can ClickHouse be a key-value store” is “no”.

What smaller granules cost

The primary index

The primary index holds one key value per granule, and it lives in memory once loaded (primary_key_lazy_load defaults to true, so loading happens on first use). The docs’ worked example: 8.87 million rows, 1,083 granules, key (UserID, URL), 96.93 KiB of primary_key_bytes_in_memory, on ClickHouse 22.2 (sparse primary indexes guide). That’s about 92 bytes per granule, most of it the URL strings.

Per billion rows with a single 8-byte key, the index is 0.98 MB at 8,192 and 62.5 MB at 128. Multiply by the key’s real width: a 50-byte composite key at 128 is about 390 MB per billion rows. Two settings help:

  • primary_key_ratio_of_unique_prefix_values_to_skip_suffix_columns (0.9) stops loading later key columns once a prefix column is nearly unique.
  • use_primary_key_cache (off; primary_index_cache_size 5 GiB) turns the always-in-memory index into an evictable cache (MergeTreeSettings.cpp).

Marks, per column

Every column has its own marks: 24 bytes per granule in .mrk2 before compression. Marks are compressed on disk (compress_marks, default true), and bit-packed in the mark cache: MarkInCompressedFile.h stores per-block minimums plus small differences, exploiting that “y is usually zero”. So the in-memory cost is usually well below 16 bytes per mark, by an amount that depends on the data. As an upper bound, take 16 bytes (the two 64-bit offsets):

Per 1B rows, 50 columns 8,192 1,024 256 128
Marks 6.1M 48.8M 195M 391M
Mark cache upper bound (16 B/mark) 98 MB 0.78 GB 3.1 GB 6.25 GB
Default mark_cache_size 5 GiB 5 GiB 5 GiB 5 GiB

The mark cache defaults to 5 GiB, SLRU (Defines.h). Marks are loaded per column file of a part, not per granule. So at small granularity, one lookup touching a cold part loads every mark of every column it reads in that part, and wide tables at 128 can push their own marks out of cache.

Rows per granule, per granule

With adaptive granularity (the default), rows per granule vary, and ClickHouse keeps “the row count of every granule in the part … in memory, 8 bytes each, which on large tables adds up to tens of gigabytes”. That’s the description of use_const_adaptive_granularity in 26.10. The cost is counted in system.parts.index_granularity_bytes_in_memory, and enable_index_granularity_compression (true) compresses it when it can. At granularity 128 that’s another 62.5 MB per billion rows before compression.

And 65,536?

Raising the granularity cuts every structure above by 8× compared with the default, at the price of 65,536-row minimum reads per column. But the 65,536 is often not reached. Adaptive granularity sets rows per granule to min(index_granularity, index_granularity_bytes / average row size) (MergeTreeIndexGranularity.cpp). With the default 10 MiB:

index_granularity Reached only while rows are below
8,192 1,280 bytes uncompressed
65,536 160 bytes uncompressed

So index_granularity = 65536 on a table with 400-byte rows gives about 26,000-row granules, not 65,536. It’s a setting for narrow, scan-heavy tables, such as metrics with a few numeric columns, where index and mark memory matter more than lookup latency.

The fix: lower the block floor too

If the goal is fast lookups on narrow columns, lower min_compress_block_size along with the granularity. ClickHouse accepts it per column; only min_compress_block_size and max_compress_block_size are allowed at column level (MergeTreeColumnSettings::validate):

CREATE TABLE kv
(
    id    UInt64 SETTINGS (min_compress_block_size = 4096),
    value Float64 SETTINGS (min_compress_block_size = 4096),
    doc   String              -- wide column: keeps the default
)
ENGINE = MergeTree
ORDER BY id
SETTINGS index_granularity = 256;

With 4 KiB, a UInt64 block holds two 256-row granules, so a lookup decompresses 512 rows instead of 8,192. The trade-off is compression ratio and overhead. Each block costs 25 bytes of header and checksum. ZSTD and LZ4 find fewer matches in a 4 KiB window than in a 64 KiB one, by an amount that depends on the data; we found no published measurement for ClickHouse columns. Like most MergeTree settings, it only applies to parts written after the change.

Alternatives that skip decompression altogether:

  • The uncompressed cache. use_uncompressed_cache = 1 per query or profile. The setting defaults to off. The stock config.xml sizes the cache at 8 GiB, but the compiled-in default (DEFAULT_UNCOMPRESSED_CACHE_MAX_SIZE) is 0. So check your server config before relying on it. The cache is skipped for queries reading over about 1M rows (merge_tree_max_rows_to_use_cache). The config.xml comment warns it “can be pointless for lz4” and “can sometimes paradoxically make queries slower”.
  • A dictionary. Altinity measured about 9,200 lookups per second for a hashed dictionary against about 4,000 for MergeTree, same hardware, 2019.
  • The columns cache (use_columns_cache, experimental in 26.10). It caches deserialised columns in stripes of about 65,536 rows (Settings.cpp). We found no published lookup measurement for it yet.

To see the floor on your own data, compare ProfileEvents['CompressedReadBufferBytes'] (uncompressed bytes decompressed) against read_rows in system.query_log for a single-key lookup.

What I couldn’t verify

  • Altinity’s per-granularity numbers are only in charts, the ClickHouse version is not stated, and they don’t say whether they changed min_compress_block_size. The link between their uncompressed-cache gain and the 64 KiB floor is our inference.
  • How much compression is lost at min_compress_block_size = 4096 on real columns: no published ClickHouse measurement found.
  • The real in-memory size of bit-packed marks per granule: it depends on the data. We used 16 bytes as an upper bound.
  • Lookups that hit two granules (a key run crossing a granule boundary) read two blocks at most. The tables assume one.
  • Compact parts (parts under min_bytes_for_wide_part, 10 MiB by default) store all columns in one file and are written by a different writer. The analysis above is for Wide parts, which is where large tables live.

FAQ

What is index_granularity in ClickHouse? The number of rows per granule: one primary-index entry and one mark per column every N rows, 8,192 by default. Adaptive granularity lowers it automatically when rows are wider than 1,280 bytes (10 MiB per granule).

Should I lower index_granularity for point lookups? Only together with min_compress_block_size for narrow columns, or when the columns you read are wide. Otherwise every lookup still decompresses a 64 KiB block per column: 8,192 rows of a UInt64 at 1,024, 256 or 128.

How much memory does a smaller index_granularity cost? Per billion rows with an 8-byte key: about 1 MB of primary index at 8,192, 7.8 MB at 1,024, 62.5 MB at 128. Marks cost up to 16 bytes per granule per column in the mark cache (5 GiB by default), plus 8 bytes per granule for adaptive row counts.

Does index_granularity = 65536 make scans faster? It shrinks indexes and marks 8×. But with the default 10 MiB index_granularity_bytes, granules only reach 65,536 rows when rows are under 160 bytes, so on wider tables the setting changes less than it seems.

Can I change index_granularity on an existing table? No. It’s fixed when the table is created. Create a new table and copy the data with INSERT ... SELECT. Column-level min_compress_block_size can be changed with ALTER TABLE t MODIFY COLUMN c MODIFY SETTING min_compress_block_size = 4096, but only parts written afterwards use it.

Tomorrow, day 10: what Nullable really costs in storage and speed, compared with default values.