ClickHouse LowCardinality vs String vs Enum: what happens after 8,192 distinct values
A LowCardinality dictionary holds 8,192 values, then ClickHouse starts another. Why 100,000 distinct values can still cost 1.9 bytes a row, and when Enum is the better pick.
On this page
Ask how many distinct values a LowCardinality column can take before it stops helping, and you get three answers from three good sources:
- The ClickHouse docs: below 10,000 distinct values it “mostly shows higher efficiency of data reading and storing”, and above 100,000 it “can perform worse in comparison with using ordinary data types”.
- The ClickHouse blog: “usually effective to a cardinality of a few hundred thousand, but we always recommend testing”.
- Altinity: “There is no hard limit in ClickHouse, the results are usually good if number of distinct values is below 10M.”
That is 10,000 to 10 million: three orders of magnitude. None of them is wrong. They disagree because the number that matters is not in any of them. It is 8,192, the default of a setting called low_cardinality_max_dictionary_size, and it applies to a run of neighbouring rows in a part, not to the table.
This is day 5 of the ClickHouse series. For this post I read the ClickHouse source at tag v26.10.2.103-stable and combined it with measurements other people have published. Nothing here was benchmarked by me: every number is linked to its source, and where I do arithmetic on top of a source, I say so.
The short answer
| Your column | Use | Why |
|---|---|---|
| A fixed set of values that you control (status, log level, event type) | Enum8 |
1 byte per row, rejects typos, sorts in the order you define |
| Repeating strings where new values appear without warning (country, service name, user agent family) | LowCardinality(String) |
1 or 2 bytes per row, no schema change for a new value |
| Many distinct values, but the table is sorted so equal values sit together (the sorting key itself, or a column that follows it) | LowCardinality(String) still works |
The dictionary limit counts values in neighbouring rows, not in the table |
| Many distinct values scattered randomly (request IDs, URLs with parameters, session tokens) | String |
Each granule would carry almost all of its strings anyway, plus the positions |
Numbers, dates, short FixedString |
The plain type | ClickHouse refuses LowCardinality here by default |
The rest of the post explains each row.
What each type stores
String stores every value in full: a length, then the bytes. A column of 11-character station IDs costs about 12 bytes per row before compression.
Enum8 and Enum16 store a number. The names live in the table definition, not in the data. An Enum8 “can contain up to 256 values enumerated in the [-128, 127] range” and an Enum16 up to 65,536; in memory the column “is stored in the same way as Int8 or Int16” (docs). So 1 or 2 bytes per row, always.
LowCardinality(String) stores a dictionary of the distinct strings and, for each row, the position of its string in that dictionary. Nobody declares the dictionary. ClickHouse builds it while writing each part.
Published column sizes show what that does. I divided the uncompressed size of each column by the row count, for two public datasets:
| Column (dataset) | As String |
Dictionary-encoded | Compressed on disk |
|---|---|---|---|
station_id, about 100k distinct values (NOAA, ~1.08 billion rows) |
11.7 B/row | 1.9 B/row (LowCardinality) |
7.58 → 3.40 MiB |
name, about 100k distinct values (NOAA) |
14.4 B/row | 1.9 B/row (LowCardinality) |
9.60 → 3.99 MiB |
weatherType, 22 values (NOAA) |
6.8 B/row | 1.0 B/row (Enum8) |
37.72 → 16.48 MiB |
OriginCityName (ontime, 172,338,036 rows) |
14.2 B/row | 1.5 B/row (LowCardinality) |
427.0 → 161.3 MB |
FlightNum (ontime) |
4.5 B/row | 2.1 B/row (LowCardinality) |
537.6 → 330.6 MB |
The bytes-per-row figures are my division of the published sizes; the NOAA row count is the “1.08 billion rows” the article’s query output reports, so those are rounded. Neither article names its ClickHouse version (they are from December 2022 and March 2019).
Two things are worth noticing. A dictionary-encoded column costs 1 to 2 bytes per row no matter how long the strings are. And the NOAA station_id column has about 100,000 distinct values, right where the docs say LowCardinality “can perform worse”. It did not. It went from 11.77 GiB to 1.92 GiB uncompressed. The source code explains why.
The 8,192 limit, read from the source
The setting is declared in Settings.cpp:
low_cardinality_max_dictionary_size, default 8192: “Sets a maximum size in rows of a shared global dictionary for the LowCardinality data type that can be written to a storage file system. This setting prevents issues with RAM in case of unlimited dictionary growth. All the data that can’t be encoded due to maximum dictionary size limitation ClickHouse writes in an ordinary method.”
Its companion, low_cardinality_use_single_dictionary_for_part (default false), adds: “By default, the ClickHouse server monitors the size of dictionaries and if a dictionary overflows then the server starts to write the next one.”
The code that does this is SerializationLowCardinality.cpp. A MergeTree wide part writes each column one granule at a time (MergeTreeDataPartWriterWide::writeSingleGranule), and a granule is 8,192 rows by default. For every granule of a LowCardinality column, the writer does this:
- It takes the distinct values of this granule and adds them to a dictionary that it keeps for the part, but only until that dictionary holds 8,192 entries.
- Values that no longer fit become “additional keys”. They are written in full next to this granule’s positions, in the column’s data file, and belong to this granule only.
- If the dictionary is now full, it is written to the dictionary file and dropped. The next granule starts an empty one.
- The positions are written. Their width depends on how big the dictionary is at that moment: one byte while it has up to 256 entries, two bytes after that. With the default limit and default granule size, a position never needs more than two bytes.
When a part ends, whatever is in the dictionary is written out. The comments in the file describe the layout the same way: the dictionary “can be shared for continuous range of granules”, and a flag at “the start of each granule” tells the reader whether it has to load a new dictionary and whether additional keys follow.
With low_cardinality_use_single_dictionary_for_part = 1, step 3 is skipped. The part gets one dictionary with the first 8,192 values it saw, and every later new value is stored as an additional key in each granule that uses it.
So the limit does not turn LowCardinality off at 8,192 distinct values, and nothing fails. It decides how often the strings are written again.
What that means for five kinds of column
Here I apply those rules by hand. This is arithmetic, not a measurement: I assume the default settings, 8,192-row granules, and either perfectly clustered or uniformly random values. Real data sits in between, and all sizes are before compression.
| Column | What the writer does | Strings stored | Positions |
|---|---|---|---|
| 1,000 distinct values, any order | One dictionary per part | 1,000 per part | 2 B/row |
| 100,000 distinct values, sorted so equal values are neighbours | A dictionary fills only after 8,192 different values have gone by | Each value about once per part | 2 B/row |
| 10,000 distinct values, random order | A granule holds about 5,600 different values. The dictionary is full in the third granule; about 950 values overflow | About 0.37 per row | 2 B/row |
| 100,000 distinct values, random order | A granule holds about 7,900 different values. The dictionary is full in the second granule; about 6,900 values overflow | About 0.92 per row | 2 B/row |
| Every value different | Each granule fills a dictionary on its own | 1 per row | 2 B/row |
The second row is the NOAA table. It is declared ORDER BY (station_id, date), and about a billion rows over about 100,000 stations is roughly 10,000 rows per station (my division). Each granule therefore sees one or two stations, a dictionary lasts for thousands of granules, and the column costs its 2 bytes of position per row: the 1.9 B/row in the published numbers.
The fourth row has the same number of distinct values, in a different order. Now 92% of the rows bring their string along anyway, and every row pays 2 more bytes for a position. With those figures, the encoded column is smaller than a plain String only when the strings average more than about 25 bytes (2 bytes ÷ 0.08 saved). For short values it is bigger. Reading is more work as well: for a granule with additional keys, the reader builds a separate dictionary from the shared entries the granule uses plus its own extra keys, where a clean granule just points at the shared one.
So the three rules of thumb at the top describe three different tables. 10,000 comes out ahead in any order for all but the shortest strings. A few hundred thousand works when the sort order helps. 10 million needs data that is very well clustered, and Altinity says as much: the total can be higher “if 10M limit is preserved at part level”, and performance “varies a lot depending on number of distinct values, string sizes and part size”.
The ontime numbers show this too. FlightNum ends up at 2.1 bytes per row, slightly above the 2 bytes of a position. The article gives one count for it, uniq(FlightNum) = 7,765 for flights from Chicago alone, so the whole column has at least that many flight numbers, close to or above 8,192. The extra 0.1 byte per row is consistent with dictionaries being written more than once per part. I can’t confirm it from the article, which does not show the table’s sort order. And OriginCityName at 1.5 bytes per row points to a mix of one-byte and two-byte positions: some stretches of the data would have been written while the dictionary still had fewer than 256 cities.
Compression hides most of the difference on disk
Look at the last column of the table of published sizes. OriginCityName shrinks 9.2× before compression but 2.6× on disk. A general-purpose codec such as LZ4 or ZSTD is very good at repeated strings, so a String column full of repeats is already small on disk. The NOAA station_id column was 7.58 MiB compressed as a plain String, out of a table of 3.83 GiB.
The larger gain is in memory and CPU. A query has to decompress the column and work on it, and the decompressed size is where LowCardinality saves the most: 6 to 9× on the long columns above. In Altinity’s test, a GROUP BY OriginCityName query over 172 million rows went from 2.089 s to 0.595 s (3.5×), and the data it processed from 3.82 GB to 281 MB. A second query, a drill-down into flights from Chicago, went from 2.197 s to 1.475 s, and to 1.064 s once a second column was also LowCardinality. Altinity’s explanation is that ClickHouse can cache a shared dictionary and “pre-calculate hashes for group by”, so the work is done once per distinct string and not once per row.
Recent releases keep adding to that side, according to the 26.x changelog:
| Version | Change |
|---|---|
| 26.7 | Hash joins natively support a single non-Nullable LowCardinality(String) join key (#107264) |
| 26.9 | Fixes a CPU regression present “since 26.6”: many small reads from a part with LowCardinality columns were “up to several times slower” on tables with a small index_granularity (#116134) |
| 26.9 | The TimeSeries engine stores series IDs as LowCardinality(UUID) by default, for a “1.16x geometric-mean PromQL speedup in the benchmark” (#117033) |
| 26.10 | GROUP BY over several LowCardinality keys reads the keys from the dictionaries instead of copying them into full columns, and caches aggregate states per tuple of positions (#121795, #121998) |
If you run 26.6 to 26.8 with a small index_granularity and many point reads, the second row is a reason to upgrade.
LowCardinality or Enum?
The docs give a recommendation: “Consider using LowCardinality instead of Enum when working with strings. LowCardinality provides more flexibility in use and often reveals the same or higher efficiency.”
The storage argument for Enum8 is weaker than it looks. Step 4 above says a LowCardinality position takes one byte while the part’s dictionary has up to 256 entries. That is the same range an Enum8 covers. For a column with 20 values, both cost 1 byte per row; the only extra is one tiny dictionary per part.
The differences that remain are about behaviour:
Enum8 / Enum16 |
LowCardinality(String) |
|
|---|---|---|
| A value you did not expect | “If you try to save any other value, ClickHouse will raise an exception.” | Stored as a new dictionary entry |
| Adding a value | ALTER TABLE, which the docs call “without cost … if only the set of values is changed” |
Nothing to do |
ORDER BY |
By the number you assigned (“Enums behave the same way as the corresponding numbers”) | By the string |
| Limit | 256 or 65,536 values | None, but see the 8,192 rule |
| Where the names live | In the table definition | In every part |
So pick Enum when you want the database to reject bad values, or when you want a custom order such as 'low' = 1, 'medium' = 2, 'high' = 3. Pick LowCardinality(String) when values arrive from outside and an exception on a new one would break ingestion. Adding an Enum value is cheap, but it is still a schema change that has to happen before the first row with that value arrives:
ALTER TABLE events
MODIFY COLUMN status Enum8('ok' = 1, 'error' = 2, 'timeout' = 3);
Types ClickHouse refuses to wrap
LowCardinality(UInt8), LowCardinality(Date) and other small fixed-size types fail at CREATE TABLE with SUSPICIOUS_TYPE_FOR_LOW_CARDINALITY unless you set allow_suspicious_low_cardinality_types = 1. The setting covers “data types with fixed size of 8 bytes or less”, and its description gives the reason: “ClickHouse stores a numeric index for each row”, so disk usage can rise and RAM use can be higher. A 2-byte position that points at a 4-byte number saves very little, and the plain number with a codec usually does better (day 4 covered codecs for numbers).
For nullable strings the order matters: write LowCardinality(Nullable(String)), with Nullable inside.
How to check a column you already have
Compare the two sizes ClickHouse already tracks. This query shows bytes per row before and after compression for each column of a table:
SELECT
column,
any(type) AS type,
round(sum(column_data_uncompressed_bytes) / sum(rows), 2) AS raw_bytes_per_row,
round(sum(column_data_compressed_bytes) / sum(rows), 2) AS disk_bytes_per_row
FROM system.parts_columns
WHERE active AND database = currentDatabase() AND table = 'events'
GROUP BY column
ORDER BY raw_bytes_per_row DESC;
How to read it for a LowCardinality(String) column:
- About 1 or 2 raw bytes per row: the dictionary is doing its job.
- Clearly more than 2: strings are being stored again and again. Either the values are too scattered for an 8,192-entry dictionary, or the column is simply not low-cardinality. Try it as a plain
Stringon a copy of the data and compare.
Changing the type is one statement (ALTER TABLE ... MODIFY COLUMN col LowCardinality(String)), but it rewrites the column in every part. Test on a copy first. Raising low_cardinality_max_dictionary_size is possible too. The setting exists, in the words of its description, to prevent “issues with RAM in case of unlimited dictionary growth”, so bigger dictionaries cost memory on every insert, merge and read. I found no published measurement of that trade-off, so I am not recommending a value.
FAQ
How many distinct values can a LowCardinality column have? There is no hard limit. With default settings a dictionary holds 8,192 values, then ClickHouse writes it and starts another. What matters is how many different values appear in neighbouring rows of a part. Up to a few thousand is fine in any order. More than that works only when equal values are stored close together.
Is LowCardinality faster than String?
For repeated values, yes. In Altinity’s published test on 172 million rows, a GROUP BY went from 2.089 s to 0.595 s. For near-unique values it is larger and slower than String.
Is Enum smaller than LowCardinality?
Hardly. Below 256 distinct values both use 1 byte per row; LowCardinality adds one small dictionary per part. Choose Enum for validation and custom sort order.
What happens when a LowCardinality dictionary exceeds 8,192 values? Nothing fails. The full dictionary is written, a new one starts, and values that did not fit are stored next to their granule. The column gets bigger and reads do more work.
Can I use LowCardinality on numbers?
Not by default. Types of 8 bytes or less need allow_suspicious_low_cardinality_types = 1, and they rarely benefit.
Does LowCardinality change my queries?
No. The column behaves like its inner type in SQL. Some third-party clients do not understand the type in the Native format; low_cardinality_allow_in_native_format = 0 makes the server send ordinary columns to them.
What I could not verify
- The arithmetic table uses idealised data (perfectly sorted or uniformly random). I did not run it on a server.
- The
FlightNumexplanation (dictionaries written more than once per part) fits the numbers but is not confirmed by the Altinity article. - No independent measurement of raising
low_cardinality_max_dictionary_size, or oflow_cardinality_use_single_dictionary_for_part = 1, turned up in my search. - The source I read is 26.10.2.103. The two published datasets were measured on much older versions that the articles do not name. The on-disk format has had a single version since it was introduced (
SharedDictionariesWithAdditionalKeys, the only value in the source), which is why I compare them.
Sources
- ClickHouse docs: LowCardinality, Enum.
- ClickHouse source at v26.10.2.103-stable: SerializationLowCardinality.cpp, ColumnUnique.h (position width), MergeTreeDataPartWriterWide.cpp, Settings.cpp.
- Dale McDiarmid, Optimizing ClickHouse with Schemas and Codecs, ClickHouse blog, 2022-12-14 (NOAA dataset).
- Alexander Zaitsev, A Magical Mystery Tour of the LowCardinality Data Type, Altinity, 2019-03-27 (ontime dataset).
- ClickHouse changelog for 2026 (26.7, 26.9, 26.10 entries).
Next in the series: skip indexes that don’t skip.