Skip to content

Dictionary serialization for String - #133397

Draft
nihalzp wants to merge 19 commits into
ClickHouse:masterfrom
nihalzp:string-dictionary-granules
Draft

nihalzp wants to merge 19 commits into
ClickHouse:masterfrom
nihalzp:string-dictionary-granules

Conversation

@nihalzp

@nihalzp nihalzp commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Related: #107436
Related: #123531

A prototype of a new MergeTree serialization of String columns. It is a third value of string_serialization_version, next to single_stream and with_size_stream: with_dictionaries.

On disk

Each granule of a top-level String column in a Wide part is stored in one of two forms:

  • plain: the lengths and the bytes of its rows, exactly as with_size_stream stores them;
  • dictionary: each distinct value of the granule stored once, in sorted order, plus one small number per row, its code, that says which of those values the row has. Rows b, a, b, b become the values a, b and the codes 1, 0, 1, 1.

The writer holds the rows of a granule until the granule is complete, so it decides from exact sizes. A granule gets a dictionary when both hold:

  • the dictionary form is at least string_dictionary_min_size_reduction (default 0.1) smaller than the plain form, in uncompressed bytes;
  • its distinct values are at most string_dictionary_max_distinct_ratio (default 0.75) of its rows.

A column has five streams, each with marks per granule:

File Holds
s.strdict.idx.bin One record per granule: the form and the number of rows; for a dictionary granule also the dictionary size, the code width (1, 2 or 4 bytes) and then one code per row, its value's position in the dictionary
s.size.bin The lengths of the rows of plain granules
s.bin The bytes of the rows of plain granules
s.strdict.size.bin The lengths of the dictionary values of dictionary granules
s.strdict.bin The bytes of the dictionary values of dictionary granules

A granule of 8,192 rows with three distinct values:

s.strdict.idx    form = dictionary, rows = 8192, dictionary size = 3, code width = 1, codes = 2 0 0 1 2 1 ... (8,192 bytes)
s.strdict.size   0 10 9
s.strdict        google.comyandex.ru
  • The dictionary is sorted and unique, so '' is code 0 when the granule has it, and two granules with the same values write the same bytes.
  • Nothing is shared between granules. A granule is read with its own dictionary, so a read of one granule loads one small dictionary.
  • A column without any dictionary granule in a part keeps only s.bin and s.size.bin. The writer drops its empty dictionary streams, and the reader reads such a column exactly as with_size_stream.
  • Compact parts, String inside Nullable, Array, Map or Tuple, and sparse String columns keep with_size_stream.
  • Merges and mutations read the strings plain and choose the form of every granule again for the new part.

In memory

The reader returns the column as ColumnReplicated(values, indexes), which master already uses for dictionary pages of Parquet and for lazy JOIN replication. values is a ColumnString and indexes a column of UInt8, UInt16 or UInt32 positions in it.

A block covers several granules (about 8 of 8,192 rows at the default max_block_size), and all of them append to one ColumnReplicated:

  • a dictionary granule appends its dictionary to values once, and its codes, shifted by the number of values already there, to indexes;
  • a plain granule appends its rows to values, with indexes that point at themselves.

Three granules of four rows become one block:

granule 1, dictionary ['a', 'b'], codes [0, 1, 1, 0]
granule 2, plain      ['x', 'y', 'z', 'x']
granule 3, dictionary ['a', 'c'], codes [1, 1, 0, 0]

values  = ['a', 'b', 'x', 'y', 'z', 'x', 'a', 'c']
indexes = [0, 1, 1, 0,  2, 3, 4, 5,  7, 7, 6, 6]

'a' is in values twice, once for each granule that has it. The .size subcolumn reads the codes and the dictionary lengths, but none of the bytes.

A block whose granules are all plain is still a ColumnReplicated, with indexes 0, 1, 2, ... that only add an index to every row. TODO: return such a block as a plain ColumnString.

Operators

From here it comes down to how each operator handles ColumnReplicated. Master already runs a function once per entry of values when all its replicated arguments share their indexes, and converts the column to a plain ColumnString everywhere else. This PR makes these operators work on the dictionary values or through the indexes:

  • Functions and filters such as LIKE and REGEXP_REPLACE run once per dictionary value (master's path). When the substring filter of a PREWHERE (StringValueFilter) matches none of the values of a dictionary granule, its codes are skipped.
  • ORDER BY ... LIMIT: the top-K filter __topKFilter compares each dictionary value once.
  • length(s), empty(s) and s = '', which read s.size, use the codes and the dictionary lengths.
  • GROUP BY one String key: the keys are read through the indexes, without a copy, and each value is hashed once. When the values of a block repeat, the key of each position is looked up once, in the ordinary aggregation and in the frozen phase of the adaptive aggregator, with one count per position for a lone count.
  • GROUP BY fixed-width keys and one String: the string is read through the indexes.
  • Aggregates without GROUP BY that ignore repeated values (uniqExact, count(DISTINCT), groupUniqArray, min, max) take each used value once.
  • min and max of a String per group read through the indexes, and skip a row that repeats the value and the group of an earlier row of the block.

Everything else still converts the column: other aggregates of a String, keys with two or more strings, merges and mutations.

Disk usage

ClickBench hits (100M rows), one part in each form, both ZSTD(3). The whole table takes 8,686 MB on disk instead of 9,409 MB (−7.7%), and its 28 String columns 4,528 MB compressed instead of 5,253 MB (−14%). Uncompressed, those columns shrink from 43.9 GB to 18.7 GB (−57%), which is what decompression and memory bandwidth pay for.

Column Compressed Uncompressed
Title 1,212 → 980 MB (−19%) 10,287 → 3,645 MB (−65%)
URL 1,476 → 1,285 MB (−13%) 9,839 → 5,344 MB (−46%)
Referer 1,377 → 1,221 MB (−11%) 7,462 → 3,789 MB (−49%)
OriginalURL 788 → 735 MB (−7%) 6,117 → 4,117 MB (−33%)
PageCharset 8 → 2 MB 1,977 → 101 MB (−95%)
SearchPhrase 225 → 215 MB (−4%) 1,540 → 793 MB (−49%)
UserAgentMinor 45 → 22 MB 1,163 → 104 MB (−91%)
BrowserCountry 41 → 16 MB 1,137 → 101 MB (−91%)
FlashMinor2 41 → 18 MB 1,110 → 104 MB (−91%)
ParamCurrency 1 → 0 MB 1,100 → 100 MB (−91%)
BrowserLanguage 14 → 8 MB 1,003 → 102 MB (−90%)
HitColor 7 → 6 MB 900 → 100 MB (−89%)

Writing costs a little more CPU: the INSERT ... SELECT of the whole table takes 393 s of CPU instead of 367 s (+7%).

Results

ClickBench hits (100M rows) on AWS c8g.metal-48xl (192 cores, two NUMA nodes), clickhouse-local, FORMAT Null, each query 3 times and the faster of runs 2 and 3. Master is 521c4bcb7b9 reading hits; the PR reads the same table rewritten with string_serialization_version = 'with_dictionaries'. At 16 and 64 threads both run on one NUMA node, at 192 threads on both with interleaved memory. The table has the 26 queries that read a String column; speedup is master / PR, so above 1 is faster.

QueryShape16 threads64 threads192 threads
Master → PRSpeedupMaster → PRSpeedupMaster → PRSpeedup
Q5count(DISTINCT SearchPhrase)110.0 → 106.0 ms1.04×35.0 → 34.0 ms1.03×46.0 → 40.0 ms1.15×
Q10GROUP BY MobilePhoneModel: count(DISTINCT UserID), where MobilePhoneModel <> ''44.0 → 43.0 ms1.02×20.0 → 20.0 ms1.00×27.0 → 25.0 ms1.08×
Q11GROUP BY MobilePhone, MobilePhoneModel: count(DISTINCT UserID), where MobilePhoneModel <> ''52.0 → 50.0 ms1.04×23.0 → 22.0 ms1.05×32.0 → 30.0 ms1.07×
Q12GROUP BY SearchPhrase: count, where SearchPhrase <> ''127.0 → 112.0 ms1.13×51.0 → 44.0 ms1.16×81.0 → 61.0 ms1.33×
Q13GROUP BY SearchPhrase: count(DISTINCT UserID), where SearchPhrase <> ''162.0 → 152.0 ms1.07×65.0 → 62.0 ms1.05×101.0 → 99.0 ms1.02×
Q14GROUP BY SearchEngineID, SearchPhrase: count, where SearchPhrase <> ''144.0 → 133.0 ms1.08×55.0 → 52.0 ms1.06×82.0 → 69.0 ms1.19×
Q16GROUP BY UserID, SearchPhrase: count181.0 → 184.0 ms0.98×64.0 → 67.0 ms0.96×101.0 → 91.0 ms1.11×
Q17GROUP BY UserID, SearchPhrase: count, LIMIT 10 without ORDER BY10.0 → 10.0 ms1.00×9.0 → 9.0 ms1.00×18.0 → 16.0 ms1.12×
Q18GROUP BY UserID, minute(EventTime), SearchPhrase: count299.0 → 299.0 ms1.00×113.0 → 113.0 ms1.00×166.0 → 157.0 ms1.06×
Q20count, where URL LIKE '%google%'388.0 → 283.0 ms1.37×107.0 → 85.0 ms1.26×71.0 → 64.0 ms1.11×
Q21GROUP BY SearchPhrase: min(URL), count, where URL LIKE '%google%'484.0 → 343.0 ms1.41×138.0 → 102.0 ms1.35×90.0 → 83.0 ms1.08×
Q22GROUP BY SearchPhrase: min(URL), min(Title), count, count(DISTINCT UserID), where Title LIKE '%Google%'559.0 → 391.0 ms1.43×163.0 → 123.0 ms1.33×118.0 → 110.0 ms1.07×
Q23SELECT * where URL LIKE '%google%', ORDER BY EventTime LIMIT 1093.0 → 77.0 ms1.21×61.0 → 48.0 ms1.27×86.0 → 77.0 ms1.12×
Q24SELECT SearchPhrase where SearchPhrase <> '', ORDER BY EventTime LIMIT 1039.0 → 37.0 ms1.05×18.0 → 17.0 ms1.06×30.0 → 22.0 ms1.36×
Q25SELECT SearchPhrase where SearchPhrase <> '', ORDER BY SearchPhrase LIMIT 1082.0 → 70.0 ms1.17×29.0 → 26.0 ms1.12×37.0 → 28.0 ms1.32×
Q26SELECT SearchPhrase where SearchPhrase <> '', ORDER BY EventTime, SearchPhrase LIMIT 1038.0 → 37.0 ms1.03×19.0 → 17.0 ms1.12×34.0 → 24.0 ms1.42×
Q27GROUP BY CounterID: avg(length(URL)), count, where URL <> ''76.0 → 70.0 ms1.09×27.0 → 26.0 ms1.04×28.0 → 29.0 ms0.97×
Q28GROUP BY the domain of Referer: avg(length(Referer)), count, min(Referer)746.0 → 578.0 ms1.29×228.0 → 186.0 ms1.23×282.0 → 245.0 ms1.15×
Q30GROUP BY SearchEngineID, ClientIP: count, sum, avg, where SearchPhrase <> ''90.0 → 91.0 ms0.99×38.0 → 39.0 ms0.97×64.0 → 59.0 ms1.08×
Q31GROUP BY WatchID, ClientIP: count, sum, avg, where SearchPhrase <> ''111.0 → 111.0 ms1.00×53.0 → 52.0 ms1.02×98.0 → 87.0 ms1.13×
Q33GROUP BY URL: count638.0 → 491.0 ms1.30×212.0 → 174.0 ms1.22×252.0 → 241.0 ms1.05×
Q34GROUP BY 1, URL: count639.0 → 489.0 ms1.31×206.0 → 174.0 ms1.18×244.0 → 228.0 ms1.07×
Q36GROUP BY URL: count, where CounterID = 62 and more21.0 → 20.0 ms1.05×22.0 → 19.0 ms1.16×23.0 → 21.0 ms1.10×
Q37GROUP BY Title: count, where CounterID = 62 and more14.0 → 9.0 ms1.56×14.0 → 10.0 ms1.40×14.0 → 11.0 ms1.27×
Q38GROUP BY URL: count, where CounterID = 62 and more, OFFSET 100015.0 → 14.0 ms1.07×15.0 → 13.0 ms1.15×16.0 → 15.0 ms1.07×
Q39GROUP BY TraficSourceID, SearchEngineID, AdvEngineID, Src, Dst: count, where CounterID = 6239.0 → 38.0 ms1.03×37.0 → 38.0 ms0.97×43.0 → 41.0 ms1.05×
Geomean1.13×1.11×1.13×

Over all 43 queries the total time is 1.19× faster at 16 threads, 1.13× at 64 and 1.09× at 192, and the queries without strings stay the same. The LIKE queries (Q20-Q22) gain most at 16 threads, where the decompression and the search they save weigh most; the sorting queries (Q24-Q26) gain most at 192 threads.

Changelog category (leave one):

  • Experimental Feature

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Add the experimental serialization with_dictionaries of String columns in MergeTree (string_serialization_version): in Wide parts, each granule of a top-level String column is stored either as before or as a sorted dictionary of its distinct values plus a code per row, whichever is smaller, and it is read as a ColumnReplicated, so that functions, filters, GROUP BY and length work once per distinct value. On ClickBench, the String columns take 14% less space compressed and 57% less uncompressed, and the 26 queries that read them are 1.13x faster at 16 threads, 1.11x at 64 threads and 1.13x at 192 threads (geometric mean). The new settings string_dictionary_min_size_reduction (0.1) and string_dictionary_max_distinct_ratio (0.75) control when a granule gets a dictionary.


Workflow [PR]
Sync PR [sync-upstream/pr/133397]

@nihalzp nihalzp added the ci-performance performance only label Nov 21, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [77eef8d]

Summary: ❌

job_name test_name status info comment
Performance Comparison (amd_release, master_head, 1/6) FAIL Performance dashboard
utf8_ascii_prefix #0 FAIL dashboard
utf8_ascii_prefix #1 FAIL dashboard
utf8_ascii_prefix #2 FAIL dashboard
min_max_index_with_cast #0 FAIL dashboard
adaptive_aggregation_nullable_keys #4 FAIL dashboard
parallel_mv #0 slower query history
Performance Comparison (amd_release, master_head, 2/6) FAIL Performance dashboard
join_lazy_indexing #85 slower query history
join_lazy_indexing #87 slower query history
join_lazy_indexing #91 slower query history
join_lazy_indexing #95 slower query history
join_lazy_indexing #99 slower query history
join_lazy_indexing #102 slower query history
join_lazy_indexing #103 slower query history
join_lazy_indexing #106 slower query history
join_lazy_indexing #107 slower query history
join_lazy_indexing #110 slower query history
14 more test cases not shown
Performance Comparison (amd_release, master_head, 3/6) FAIL Performance dashboard
negative_limit_by_in_order #4 FAIL dashboard
sip_hash_128_strings #39 FAIL dashboard
sip_hash_128_strings #38 FAIL dashboard
sip_hash_128_strings #44 FAIL dashboard
sip_hash_128_strings #37 FAIL dashboard
sip_hash_128_strings #43 FAIL dashboard
sip_hash_128_strings #45 FAIL dashboard
merge_tree_insert #19 unstable query history
uniq #65 slower query history
Performance Comparison (amd_release, master_head, 4/6) FAIL Performance dashboard
to_valid_utf8 #3 FAIL dashboard
aggregation_top_k_group_limit #2 unstable query history
Performance Comparison (amd_release, master_head, 5/6) FAIL Performance dashboard
functions_with_hash_tables #3 unstable query history
sorted_key_prefix_runs #1 slower query history
Performance Comparison (arm_release, master_head, 1/8) FAIL Performance dashboard
system_clusters_replicated_databases #0 unstable query history
system_clusters_replicated_databases #1 unstable query history
Performance Comparison (arm_release, master_head, 3/8) FAIL Performance dashboard
if_string_hits #2 FAIL dashboard
uniq_hits_no_key_other #47 FAIL dashboard
flat_dictionary #7 unstable query history
hashed_dictionary_load_factor #19 unstable query history
map_lowcardinality_key_lookup #2 unstable query history
tpcds #91 unstable query history
Performance Comparison (arm_release, master_head, 4/8) FAIL Performance dashboard
Check Results FAIL
Performance Comparison (arm_release, master_head, 5/8) FAIL Performance dashboard
sorted_key_prefix_runs #1 FAIL dashboard
parallel_insert #0 FAIL dashboard
leftpad #5 FAIL dashboard
hashed_join_keys #2 unstable query history
range_hashed_dictionary #1 unstable query history
range_hashed_dictionary #3 unstable query history
range_hashed_dictionary #5 unstable query history
vectorize_aggregation_combinators #3 unstable query history
vectorize_aggregation_combinators #5 unstable query history
Performance Comparison (arm_release, master_head, 6/8) FAIL Performance dashboard
parallel_mv #0 FAIL dashboard
permuted_columns_cache #0 FAIL dashboard

@clickhouse-gh clickhouse-gh Bot added the pr-experimental Experimental Feature label Nov 21, 2026
@nihalzp nihalzp changed the title Dictionary-encoded granules for String columns Dictionary serialization for String Nov 21, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-data-types Type system & core datatypes used across query planning/execution. label Nov 21, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-performance performance only comp-data-types Type system & core datatypes used across query planning/execution. pr-experimental Experimental Feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant