Data modeling courseLesson 12 of 15
Data modeling course · Lesson 12 of 15
Partitioning, Clustering and Data Layout
How partitioning and clustering let warehouses and lakehouses skip data, how to choose keys, and why too many small partitions or files make queries slower.
On this page
Analytical queries are fast when the engine can skip most of the data. A query that reads 2 % of a table finishes sooner, and on platforms that charge per scan or per second it also costs less, whatever the hardware. Partitioning and clustering are the two ways to arrange data so that skipping is possible. Compaction keeps that arrangement efficient as new data keeps arriving.
This lesson explains how each works, demonstrates both on PostgreSQL and on Parquet files with DuckDB, shows how the main platforms implement them, and ends with a decision table.
Partitioning
Partitioning splits a table into separate physical groups by the value of one column (or an expression of it), typically a date. On a data lake each partition is a folder:
events/
event_date=2026-10-01/ part-0001.parquet ...
event_date=2026-10-02/ part-0001.parquet ...
In a database each partition is a separate child table. Either way, a query with WHERE event_date = '2026-10-02' reads only that partition. This is partition pruning, and it works from metadata alone: the engine knows what each partition can contain without opening it.
Partitioning also makes maintenance simple. Reprocessing a day means replacing one partition; retention means dropping old partitions instead of running a large DELETE.
Partitioning in PostgreSQL
PostgreSQL’s declarative partitioning shows the mechanics clearly. One parent table, one child per day, and a default partition for anything outside the ranges:
CREATE TABLE events (
event_id BIGINT NOT NULL, event_date DATE NOT NULL, customer_id INT NOT NULL,
event_type TEXT NOT NULL, amount NUMERIC(10,2)
) PARTITION BY RANGE (event_date);
CREATE TABLE events_2026_10_01 PARTITION OF events FOR VALUES FROM ('2026-10-01') TO ('2026-10-02');
CREATE TABLE events_2026_10_02 PARTITION OF events FOR VALUES FROM ('2026-10-02') TO ('2026-10-03');
CREATE TABLE events_2026_10_03 PARTITION OF events FOR VALUES FROM ('2026-10-03') TO ('2026-10-04');
CREATE TABLE events_default PARTITION OF events DEFAULT;
INSERT INTO events
SELECT g, DATE '2026-10-01' + (g % 3), (g * 7919) % 5000,
CASE WHEN g % 10 = 0 THEN 'purchase' ELSE 'view' END,
CASE WHEN g % 10 = 0 THEN (g % 200) + 0.99 END
FROM generate_series(1, 30000) AS g;
ANALYZE events;
SELECT tableoid::regclass AS partition, count(*) FROM events GROUP BY 1 ORDER BY 1;
Each daily partition receives 10,000 rows; rows are routed automatically on insert. Now compare the plans of three queries that ask for the same day:
EXPLAIN (COSTS OFF) SELECT count(*) FROM events WHERE event_date = DATE '2026-10-02';
EXPLAIN (COSTS OFF) SELECT count(*) FROM events
WHERE event_date >= DATE '2026-10-02' AND event_date < DATE '2026-10-03';
EXPLAIN (COSTS OFF) SELECT count(*) FROM events WHERE to_char(event_date, 'YYYY-MM-DD') = '2026-10-02';
The first two plans scan one partition:
Aggregate
-> Seq Scan on events_2026_10_02 events
Filter: (event_date = '2026-10-02'::date)
The third wraps the partition column in a function, so the planner cannot prove which partitions match and scans all four:
Aggregate
-> Append
-> Seq Scan on events_2026_10_01 events_1
-> Seq Scan on events_2026_10_02 events_2
-> Seq Scan on events_2026_10_03 events_3
-> Seq Scan on events_default events_4
That is the most common real-world pruning bug, and it exists in every engine: filter on the raw partition column with a range, not on an expression of it. (Some engines, such as Iceberg with hidden partitioning or BigQuery with partitioning on a timestamp’s date, can prune on certain expressions because the partition is defined as that expression; do not rely on it elsewhere.)
Partition-level maintenance is metadata work, which is why a daily reload can be made idempotent cheaply:
-- Re-run 2 October: replace exactly one partition, whatever happened before
BEGIN;
TRUNCATE events_2026_10_02;
INSERT INTO events
SELECT g, DATE '2026-10-02', (g * 7919) % 5000, 'view', NULL
FROM generate_series(1, 5000) AS g;
COMMIT;
-- Retention: remove the oldest day without a large DELETE
ALTER TABLE events DETACH PARTITION events_2026_10_01;
DROP TABLE events_2026_10_01;
SELECT tableoid::regclass AS partition, count(*) FROM events GROUP BY 1 ORDER BY 1;
Choosing a partition column
- It appears in most filters, usually a date or month, and in your retention and reprocessing logic.
- It has low to moderate cardinality: hundreds or a few thousand values over the table’s life, not millions.
- Each partition holds a substantial amount of data. Partitioning a small table, or partitioning by a high-cardinality column, produces thousands of tiny partitions and files, which is slower than no partitioning: the engine spends its time listing, opening and planning.
- Its values do not change for a row. Updating a row’s partition column means moving it between partitions.
Clustering and file statistics
Partitioning helps one dominant filter. For the next most common filters, such as customer_id = 42, engines rely on statistics per file or block: Parquet stores the minimum and maximum of each column per row group, Delta and Iceberg store min/max per file in their metadata, and Snowflake keeps them per micro-partition. If a block’s range does not include 42, it is skipped. These are sometimes called zone maps or data-skipping statistics.
Statistics only help if similar values are stored together. Clustering reorganises data so they are. The DuckDB example writes the same 300,000 rows twice, once in arrival order and once sorted by customer_id, with 10,000-row row groups, then counts how many row groups could contain customer 42 according to their min/max statistics. It also writes a date-partitioned copy and shows that a date filter reads one partition folder out of three.
import os
import duckdb
con = duckdb.connect()
con.execute("""
CREATE TABLE events AS
SELECT g AS event_id, DATE '2026-10-01' + (g % 3)::INT AS event_date,
(g * 7919) % 5000 AS customer_id, g % 97 AS amount
FROM range(1, 300001) t(g)""")
# Partitioned layout: one folder per event_date
con.execute("COPY events TO 'events_by_date' (FORMAT parquet, PARTITION_BY (event_date), OVERWRITE_OR_IGNORE)")
print(sorted(os.listdir("events_by_date")))
plan = con.execute("""EXPLAIN SELECT count(*)
FROM read_parquet('events_by_date/*/*.parquet', hive_partitioning = true)
WHERE event_date = DATE '2026-10-02'""").fetchall()[0][1]
print(plan)
assert "Scanning Files: 1/3" in plan
# Clustering: same rows, arrival order vs sorted by customer_id
con.execute("COPY events TO 'unsorted.parquet' (FORMAT parquet, ROW_GROUP_SIZE 10000)")
con.execute("COPY (SELECT * FROM events ORDER BY customer_id) TO 'sorted.parquet' (FORMAT parquet, ROW_GROUP_SIZE 10000)")
def candidate_row_groups(path, value):
return con.execute(f"""
SELECT count(*) AS row_groups,
count(*) FILTER (WHERE {value} BETWEEN stats_min_value::BIGINT AND stats_max_value::BIGINT)
FROM parquet_metadata('{path}') WHERE path_in_schema = 'customer_id'""").fetchone()
unsorted, clustered = candidate_row_groups("unsorted.parquet", 42), candidate_row_groups("sorted.parquet", 42)
print("unsorted:", unsorted, "sorted:", clustered)
assert unsorted == (30, 30) and clustered == (30, 1)
In arrival order, every one of the 30 row groups spans almost the whole customer range, so a filter on one customer must read all of them. Sorted, only one row group can contain customer 42 and the other 29 are skipped from their footers alone. The plan for the partitioned copy reports Scanning Files: 1/3 with the date as a file filter.
Sorting a whole table once is easy; keeping it clustered as new data arrives is the hard part, and that is what platform clustering features do:
- Snowflake stores every table in micro-partitions with per-column metadata. Data is naturally clustered by load order (usually time); a clustering key on a large table makes Automatic Clustering re-sort micro-partitions in the background, which costs credits.
- Delta Lake offers Z-ordering (
OPTIMIZE ... ZORDER BY, a multi-column sort applied on demand) and liquid clustering (CLUSTER BY), which clusters incrementally, lets you change keys later and is the usual default for new tables. Liquid clustering replaces partitioning and Z-order on a table; it is not combined with them. See OPTIMIZE, Z-ORDER, VACUUM and liquid clustering. - Apache Iceberg uses hidden partitioning with transforms such as
days(event_ts)orbucket(16, customer_id), so queries filter on the real column and the table maps it to partitions; partition specs can evolve without rewriting old data. Sort orders cluster data within files. - BigQuery combines partitioned tables (by ingestion time, a date or timestamp column, or an integer range) with clustered tables on up to four columns, re-clustered automatically.
- Amazon Redshift uses sort keys for zone-map skipping and distribution styles to place rows across nodes for joins.
- PostgreSQL has no automatic clustering; B-tree indexes (and
BRINindexes on naturally ordered columns) do the equivalent job for selective lookups.
The names differ; the idea is the same: make each block’s min/max range narrow on the columns people filter by.
The small-files problem
Every file has overhead: listing it, opening it, reading its footer and metadata, scheduling a task for it. Thousands of small files, produced by frequent appends, streaming micro-batches or over-partitioning, can make reads slower than one large file holding the same rows. They also bloat table-format metadata.
- Compact regularly:
OPTIMIZEin Delta Lake,rewrite_data_filesin Iceberg, or the platform’s automatic compaction. - Control output in Spark jobs: repartition by the partition column before writing so each partition gets a few large files rather than one file per task (see Writing efficient output).
- Aim for files in the hundreds of megabytes (up to around 1 GB) for large analytical tables on object storage. Managed platforms often tune this automatically.
Decision table
| Situation | Layout choice | Why |
|---|---|---|
| Large fact or event table, most queries filter on a date range, retention by date | Partition by date (or month for smaller volumes), or a clustering key that starts with the date | Pruning for the dominant filter and cheap partition-level reloads and deletes |
| The same table is also filtered by customer or product | Add clustering, Z-order, liquid clustering or a sort key on those columns | Partitioning by a high-cardinality column would create millions of tiny partitions |
| New Delta table on a recent runtime | Liquid clustering on the main filter columns; no partitioning | Incremental, keys can change, avoids small-partition problems |
| Table under about 1 GB, or rarely filtered | Nothing; maybe one sort on load | Layout features cost compute and complexity that small tables do not repay |
| Streaming or frequently appended table | Partition or cluster by time, plus scheduled or automatic compaction | Micro-batches produce small files |
| Many different, unpredictable filters | Clustering on the top one or two; consider search optimisation or indexes for point lookups | No layout helps every filter equally |
| Tables joined on a key across nodes (Redshift, some Spark designs) | Distribution or bucketing on the join key | Avoids reshuffling large tables for every join |
Trade-offs
- Pruning vs file count. Finer partitions prune more precisely but create more, smaller files. Choose the granularity that leaves each partition large.
- Write cost vs read cost. Clustering and compaction rewrite data. On a table that is read rarely, the rewrite can cost more than it saves.
- Fixed vs evolving keys. Classic partitioning is hard to change once a table is large; liquid clustering, Iceberg partition evolution and warehouse clustering keys can change with less pain.
- Concurrency. Partition boundaries let jobs that write different dates proceed without conflicting; clustered tables rely on the table format’s conflict detection instead.
Common mistakes
- Partitioning by a high-cardinality column (user id, timestamp to the second).
- Filtering on an expression of the partition column (
to_char(event_date, ...),CAST(ts AS DATE)on a timestamp partition), which prevents pruning in many engines. - Never compacting a streaming or frequently appended table.
- Choosing keys without looking at actual query patterns; check the query history first.
- Adding clustering to small tables, then paying for background reclustering that buys nothing.
- Forgetting that statistics are collected only on some columns (for example the first 32 columns by default in Delta), so a filter column outside them is never skipped.
- Running a large
DELETEfor retention when dropping a partition would be metadata-only.
In an interview
Expect “How would you lay out a large events table?” and “What is partition pruning?”. A strong answer starts from the query pattern (“most queries filter on a date range and some on customer”). It picks partitioning or clustering for each filter with a reason (cardinality, data per partition) and mentions file sizes and compaction. It also says how you would verify it in the plan: EXPLAIN, Spark’s PartitionFilters, or Snowflake’s partitions scanned versus total in the Query Profile. Follow-ups include “what if the filter is on a column you did not partition by?” (statistics and clustering) and “what happens when a streaming job writes every minute?” (small files and compaction). Practise with How do you partition a large table? and When to use clustering keys in Snowflake.
Key takeaway
Partition by the most common low-cardinality filter, cluster by the next most common filters, keep files large enough to read efficiently, filter on raw columns so pruning works, and check the plan to prove data is being skipped.
Progress is saved in this browser only. No account needed.

