Skip to content

CONCEPT Cited by 1 source

ClickHouse MergeTree partition by time

Definition

The canonical idiom for ingesting time-series data into a ClickHouse MergeTree table: declare PARTITION BY toYYYYMM(timestamp) (monthly) or PARTITION BY toDate(timestamp) (daily) on the MergeTree() engine so that ClickHouse stores rows from the same time bucket together on disk. Partitions are both the unit of fast pruning at query time (WHERE timestamp BETWEEN ... skips whole partitions) and the unit of archival / eviction via DETACH PARTITION or TTL.

Canonical schema from the 2025-12-09 Redpanda IoT pipeline post:

CREATE TABLE telemetry_events (
    timestamp DateTime,
    device_id String,
    value Float64
) ENGINE = MergeTree()
PARTITION BY toYYYYMM(timestamp);

Verbatim framing: "PARTITION BY toYYYYMM(timestamp) or toDate(timestamp) will allow you to segment data monthly or daily. The above code creates a table named telemetry_events that groups its rows into monthly partitions like 202501 or 202502." (Source: sources/2025-12-09-redpanda-streaming-iot-and-event-data-into-snowflake-and-clickhouse)

Why time-partition

  1. Partition pruning — queries with a predicate on the partition key read only matching partitions. Month-level retention + hot-cold query patterns benefit most.
  2. Archival granularity — whole partitions can be detached ("ALTER TABLE telemetry_events DETACH PARTITION '202501'") and moved to external storage (S3, NAS) while the active dataset stays queryable.
  3. TTL ergonomicsMODIFY TTL timestamp + INTERVAL 12 MONTH DELETE drops whole partitions rather than doing expensive row-level deletes on a non-partitioned table.
  4. Compaction locality — the MergeTree background merge process works within a partition; smaller partitions = faster merges but more metadata overhead.

Choosing granularity

  • Monthly (toYYYYMM) — smaller partition count, larger partitions. Ideal when retention is measured in months-to- years and the WHERE predicates naturally span months. Marketing analytics, billing rollups.
  • Daily (toDate) — more partitions, smaller ones. Ideal when retention is measured in days-to-weeks, dashboards filter on "last 24 hours", or archival cadence is daily. Logs, alerts, high-frequency telemetry.
  • Hourly (toStartOfHour) — rarely used; overhead is usually too high.

Composes with

Caveats

  • Cardinality of partition key matters. Too many small partitions (e.g. toStartOfMinute on high-volume ingest) explodes metadata and slows background merges.
  • Partition key must be a deterministic function of row columns. You cannot re-partition without rewriting the table.
  • Detach is a metadata operation, not a copy. Detached partitions live in the detached/ directory on the same storage; to actually move them to cold storage you need a separate copy step (S3 CLI, rsync) or an external-storage volume binding.

Seen in

Last updated · 470 distilled / 1,213 read