Skip to main content
One of the secrets to ClickHouse query performance is compression. Less data on disk means less I/O and faster queries and inserts. The overhead of any compression algorithm with respect to CPU is in most cases outweighed by the reduction in IO. Improving the compression of the data should therefore be the first focus when working on ensuring ClickHouse queries are fast.
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
All of these are configured through the schema.

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 the posts 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).
Using the following queries, we can measure the current compressed and uncompressed size of each column. Let’s examine the size of the initial optimized schema posts with no ordering key.
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
We show both a compressed and uncompressed size here. Both are important. The compressed size equates to what we will need to read off disk - something we want to minimize for query performance (and storage cost). This data will need to be decompressed prior to reading. The size of this uncompressed size will be dependent on the data type used in this case. Minimizing this size will reduce memory overhead of queries and the amount of data which has to be processed by the query, improving utilization of caches and ultimately query times.
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:
Repeating this query for the posts_v3, the table with an optimized type and ordering key, we can see a significant reduction in uncompressed and compressed sizes.
The full column breakdown shows considerable savings for the 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.
The compression improvements for these columns is shown below:

Compression in ClickHouse Cloud

In ClickHouse Cloud, we utilize the ZSTD 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.
Last modified on June 29, 2026