Speeding up LZ4 in ClickHouse
LZ4 decompression often tops ClickHouse query profiles because highly compressible, cached columnar data shifts the cost onto CPU, yet storing data uncompressed proved impractical due to disk space. LZ4 is an LZ77 variant whose compressed blocks alternate literals and matches within a 64 KB sliding window, and whose compressor finds matches through a hash-based "cache table". Decompression speeds up when generic memcpy is replaced by inlined over-copying routines such as wildCopy8, which may safely read and write extra bytes inside buffer bounds. The fast decoder skips bounds checks on match copies, so untrusted input needs checksums, guard pages or the safe decoder.
Related posts
- Five Methods For Database Obfuscation
ClickHouse hardMaster
ClickHouse needed publishable benchmark data that kept the properties driving analytical query performance in its private web analytics dataset: compression ratio, per-column and cross-column cardinality, and value distributions such as power laws. The post compares approaches to obfuscating that data. Hand-written probabilistic models in C++ were too laborious and specific to one dataset. An LSTM that generated TSV dumps produced gigabyte-sized models, ran at about 100 rows per second and distorted character frequencies. Mutating the literals inside LZ4-compressed data preserved compression ratio and decompression speed while removing every original byte, guided by heuristics that keep structure such as valid UTF-8.
- Optimizing ClickHouse with Schemas and Codecs
ClickHouse mediumAdept
Tightening a ClickHouse schema with strict types cuts a 1B-row NOAA weather dataset from 131.58 GiB to 71.38 GiB uncompressed. The changes are right-sized integers instead of Int64, Int16 elevation, an Enum8 for weather type, LowCardinality for ~100k-value strings, and Float32 lat/lon in place of a Point. Compressed size moves less, from 4.07 GiB to 3.83 GiB, but smaller uncompressed data lowers RAM use and improves CPU cache efficiency at query time. Each step is checked against per-column system.columns stats and a benchmark query, before moving on to tuning column codecs for the largest columns such as date and temperatures.
- ClickHouse Release 23.9
ClickHouse mediumAdept
ClickHouse 23.9 ships 20 new features, 19 performance optimisations and 55 bug fixes, highlighted by JSON schema type inference, a GCD compression codec and SSH key authentication. Type inference derives nested Tuple schemas from well-structured JSON by sampling the first 25k rows or 32MB, but it ignores columns that appear after the sample and rejects inconsistent types for the same path. The GCD codec divides each block's values by their greatest common divisor before Delta/ZSTD. On an 11-billion-row Forex dataset it cut compressed Decimal column size by 38% with oversized precision and about 20% even with optimal precision. Users can now authenticate with SSH keys configured in YAML or via DDL instead of passwords.
- ClickHouse Release 24.6
ClickHouse mediumAdept
ClickHouse 24.6 adds an optimize_row_order setting for plain MergeTree tables. Within each range of identical ORDER BY values, it sorts rows by the remaining columns in ascending cardinality, creating long runs that LZ4 and ZSTD compress better. Loading 1 billion PyPI download rows shows about 30% better compression (25.36 GiB down to 17.52 GiB), at the cost of 30-50% slower inserts. It helps only with no or low-cardinality sort keys. The release also brings chDB 2.0, which queries Pandas DataFrames, Arrow tables and Python dicts in-process, plus Hilbert curve encoding functions that complement Morton encoding for locality-preserving multi-column sort keys.
- Compressing nginx logs 170x with column storage
ClickHouse mediumAdept
Parsing nginx access logs into typed columns in ClickHouse and tuning storage raises compression from roughly 38x (ZSTD on the raw file) to about 178x on a 66.7M-line, 20 GB dataset. Regex extraction into separate columns alone reaches 56x. Choosing tighter types (IPv4, UInt16/UInt32), LowCardinality dictionary encoding for columns under ~100k distinct values, and codecs such as Delta plus ZSTD lifts it to 92x. Picking an ordering key that groups low-cardinality, high-volume columns so similar values sit together pushes it past 170x, though production keys must also balance query access patterns.