For why ClickHouse compresses data so well, we recommended reading this article. In short, our column-oriented database writes values in column-order. When these values are sorted, identical values are located adjacently to each other and compression algorithms exploit contiguous patterns in data. On top of this, ClickHouse has codecs and granular data types which allow you to easily tune compression further.Compression in ClickHouse will be impacted by 3 principal factors:
- The ordering key
- The data types
- Which codecs are used
Choose the right data type to optimize compression
Let’s use the Stack Overflow dataset as an example. Let’s compare compression statistics for the following schemas for theposts table:
posts- A non type optimized schema with no ordering key.posts_v3- A type optimized schema with the appropriate type and bit size for each column with ordering key(PostTypeId, toDate(CreationDate), CommentCount).
posts with no ordering key.
A note on compact versus wide parts
A note on compact versus wide parts
If you’re seeing
compressed_size or uncompressed_size values equal to 0, this could be because the type of the
parts are compact and not wide (see description for part_type in system.parts).
The part format is controlled by settings min_bytes_for_wide_part
and min_rows_for_wide_part meaning that if the inserted
data results in a part which doesn’t exceed the values of the aforementioned settings, the part will be compact rather
than wide and you won’t see the values for compressed_size or uncompressed_size.To demonstrate:Query
Response
The above query relies on the table columns in the system database. This database is managed by ClickHouse and is a treasure trove of useful information, from query performance metrics to background cluster logs. We recommend “System Tables and a Window into the Internals of ClickHouse” and accompanying articles[1][2] for the curious reader.
To summarize the total size of the table, we can simplify the above query:
posts_v3, the table with an optimized type and ordering key, we can see a significant reduction in uncompressed and compressed sizes.
Body, Title, Tags and CreationDate columns achieved by ordering the data prior to compression and using the appropriate types.
Choosing the right column compression codec
With column compression codecs, we can change the algorithm (and its settings) used to encode and compress each column. Encodings and compression work slightly differently with the same objective: to reduce our data size. Encodings apply a mapping to our data, transforming the values based on a function by exploiting properties of the data type. Conversely, compression uses a generic algorithm to compress data at a byte level. Typically, encodings are applied first before compression is used. Since different encodings and compression algorithms are effective on different value distributions, we must understand our data. ClickHouse supports a large number of codecs and compression algorithms. The following are some recommendations in order of importance:
See here for further options.
Below we specify the
Delta codec for the Id, ViewCount and AnswerCount, hypothesizing these will be linearly correlated with the ordering key and thus should benefit from Delta encoding.
Compression in ClickHouse Cloud
In ClickHouse Cloud, we utilize theZSTD compression algorithm (with a default value of 1) by default. While compression speeds can vary for this algorithm, depending on the compression level (higher = slower), it has the advantage of being consistently fast on decompression (around 20% variance) and also benefiting from the ability to be parallelized. Our historical tests also suggest that this algorithm is often sufficiently effective and can even outperform LZ4 combined with a codec. It is effective on most data types and information distributions, and is thus a sensible general-purpose default and why our initial earlier compression is already excellent even without optimization.