Repository navigation
Closed
Labels
questionQuestion?
Description
Company or project name
No response
Question
I have a table that is used for lookups by primary key:
CREATE TABLE stack_map_v2_staging_replica
(
`id` UInt128 CODEC(NONE),
`stack` Array(LowCardinality(String)) CODEC(ZSTD(1)),
`seen` datetime CODEC(ZSTD(1)),
)
ENGINE = ReplacingMergeTree(seen)
PRIMARY KEY (id, stack)It turned out to be rather slow with the default index granularity when looking up 10000 random ids, so I lowered the granularity down to 32:
CREATE TABLE stack_map_v2_fine_granularity_staging_replica
(
`id` UInt128 CODEC(NONE),
`stack` Array(LowCardinality(String)) CODEC(ZSTD(1)),
`seen` datetime CODEC(ZSTD(1)),
)
ENGINE = ReplacingMergeTree(seen)
PRIMARY KEY (id, stack)
SETTINGS index_granularity = 32The interesting thing is that the new table is more than 2x bigger than the old one, exclusively due to stack:
SELECT
`table`,
column,
sum(rows) AS rows,
sum(column_data_uncompressed_bytes) AS uncompressed,
sum(column_data_compressed_bytes) AS compressed,
toDecimal64(uncompressed / rows, 2) AS uncompressed_per_row,
toDecimal64(compressed / rows, 2) AS compressed_per_row,
toDecimal64(uncompressed / compressed, 2) AS compression_ratio
FROM system.parts_columns
WHERE (`table` IN ('stack_map_v2_staging_replica', 'stack_map_v2_fine_granularity_staging_replica')) AND (database = 'r0') AND active
GROUP BY
`table`,
column,
column_position,
type
ORDER BY
`table` ASC,
column_position ASC
SETTINGS output_format_decimal_trailing_zeros = 1 ┌─table─────────────────────────────────────────┬─column─┬─────rows─┬─uncompressed─┬──compressed─┬─uncompressed_per_row─┬─compressed_per_row─┬─compression_ratio─┐
1. │ stack_map_v2_fine_granularity_staging_replica │ id │ 59013307 │ 943170144 │ 943530044 │ 15.98 │ 15.98 │ 0.99 │
2. │ stack_map_v2_fine_granularity_staging_replica │ stack │ 59013307 │ 175267577852 │ 22782374373 │ 2969.96 │ 386.05 │ 7.69 │
3. │ stack_map_v2_fine_granularity_staging_replica │ seen │ 59013307 │ 235792536 │ 80619729 │ 3.99 │ 1.36 │ 2.92 │
4. │ stack_map_v2_staging_replica │ id │ 64879009 │ 1038064144 │ 1038262319 │ 16.00 │ 16.00 │ 0.99 │
5. │ stack_map_v2_staging_replica │ stack │ 64879009 │ 77948447028 │ 10678442703 │ 1201.44 │ 164.59 │ 7.29 │
6. │ stack_map_v2_staging_replica │ seen │ 64879009 │ 259516036 │ 91848672 │ 4.00 │ 1.41 │ 2.82 │
└───────────────────────────────────────────────┴────────┴──────────┴──────────────┴─────────────┴──────────────────────┴────────────────────┴───────────────────┘
How come? Are dictionaries maintained per granule and not per part as I expected? Is it something else?
Metadata
Metadata
Assignees
Labels
questionQuestion?
Activity
den-crane commentedon Mar 7, 2026
Try
optimize finalfor both tables. Each part has its own dictionary.In general index_granularity < 256 has a little sense because of compression blocks (64k-1mb).
With index_granularity=32, there is a high probability that ClickHouse will need to decompress the same amount of data (1 MB) as with index_granularity=256, but will incur a loss due to the bloated primary key size.
bobrik commentedon Mar 8, 2026
@den-crane I do see difference between 32 and 128 for the number of rows read:
If there's a log line that could say that going below 256 is useless, I'd be very interested.
I have a similar setup at home where I can do DDL at will. I made a snapshot the table and variants with different granule sizes.
Make sure to scroll horizontally for the comments. Note that all have just 2 parts, so not much use doing
optimize final.There's a 3x difference in uncompressed size between 32 and 128k. There's also sizeable difference even between 4k and 8k.
We can then compare files on disk to see what drives it for the extreme cases of 32 and 128k:
To highlight the biggest driver:
It does look like it is driven by the dictionary size, which increased 10x between granule size of 32 and 128k.
There are 78k unique frames across all stacks with an average stack depth of 90:
bobrik commentedon Mar 9, 2026
There's something wrong with setting propagation.
I create a table with the explicit settings:
Then I also set the settings on the session in
clickhouse-localfor good measure:With the following change:
For this insert:
I'm getting the default values (lots of lines like this):
If I force
low_cardinality_use_single_dictionary_for_part=1in code:Then it does what I expect it to do and the dictionary is shrunk to a reasonable size:
I suppose this does not explain why index granularity balloons the dictionary size, but it points to the related issue of the compensating levers of
low_cardinality_use_single_dictionary_for_partnot being connected to anything.bobrik commentedon Mar 9, 2026
Here's a theory: for granule size of 128k the dict overflows in every granule and overflow keys are written and deduplicated per granule. There are only a few granules, so this is rather efficient. For granule size of 32, the dict overflows much more often, which triggers the same process of writing overflow keys per granule that overflows, but there are way more granules, so there are way more dictionaries flushed.
ClickHouse/src/DataTypes/Serializations/SerializationLowCardinality.cpp
Lines 519 to 524 in 20f9a61
While dictionaries are per part, dictionary flushing and overflow keys are per granule (that overflows). Larger granules dampen the effect because they reduce the number of times when a dictionary can be flushed. It's still very much unexpected and there's probably a better way to handle this.
Ultimately the fix for my data with existing assumptions is
low_cardinality_use_single_dictionary_for_part=1to force one dictionary per part (however large it is), but it seems broken as it never applies, as described in my previous comment.bobrik commentedon Mar 9, 2026
This does the trick:
Uncompressed size is now smaller for 32 with settings overrides than for 128k with no overrides:
The dictionary size for 32 is the same as 128k with no setting overrides:
I kind of expected the dict to be smaller too now that we do 1 dict instead of 9 (for 128k), but it's too late to dig into it today.
den-crane commentedon Mar 9, 2026
@bobrik
I would not trust these numbers much. They are different but it has zero impact on performance.
_query_duration_ms / CompressedReadBufferBytes -- are the same (and they are sums for 100000 executions).
From my POV index_granularity has no good impact on performance for this synthetic dataset.
From my naive understanding: if a table's granule fits into 1Mb then there is no difference how many rows in the granule, 32/256/8192, ClickHouse would process (read/decompress) 1Mb anyway.
If the average
stacksize ~1000 bytes, then with index_granularity=8192 => granule_size = ~8MB, has sense to reduce index_granularity 8192 / 8 = 1024 to make granule = 1Mb.In my case avg stacks' size is less than 112 bytes.
den-crane commentedon Mar 9, 2026
It has no sense to use users' settings in DDL.
ClickHouse allows users' settings because some settings impact on the CREATE TABLE process.
It works as
These
QUERY SETTINGS low_cardinality_max_dictionary_size = 1048576, low_cardinality_use_single_dictionary_for_part = 1are noop on CREATE TABLE.
You need to define low_cardinality_max_dictionary_size/low_cardinality_use_single_dictionary_for_part against insert (query/session/user)
bobrik commentedon Mar 10, 2026
These are toy tables. My production table is 150 million entries and I want to look up 100k to 500k ids that I happen to need to resolve at a time. I wouldn't focus too much on the exact granule size until the dictionary ballooning is under control. I will review the exact right number that works for us, but this is secondary to the problem at hand.
The blocks size is different between 32 and 8192 (64k vs 1M):
You can see that compression ratio is worse, but the uncompressed size is also a lot smaller (#98968 (comment)).
That's pre-dictionary. It doesn't matter how long the raw strings are, we are not storing them like that.
The number is 8423 for me.
I agree, that's why I put them into
CREATE TABLESETTINGSblock, where they do make sense (to me at least), similar to index granularity. It doesn't work there as it's not a table setting as you correctly note.It also doesn't work if I set them in the session that does the
INSERTquery inclickhouse-local:That's on v26.1.1.1-new. The same sequence produces the same outcome with a proper ClickHouse server v25.10.3.100.
Maybe it does work as a global setting or a profile setting. From the code that I changed, it looks like
global_settingsis what's getting consulted, sort of makes sense if you remember that merges happen outside of user sessions and they need to look at the settings to decide how to lay out dictionaries. That's not very flexible though. Table level setting as with my patch feels right.cc @KochetovNicolai, please have a look at #98968 (comment) and #98968 (comment) as you seem to be the person who touched a bunch of the related low cardinality code.
bobrik commentedon Mar 10, 2026
From #98968 (comment):
They are not exactly the same, just very close:
There are clearly multiple logical blocks (smaller than 1048576) for 128k:
I'm using the same logical block splitting heuristic here as I noted in #96704.
There's only one logical block for 32 with one dictionary forced via my patch:
Decompressing both and checking for duplicates:
None for 32:
At many as 9 for 128k (matching the number of dictionaries/logical blocks):
One intuitive thought here is that having 10x number of dictionaries does not make much of a difference. You have to remember to zoom out and see that it does in fact help when you look at the top line number:
bobrik commentedon Mar 10, 2026
I deployed this into production for the default profile:
The parts that were merged after a restart show a clear improvement (10x uncompressed size!):
Similarly sized part of ~9 million rows (rows 11 and 2 above, respectively):
Similar-ish sized part from the previous table with
index_granularity = 8192and 11.2m rows:Having a single dict is the clear winner at 5.5x smaller row vs multiple dicts with granule size of 32 and 2.38x smaller row vs multiple dicts and granule size of 8192.
bobrik commentedon Mar 11, 2026
There are unexpected issues with repeated decompressions: #99236.