Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

ClickHouse 23 min read How-to mediumAdept

Optimizing ClickHouse with Schemas and Codecs

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.

Assumes: ClickHouse MergeTree basicscolumnar storageSQL

Related posts

  • 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.

  • 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.

  • Top 10 best practices tips for ClickHouse

    ClickHouse mediumAdept

    Ten practices for getting performance and storage efficiency out of ClickHouse, each measured on a 150M-row Amazon reviews dataset. Aligning ORDER BY with query filters (low-cardinality columns first) made one query 3x faster and cut scanned data 347x. Right-sized types, no Nullable and LowCardinality(String) doubled query speed and shrank storage. Partitioning is best avoided except for TTL expiry or FINAL on merge engines; monthly partitions slowed one aggregation 46x. The post also covers lightweight versus heavyweight skipping indexes, JSON type hints (38% less storage, 26% faster), and preferring Parquet/ORC for bulk ingestion.

  • AI doesn’t always generate perfect ClickHouse schemas (yet)

    ClickHouse mediumAdept

    LLM-generated ClickHouse schemas tend to over-optimize from day one with custom partitioning, per-column codecs, projections and materialized views that can hurt at production scale. Five recurring pitfalls are covered: partitioning for query speed instead of data lifecycle, OPTIMIZE TABLE ... FINAL creating irreversible oversized parts that break later mutations, materialized view sprawl slowing ingestion, JSON columns misconfigured via max_dynamic_paths, and projections adding 1-2 seconds of planning latency at 5-10+ TB. The recommendation is to start with a plain MergeTree, a well-chosen ORDER BY, default compression and nothing else, then measure the real workload and add complexity only where data justifies it.

  • 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.

The quest for software engineering knowledge.

Look:
Layout: