---
title: "Speeding up LZ4 in ClickHouse"
description: "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."
url: "https://swe.quest/articles/speeding-up-lz4-in-clickhouse-dbbd26281ee7c0df"
canonical: "https://clickhouse.com/blog/lz4-compression-in-clickhouse"
source: "ClickHouse"
published: "2023-04-12"
difficulty: "hard"
kind: "Deep dive"
reading_minutes: 32
tags: ["performance","storage-engines","c-cpp","algorithms","analytics"]
---

# Speeding up LZ4 in ClickHouse

A post on [ClickHouse](https://swe.quest/sources/clickhouse.md), published 2023-04-12, 32 min read, Deep dive. Original: [https://clickhouse.com/blog/lz4-compression-in-clickhouse](https://clickhouse.com/blog/lz4-compression-in-clickhouse)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Hard: written for specialists in the field

## Topics

- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)
- [Storage engines](https://swe.quest/tags/storage-engines.md) (Data & databases)
- [C & C++](https://swe.quest/tags/c-cpp.md) (Languages)
- [Algorithms](https://swe.quest/tags/algorithms.md) (Architecture & design)
- [Analytics & OLAP](https://swe.quest/tags/analytics.md) (Data & databases)

## Prerequisites

- C++ memory operations ([easy posts on C & C++](https://swe.quest/tags/c-cpp.md?difficulty=easy))
- CPU caches and SIMD basics ([easy posts on Hardware](https://swe.quest/tags/hardware.md?difficulty=easy))
- compression basics

## Keywords

LZ4, ClickHouse, LZ77, Zstandard, memcpy, LZ4\_decompress\_fast

## Related posts

- [Five Methods For Database Obfuscation](https://swe.quest/articles/five-methods-for-database-obfuscation-477824be1d934bc1.md): ClickHouse · 2023-02-16 · Hard · 23 min read · Analytics & OLAP, Privacy, Performance engineering, Testing, Algorithms
  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.
- [Optimizing ClickHouse with Schemas and Codecs](https://swe.quest/articles/optimizing-clickhouse-with-schemas-and-codecs-795411feff5942b1.md): ClickHouse · 2022-12-14 · Medium · 23 min read · Analytics & OLAP, Storage engines, Performance engineering, SQL
  Tightening a ClickHouse schema with strict types cuts a 1B-row NOAA weather dataset from 131.58 GiB to 71.38 GiB uncompressed.
- [ClickHouse Release 23.9](https://swe.quest/articles/clickhouse-release-23-9-73e8083c92226065.md): ClickHouse · 2023-10-24 · Medium · 12 min read · Analytics & OLAP, Databases, Storage engines, Identity & auth
  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.
- [ClickHouse Release 24.6](https://swe.quest/articles/clickhouse-release-24-6-003c18b45227c882.md): ClickHouse · 2024-07-11 · Medium · 15 min read · Analytics & OLAP, Storage engines, Python, Performance engineering
  ClickHouse 24.6 adds an optimize\_row\_order setting for plain MergeTree tables.
- [Compressing nginx logs 170x with column storage](https://swe.quest/articles/compressing-nginx-logs-170x-with-column-storage-f7055f56d674ca49.md): ClickHouse · 2025-10-23 · Medium · 14 min read · Analytics & OLAP, Observability, Storage engines, Cost optimization
  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.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
