shuffleet
Home
Practice
Problems
Lessons
Plans
Compete
Challenges
Interviews
Leaderboard
Progress
Skills
Analytics
Submissions
Account
Settings

Partitioning and pruning

Intermediate · 4 min read

In two sentences

Partitioning stores a table in separate folders by the value of a column, such as one folder per day. A query that filters on that column only opens the folders it needs; skipping the rest is called pruning, and it's the difference between reading 1 GB and 1 TB.

See it

s3://lake/page_views/
  view_date=2026-04-01/  part-0.parquet  part-1.parquet
  view_date=2026-04-02/  part-0.parquet
  view_date=2026-04-03/  part-0.parquet  part-1.parquet
queryfolders read
WHERE view_date = DATE '2026-04-02'1
WHERE view_date >= DATE '2026-04-02'2
WHERE country = 'FR'all 3 (not the partition column)

A filter on the partition column opens one folder and skips the rest; partitioning by user creates thousands of tiny folders

In SQL

Iceberg in Trino, partitioned by the day of a timestamp:

SQL
CREATE TABLE page_views (
  view_ts timestamp(6), user_id varchar, url varchar, country varchar
)
WITH (partitioning = ARRAY['day(view_ts)']);

This is Iceberg's hidden partitioning: you filter on view_ts itself and Iceberg prunes by day for you. No separate view_date column to keep in sync.

The planner reads Iceberg metadata, which lists each file with its day, and opens only the files for 04-02

SQL
SELECT count(*) FROM page_views
WHERE view_ts >= TIMESTAMP '2026-04-02 00:00' AND view_ts < TIMESTAMP '2026-04-03 00:00';

See what's actually on disk:

SQL
SELECT * FROM "page_views$partitions";   -- one row per partition: record and file counts
SELECT file_path, record_count, file_size_in_bytes FROM "page_views$files";

Choosing the column

Partition by what queries filter on most, at a grain that keeps each partition reasonably large.

choiceresult
day(view_ts) for dashboards filtered by date✓
user_id (millions of values)millions of tiny files: slow to list and to read
hour(view_ts) for 10 MB a daythousands of tiny partitions; use day or month
a high-cardinality key you must filter onbucket(16, user_id): a fixed number of groups

When to use it

Partition when both are true: the table is large (tens of GB and growing), and most queries filter on one column, usually time. If either is false, don't.

situationapproach
events, logs, facts queried by date rangeday(ts) (or month(ts) when a day is small)
also filtered by country, customer, status…partition by time, sort by the second column (sorted_by)
filtered by a high-cardinality key (user, device)bucket(n, key), not the raw key
a dimension or a table under a few GBno partitioning
queries changed and the layout no longer fitsIceberg partition evolution: new data uses the new spec, no rewrite

Best practices

  • Design from the queries, not the data. List the five most common filters before choosing.
  • Aim for files in the hundreds of MB. Thousands of KB-sized files are slower than one unpartitioned table.
  • Compact on a schedule when something writes often (streaming, frequent MERGE): in Trino, ALTER TABLE page_views EXECUTE optimize.
  • Prefer Iceberg's hidden partitioning to a hand-maintained event_date column; filters on the real column prune, and nobody can write a mismatched value.
  • Check that it prunes. Compare the data read in the query stats with and without the filter.

Common mistakes

  • Filtering on a function of a Hive-style partition column. With a view_date column, WHERE year(view_date) = 2026 may not prune. Filter on the column directly, with a range.
  • Too fine a grain. Small files are the most common lakehouse performance problem. Each file has overhead on listing, opening and planning.
  • Losing the layout on export. Writing the result of a query without the partitioning drops it; the next reader scans everything.
  • Partitioning a small table. Below a few GB, one well-sorted set of files is usually faster.

What an interviewer asks next

  • "Queries filter by date and by country. How do you lay it out?" Partition by date; sort or cluster by country inside the partitions.
  • "A streaming job writes a file every minute. What happens after a month?" Small files. Compact them on a schedule.

Go further

Iceberg's partitioning page explains hidden partitioning and why it removes a whole class of mistakes. Trino's Iceberg page lists the transforms (year, month, day, hour, bucket, truncate) and the metadata tables.

Practise it

Lay Out Page Views by Day in S3, then Compact a Micro-Batched Table Full of Small Files, then Choose a Partition Spec for How the Table Is Actually Queried.