LowCardinality and Data Types for Smaller Tables

Dictionary encoding, enums, and codecs shrink tables and speed up queries.

· 8 min read
Cover illustration for “LowCardinality and Data Types for Smaller Tables”
ClickHouse Table Sizing · November 21, 2026 · 8 min read · 1,874 words

Smaller tables start with better data types as well as stronger compression. LowCardinality, Enum, and codec-paired numerics are the highest-leverage schema choices because they change both how much data is stored and how the engine processes it. Their gains compound with sort keys and storage tiers, and can make a well-typed analytical table fast enough to serve an application directly.

Why data type selection is the highest-leverage schema decision a team can make

Schema design often looks like a correctness exercise. Can this field hold the value, and does its definition match what the application expects? Those questions matter, but the same definition also controls how much space the data takes up, how queries run, and the cloud bill. Most talks about optimization start with indexes or partitioning, even though the choice of types comes first and shapes what those later choices can do.

That makes types more than labels on columns. Dictionary encoding through LowCardinality, fixed-set semantics through Enum, and codec-paired numerics each reduce stored bytes while changing how the engine processes values. Smaller columns let more data fit in CPU cache, and vectorized execution has less work to do with small integers than with variable-length strings. Compression can then act on those smaller representations, so the benefit of a good type choice extends into the decisions that follow it.

How LowCardinality works and why it speeds up more than storage

LowCardinality(T) stores a compact dictionary of distinct values and replaces each row’s value with a small integer index pointing into that dictionary. The row holds a reference to it instead. That substitution is the basis of both the storage saving and the cheaper execution path.

The execution gain follows from what the engine handles. Vectorized operations can work on shorter integers in place of processing variable-length strings over and over. This is not limited to strings, because LowCardinality works with most scalar types. Repeated numeric or date values can therefore be candidates for the same treatment.

The dictionary approach pays off when distinct values stay well below a few thousand, but its own size becomes a burden as that count grows. What if a small vocabulary could expand later? The response is to measure it with uniqExact() and monitor its growth. A column that stays below the threshold retains its gains.

When to use Enum instead of LowCardinality, and when the choice matters

The code-review rule is simple enough to apply directly. Use Enum8 for a fixed, schema-defined vocabulary, and LowCardinality for a small vocabulary that must accept new values without a deployment. Both can represent repeated categories compactly, but they make different promises about change. Mutability is the deciding factor.

Enum8 and Enum16 enforce a closed set of values. Enum8 encodes its values as single bytes, making it a good fit for fixed order statuses, event categories, or HTTP methods. The schema also documents which values belong there. That guarantee has a cost, because adding a variant requires a schema change. A category that looks settled today is not necessarily a closed vocabulary.

LowCardinality makes the opposite trade-off by letting its dictionary expand at write time. User-supplied tags, region codes, and product SKUs can fit that model when each has only a small number of distinct values. Using Enum for a growing vocabulary means running ALTER TABLE operations in production. Using LowCardinality for a truly fixed set, however, gives up schema enforcement for room to grow that the set does not need.

How codecs stack on top of dictionary encoding to push compression further

Codec selection is separate from type selection, so neither replaces the other. LowCardinality first shrinks the representation into dictionary indexes, and a codec can compress the resulting integer stream further. The second choice acts on what the first leaves behind.

For monotonically increasing numerics, CODEC(Delta) stores how much each value differs from the one before it in place of storing the full values. Those differences are typically much smaller numbers. ZSTD can then compress the deltas. The practical rule is to apply Delta to values that trend monotonically, including suitable timestamps, sequence IDs, and financial amounts, whether or not LowCardinality is also useful.

The remaining codec choice balances latency against density. LZ4 decompresses faster and suits hot, frequently queried columns, while ZSTD achieves higher density for cold or archival data where infrequent queries justify the extra decompression work. Consider a financial dataset whose amounts trend monotonically. Delta turns those amounts into differences between one value and the next, and ZSTD compresses that stream, while dictionary encoding can also save space in any columns with repeated values. The financial label alone does not establish the fit. The values’ pattern does.

Approximate types for counting and aggregation when exactness is not required

The same reasoning extends from stored columns to aggregation state. Keeping raw strings for exact unique counts can consume memory and slow execution, just as keeping repeated full strings can inflate a column. Exactness has a cost of its own. Before choosing an aggregation strategy, the question is whether the downstream consumer needs an exact integer or whether an answer within one percent at a tenth of the storage cost would support the same decision.

An approximate counting approach belongs in that discussion when exactness is not required. For dashboards, monitoring, and product analytics, fast approximate counts will often serve the decision better than slower, more expensive exact counts. If the consumer needs an exact result, the smaller representation is not an equivalent substitute.

How tiered storage multiplies the gains already won at the type level

Types and codecs reduce the number of stored bytes. Tiered storage reduces what those bytes cost. The bill depends on both. A smaller representation and a cheaper storage tier can compound savings. Cold data is where that second reduction becomes useful, because its access pattern no longer justifies paying for the fastest storage.

The pattern is to keep recent, hot data on fast local or networked storage with LZ4 for query speed. Older data moves automatically to object storage, with ZSTD(7) at the high end of the density range. The access penalty is acceptable because queries reach that tier rarely.

Compute-storage separation lets data move this way without tying where it is stored to a single node’s disk. Stateless compute nodes read from object storage, so moving a partition to a cold tier does not require reshuffling data across a cluster. Fewer bytes remain fewer bytes, even after their storage price falls.

Using LowCardinality in sort keys to get compression and primary index efficiency at once

A sort key connects column representation to query access. It determines which columns the primary sparse index covers and therefore which filters can skip data granules. A compact column is helpful, but a compact column that also supports frequent filters can do a second job.

In a migration from another data warehouse, carrying over event_id as an unenforced primary key reflects its data manipulation model but is the wrong starting point for this sort-key decision. Low-cardinality fields used frequently in WHERE clauses, such as event_type, region, and status, make better leading components. Their value comes from the query pattern. The engine can skip whole granules whose values do not match, avoiding reads and subsequent row discards.

With LowCardinality in the key, index entries use integer comparisons in place of string comparisons, so the primary index takes less work to scan. The declaration has already cut the space the column takes up. Its placement in a useful sort key now lets the same representation improve filtering as well, which is the bridge from smaller tables to responsive queries.

Eliminating the warehouse-to-API pipeline by serving directly from a well-typed table

The standard warehouse-to-API pattern compensates for analytical stores that have historically been too slow for sub-100ms responses. Analytical queries run in the warehouse, results are materialized into Redis or Postgres, and CDC or dbt jobs keep the serving copy current. That arrangement introduces another system to operate. It also brings consistency and sync lag into the work of serving a result that would otherwise be simple.

LowCardinality reduces scan width, a suitable sort key enables granule skipping, and materialized views pre-aggregate common query shapes. Together, these can make point lookups and dashboard queries fast enough to serve directly. The payoff is conditional on the resulting speed.

Caching handles the remaining latency gap where needed. Real-time metrics can use a short cache of minutes, while daily aggregates can use an hour or more, with both served from the same analytical table. Kafka ingestion and incremental materialized views keep the serving table current without a batch refresh cycle. The pipeline therefore contracts into the ingestion path, eliminating the export path. Removing that second path removes the work of synchronizing two copies, debugging their lag, and paying for two storage systems.

Where the LowCardinality approach breaks down and what to do instead

LowCardinality becomes counterproductive when its dictionary grows too large. Beyond a few thousand distinct values, the dictionary can become overhead rather than a compression tool, and query performance can degrade relative to a plain type. The benefit depends on repetition within a sufficiently small set of values.

High-cardinality raw URLs, full user-agent strings, free-text fields, and UUID event identifiers are poor candidates. Applying LowCardinality to them adds dictionary maintenance without the integer-comparison speedup. Blanket use is the common mistake here. A useful optimization for repeated categories becomes a penalty when applied to every string column simply because its type is String.

The diagnostic step is to run uniqExact() on actual production data before choosing the representation. LowCardinality should be applied column by column against a measured threshold, with monitoring for vocabularies that may grow. For genuinely high-cardinality strings used in filters, bloom filter indexes or inverted indexes are the alternatives, depending on the query pattern. That distinction separates two different problems. Dictionary encoding exploits repetition, while filtering high-cardinality strings may call for a different access tool.

A schema review checklist that applies the full type-choice argument

Start by running uniqExact() on every string and repeated-value column, flagging low distinct-value counts as LowCardinality candidates. This establishes the evidence for each type decision before compression or key design can obscure it.

Among the flagged columns, separate fixed vocabularies from open ones based on whether adding a value should need a code or schema change. Enum candidates need an enforced set, while LowCardinality candidates need room to grow without a migration.

Next, identify monotonically increasing timestamps, amounts, and sequence IDs for CODEC(Delta, ZSTD), evaluating LZ4 against ZSTD for hot columns using actual query patterns. This pairs compression with the values’ structure without treating decompression latency as free.

Compose the ORDER BY key from low-cardinality, frequently filtered columns. The key should build on the earlier choice of types to allow granule skipping and make index comparisons cheaper.

Set age-based tiering to move data past the hot window into object storage with high-level ZSTD, and check that compute can read it without touching the query path’s local cache.

Audit whether the resulting table can serve the API or dashboard fast enough, removing the serving copy and its maintenance pipeline if it can.

Before migration work begins, use a free workload assessment to identify which steps offer the most leverage for the specific schema.

More in ClickHouse Table Sizing