Skip to main content
By default, ClickHouse compresses data with ZSTD(3). Parts of MergeTree tables that are smaller than 100 MiB when written are compressed with the faster LZ4, and merges recompress them with ZSTD(3) once they grow larger (see the compression server setting). For MergeTree-engine family you can change the default compression method in the compression section of a server configuration. You can also define the compression method for each individual column in the CREATE TABLE query.
The Default codec can be specified to reference default compression which may depend on different settings (and properties of data) in runtime. Example: value UInt64 CODEC(Default) — the same as lack of codec specification. See also Adaptive Codec Selection. Also you can remove current CODEC from the column and use default compression from config.xml:
Codecs can be combined in a pipeline, for example, CODEC(Delta, Default). Codec names are case-insensitive, so CODEC(ZSTD), CODEC(zstd) and CODEC(ZStd) all name the same codec.
You can’t decompress ClickHouse database files with external utilities like lz4. Instead, use the special clickhouse-compressor utility.
Compression is supported for the following table engines:
  • MergeTree family. Supports column compression codecs and selecting the default compression method by compression settings.
  • Log family. Uses ZSTD(3) by default and supports column compression codecs.
  • Set. Only supported the default compression.
  • Join. Only supported the default compression.
ClickHouse supports general purpose codecs and specialized codecs.

General Purpose Codecs

NONE

NONE — No compression.

LZ4

LZ4 — Lossless data compression algorithm. Applies LZ4 fast compression. It is used by default for small MergeTree parts.

LZ4HC

LZ4HC[(level)] — LZ4 HC (high compression) algorithm with configurable level. Default level: 9. Setting level <= 0 applies the default level. Possible levels: [1, 12]. Recommended level range: [4, 9].

ZSTD

ZSTD[(level)] — ZSTD compression algorithm with configurable level. Possible levels: [1, 22]. Default level: 1. High compression levels are useful for asymmetric scenarios, like compress once, decompress repeatedly. Higher levels mean better compression and higher CPU usage.

ZXC

ZXC[(level)] — asymmetric zxc compression algorithm with configurable level. Possible levels: [1, 7]. Default level: 3. ZXC is a codec of the same kind as LZ4, made for the compress-once, decompress-many pattern: it spends more CPU time on compression and decompresses faster than LZ4. Higher levels mean better compression and slower compression, while decompression stays fast. For a comparison with LZ4 and ZSTD and for the choice of a level, see Choosing between LZ4, ZXC and ZSTD.
This codec is in beta and requires SET enable_zxc_codec = 1 to use.

GDeflate

GDeflate[(level)] — GDeflate, a variant of Deflate whose bit stream is split into 32 interleaved sub-streams, with configurable level. Possible levels: [1, 12]. Default level: 6. A block is compressed in independent chunks of 64 KiB, each a GDeflate page, the format that nvcomp and DirectStorage expand on a GPU, many chunks in parallel. On the CPU, it compresses and decompresses about as fast as Deflate, slower than LZ4 and ZSTD.
This codec is experimental and requires SET enable_gdeflate_codec = 1 to use.

Obsolete: ZSTD_QAT

Obsolete: DEFLATE_QPL

Choosing between LZ4, ZXC and ZSTD

  • LZ4 compresses and decompresses fast, with a moderate compression ratio. It is used by default for small MergeTree parts.
  • ZSTD (ZSTD(3) is the default codec) is a good combination of compression ratio and speed when the size of the data matters. With ZSTD(1), the data is 33% to 47% smaller than with LZ4, while the hits table below is compressed 1.5 to 1.7 times slower and decompressed about two times slower.
  • ZXC with the default level 3 has a compression ratio close to LZ4: the data is 1% to 22% smaller. It is compressed about three times slower than with LZ4 and decompressed 1.3 to 2.4 times faster, which suits data that is written once and read many times.
These numbers are from the measurements below. They depend on the data, so try a codec on your own tables before switching to it.

Detailed comparison on five tables

The compression ratio of five tables, where every column is compressed with the same codec: The tables are hits of ClickBench (web analytics, 100 million rows, 105 columns), Amazon reviews of 2015 (42 million rows, mostly text), OnTime of 2013–2022 (56 million rows, 109 columns, mostly numbers and LowCardinality strings), NOAA (1.07 billion rows of weather measurements) and 10 million Bluesky events of JSONBench in a JSON column. Compression speed on the hits table, in MB of uncompressed data per second on one CPU core: Decompression speed on the hits table, in the same units: On the other four tables, ZXC(3) decompresses 1.3 to 2.1 times faster than LZ4 on x86-64 and 1.5 to 2.4 times faster on ARM.
The effect on the hits table as a whole, relative to the same table with LZ4, as the range over the four machines. INSERT is INSERT ... SELECT of the whole table in 16 threads, merges are the merges of the parts it wrote (not measured for every level), the scan reads all the columns in one thread, and the time of the ClickBench queries is the sum over the 43 queries with a warm page cache:
The levels of ZXC form three groups:
  • Level 1 compresses at 0.7 to 2.4 of the speed of LZ4, and level 2 at 0.6 to 0.9 of it, but the data is larger than with LZ4: by 7% to 42% with level 1 and by up to 13% with level 2. They are useful only for the decompression speed.
  • Levels 3, 4 and 5 compress better than LZ4, typically at a third, a quarter and a seventh of its speed. With LZ4HC, the data is 4% to 12% smaller than with level 5, but it is compressed 1.5 to 7 times slower.
  • Levels 6 and 7 add entropy coding. Level 6 compresses better than LZ4HC, and level 7 about as well as ZSTD(1), while both decompress 2 to 4 times faster than ZSTD(1). Their compression is the slowest among these codecs: 6 to 50 MB/s per CPU core, which is slower than LZ4HC and 20 to 90 times slower than ZSTD(1).
Take into account:
  • Decompression is only a part of the work of a query. A query that reads all the columns of hits in one thread uses 11% to 18% less CPU time with ZXC(3) than with LZ4, and the 43 queries of ClickBench take 1% to 5% less time. When all the CPU cores read all the columns at once, the difference shrinks: between 9% less and 3% more CPU time than with LZ4.
  • ZXC has a fixed cost per compressed block: about 35 bytes, one to three microseconds on compression and about half a microsecond on decompression. It is negligible for the blocks of 64 KiB to 1 MiB that MergeTree writes for large parts. For blocks below 16 KiB, ZXC has no advantage over LZ4, and blocks below 1 KiB are compressed worse and several times slower than with LZ4. Such blocks appear in small parts and in the sparse subcolumns of JSON.
  • The Bluesky table is the worst case among the five: more than half of it are identifiers and content hashes, and ZXC(3) compresses it five to seven times slower than LZ4, to the same size. On the hashes, which are a third of the table and are written in base32, ZXC(3) is six to nine times slower than LZ4.
  • The numbers were obtained with ClickHouse 26.12 and zxc 1.0.1 on AWS m8i, m8a, m8g and r7g instances, on the blocks of data as MergeTree compresses them. They depend on the data, so try the codec on your own tables before switching to it.

Specialized Codecs

These codecs are designed to make compression more effective by exploiting specific features of the data. Some of these codecs do not compress data themselves, they instead preprocess the data such that a second compression stage using a general-purpose codec can achieve a higher data compression rate.

Delta

Delta(delta_bytes) — Compression approach in which raw values are replaced by the difference of two neighboring values, except for the first value that stays unchanged. delta_bytes is the maximum size of raw values, the default value is sizeof(type). Specifying delta_bytes as an argument is deprecated and support will be removed in a future release. Applicable to any fixed-size data type of 1, 2, 4, 8, 16 or 32 bytes. This includes the wide types such as UInt128, Int256 and Decimal256, and also UUID, IPv6 and FixedString of such a size. Delta is a data preparation codec, i.e. it cannot be used stand-alone.

DoubleDelta

DoubleDelta(bytes_size) — Calculates delta of deltas and writes it in compact binary form. The bytes_size has a similar meaning than delta_bytes in Delta codec. Specifying bytes_size as an argument is deprecated and support will be removed in a future release. Optimal compression rates are achieved for monotonic sequences with a constant stride, such as time series data. Can be used with any numeric type. Implements the algorithm used in Gorilla TSDB, extending it to support 64-bit types. Uses 1 extra bit for 32-bit deltas: 5-bit prefixes instead of 4-bit prefixes. For additional information, see Compressing Time Stamps in Gorilla: A Fast, Scalable, In-Memory Time Series Database.

GCD

GCD() - - Calculates the greatest common denominator (GCD) of the values in the column, then divides each value by the GCD. Can be used with integer, decimal and date/time columns. The codec is well suited for columns with values that change (increase or decrease) in multiples of the GCD, e.g. 24, 28, 16, 24, 8, 24 (GCD = 4). GCD is a data preparation codec, i.e. it cannot be used stand-alone.

Gorilla

Gorilla(bytes_size) — Calculates XOR between current and previous floating point value and writes it in compact binary form. The smaller the difference between consecutive values is, i.e. the slower the values of the series changes, the better the compression rate. Implements the algorithm used in Gorilla TSDB, extending it to support 64-bit types. Possible bytes_size values: 1, 2, 4, 8, the default value is sizeof(type) if equal to 1, 2, 4, or 8. In all other cases, it’s 1. For additional information, see section 4.1 in Gorilla: A Fast, Scalable, In-Memory Time Series Database.

ALP

ALP(variant) — Adaptive lossless compression for floating-point data. Supports Float32 and Float64. For details, see ALP: Adaptive lossless floating-point compression.
In most scenarios, ALP is the best codec ClickHouse offers for Float32 and Float64 columns.
The codec accepts an optional variant argument:
  • ALP() or ALP(AUTO) (default) — Uses STD and falls back to RD based on the estimated compressed size.
  • ALP(STD) — Standard ALP variant. Represents each value as an exact scaled integer using decimal powers, then compresses the resulting integers with Frame-of-Reference and bit-packing. Non-representable values are stored as raw exceptions. Works best for numbers originating from decimals (e.g., measurements, prices).
  • ALP(RD) — Real Doubles variant. Reinterprets each value’s bit pattern and splits it into a high part (sign + exponent + top mantissa bits) and a low part. High parts are dictionary-encoded (up to 8 entries), low parts are bit-packed. Works best when many values share the same high bits.

PCO

PCO(level) — Lossless compression for sequences of fixed-width numbers, using the pcodec library. Applicable to all numeric types of 1, 2, 4 or 8 bytes (Int8..Int64, UInt8..UInt64, Float32, Float64, and the types backed by them such as Date, DateTime, Decimal32, Decimal64, IPv4, and Enum). Decimal128/Decimal256 and non-numeric types are not supported. pcodec identifies an approximate structure of the numbers (a “mode”, e.g. an approximate common multiplier) and splits each value into “latents” that it then compresses with delta encoding and an interleaved tANS entropy coder. On numeric columns with smooth, multimodal, or high-entropy distributions (measurements, timings, counters, identifiers) it often compresses better than Gorilla/FPC/ZSTD/ALP; it has no run-length or match-based path, so constant or low-cardinality columns compress worse. Encoding is several times slower than ZSTD; decoding is workload-dependent. The optional level argument sets the compression level from 0 to 12; the default is 8. When a block would not compress (e.g. incompressible data), it is stored uncompressed, so the codec never expands data by more than a small constant per block. The payload of each compression block is a standalone .pco stream, compatible with the reference pcodec implementation.
This codec is in beta and requires SET enable_pco_codec = 1 to use. Because it needs the column type, it can only be specified per column; it cannot be used in the untyped compression settings (marks_compression_codec, primary_key_compression_codec, default_compression_codec) or in TTL ... RECOMPRESS, except as an alternative of FirstApplicable, e.g. FirstApplicable(PCO, ZSTD(3)).

Wallaby

Wallaby — Lossless compression for Float32 and Float64 columns that combines the integerization of floating-point values with entropy coding. For every chunk of up to 16384 values, the codec turns the values into integers in the cheapest of several ways: as numerators of a common denominator (decimals such as 12.34, but also other grids, such as averages of decimals over 60 samples), as multiples of a constant (decimals produced by a multiplication, such as x * 0.01, which are not correctly rounded decimals), or as their bit patterns with the common trailing zeros removed. The integers are then predicted from the previous ones (differences of order up to 7, so smooth and monotonic series cost little), and the residuals are coded with an entropy coder (tANS) whose alphabet is fitted to the chunk: frequent exact values get dictionary symbols, the others fall into logarithmic or fixed-width bins, and runs of repeated values are single symbols. Values that do not fit the chosen representation (NaN, infinities, a minority of other precision) are stored separately, so the codec is lossless for every bit pattern. Constant chunks collapse to a single value, and incompressible data is stored as is with one byte of overhead per block. Compared with ALP, which bit-packs the integers, Wallaby is slower but compresses better, most notably on data with a skewed or clustered distribution, repeated values, or a smooth trend. Compared with PCO, it finds the integer representation more reliably on blocks of a few thousand values and describes its entropy model more compactly.
This codec is experimental and requires SET enable_wallaby_codec = 1 to use.

FPC

FPC(level, float_size) - Repeatedly predicts the next floating point value in the sequence using the better of two predictors, then XORs the actual with the predicted value, and leading-zero compresses the result. Similar to Gorilla, this is efficient when storing a series of floating point values that change slowly. For 64-bit values (double), FPC is faster than Gorilla, for 32-bit values your mileage may vary. Possible level values: 1-28, the default value is 12. Possible float_size values: 4, 8, the default value is sizeof(type) if type is Float. In all other cases, it’s 4. For a detailed description of the algorithm see High Throughput Compression of Double-Precision Floating-Point Data.

SZ3

SZ3 or SZ3(algorithm, error_bound_mode, error_bound) - A lossy but error-bound codec (SZ3 Lossy Compressor) for columns of type Float32, Float64, Array(Float32), or Array(Float64). For array columns, compression is most effective when all arrays have the same length (they are then compressed as fixed-width vectors); arrays of different lengths are still supported and are compressed as a flat sequence of values. The codec is not applicable to Map columns, because its keys would be corrupted by lossy compression. Supported values for ‘algorithm’ are ALGO_LORENZO_REG, ALGO_INTERP_LORENZO and ALGO_INTERP. Supported values for ‘error_bound_mode’ are ABS, REL, PSNR and ABS_AND_REL. Argument ‘error_bound’ is the maximum error and of type Float64. The codec is also not applicable to LowCardinality columns of MergeTree tables (also inside Array, Tuple or Nested), because their dictionaries would be corrupted.
This codec is experimental and requires SET enable_sz3_codec = 1 to use.

T64

T64 — Compression approach that crops unused high bits of values in integer data types (including Enum, Date and DateTime). At each step of its algorithm, codec takes a block of 64 values, puts them into 64x64 bit matrix, transposes it, crops the unused bits of values and returns the rest as a sequence. Unused bits are the bits, that do not differ between maximum and minimum values in the whole data part for which the compression is used. DoubleDelta and Gorilla codecs are used in Gorilla TSDB as the components of its compressing algorithm. Gorilla approach is effective in scenarios when there is a sequence of slowly changing values with their timestamps. Timestamps are effectively compressed by the DoubleDelta codec, and values are effectively compressed by the Gorilla codec. For example, to get an effectively stored table, you can create it in the following configuration:

Quantized

Quantized(method, dimensions[, ...]) — A specialized codec to support approximate vector search on columns of type Array(Float32), Array(Float64), Array(BFloat16), Array(Int8) or Array(UInt8). It stores the original, full-precision vectors, as well as a compact quantized code per vector alongside. On MergeTree-family tables, vector search queries with setting vector_search_use_quantized_codes will scan the quantized codes to build a shortlist and subsequently rescore the results against the full-precision vectors. This two-stage search reads fewer bytes than a normal full-precision scan at the cost of lower recall. dimensions is the vector length; supported method values are rabitq, turboquant, int8, prefix and product, each a different size / accuracy / distance-function trade-off. For Array(Int8) and Array(UInt8) columns, the int8 method and the prefix method with format int8 are not supported. The codec can only be set in CREATE TABLE, it cannot be added, removed, or changed through ALTER TABLE, including with ADD COLUMN ... CODEC(Quantized(...)). It cannot be chained with any other codec (not even an encryption codec such as AES_128_GCM_SIV). For more details, see Vector search with quantized codecs.
This codec is experimental and requires SET enable_quantized_codec = 1 to use.

Encryption Codecs

These codecs don’t actually compress data, but instead encrypt data on disk. These are only available when an encryption key is specified by encryption settings. Note that encryption only makes sense at the end of codec pipelines, because encrypted data usually can’t be compressed in any meaningful way. Encryption codecs:

AES_128_GCM_SIV

CODEC('AES-128-GCM-SIV') — Encrypts data with AES-128 in RFC 8452 GCM-SIV mode.

AES-256-GCM-SIV

CODEC('AES-256-GCM-SIV') — Encrypts data with AES-256 in GCM-SIV mode. These codecs use a fixed nonce and encryption is therefore deterministic. This makes it compatible with deduplicating engines such as ReplicatedMergeTree but has a weakness: when the same data block is encrypted twice, the resulting ciphertext will be exactly the same so an adversary who can read the disk can see this equivalence (although only the equivalence, without getting its content).
Most engines including the “*MergeTree” family create index files on disk without applying codecs. This means plaintext will appear on disk if an encrypted column is indexed.
If you perform a SELECT query mentioning a specific value in an encrypted column (such as in its WHERE clause), the value may appear in system.query_log. You may want to disable the logging.
Example
If compression needs to be applied, it must be explicitly specified. Otherwise, only encryption will be applied to data.
Example

Selecting a Codec by Data Type

FirstApplicable(codec1, codec2, ...) — Uses the first codec in the list that is applicable to the data type. A specialized codec is applicable only to some data types, e.g. PCO to numbers and ALP to floating-point numbers, while a general purpose codec such as ZSTD is applicable to any data type, so it is a natural last alternative:
Here id is compressed with PCO and name with ZSTD(3). The codec is selected for every stream of a column separately, so the x element of point is compressed with PCO and the label element with ZSTD(3). FirstApplicable only selects a codec: it has no header of its own in the compressed data, which is written by the selected codec alone. It is an element of a codec pipeline, so it can be combined with other codecs, as in CODEC(Delta, FirstApplicable(PCO, ZSTD)). An alternative can be a pipeline of several codecs, written as Multiple(codec1, codec2, ...), which is applicable if every codec in it is:
The alternative Default is the default codec, and an alternative can also be another FirstApplicable. FirstApplicable is accepted wherever a codec is, including the settings where the data type is not known: default_compression_codec, marks_compression_codec, primary_key_compression_codec and TTL ... RECOMPRESS CODEC(...). For the default codec of a MergeTree table, the codec is selected for the data type of every column that uses the default codec:
When the data type is not known, as for marks, the first alternative that is applicable to any data type is used, so the list must contain one. If no alternative is applicable to the data type of a column, the query fails.

Adaptive Codec Selection

The specialized codecs above can shrink the right data dramatically, but choosing them takes expertise, and no single choice fits a column whose data changes over time. With the MergeTree setting enable_adaptive_codec_selection enabled, ClickHouse chooses for you. For columns that use the default codec (CODEC(Default) or no CODEC at all), the codec that compresses a block smallest is chosen among the table’s default codec, NONE, and specialized codecs suited to the column type, each on its own and, when the default codec is a general-purpose compression such as LZ4 or ZSTD, followed by it. The choice is made on one block in every adaptive_codec_selection_period blocks and reused for the blocks in between.
Specialized codecs are currently chosen for integers up to 64 bits, enums, dates and times, Decimal32/Decimal64, IPv4, and Float32/Float64. Other columns select between the default codec and NONE for their values.
A block on which the choice is made is never larger than the default codec would make it, and incompressible data is stored raw (compressing it would produce a slightly larger file that is slower to read). The work happens in the background, on merges and mutations, where the data is recompressed anyway. Insert speed is unaffected. Queries often get faster, as a smaller block is less to fetch and less to decompress. Each block records the codec it was written with, so reading requires no setting, and the feature can be switched off at any time with all data remaining readable.
You can observe how it works with the mergeTreeCodecBlockCounts table function. Here time grows steadily, so T64, which stores only the bits that vary within a block, followed by LZ4 (the default for a part this small) squeezing the regular pattern those bits form, beat the default alone on every block. user_id holds hashes that no codec can shrink, so its blocks were stored raw: