Sub topic 5
Partitioning and when it helps or hurts
This chapter assumes you know basic SQL, can run commands in a Databricks notebook or SQL warehouse, and understand that a Delta table is Parquet files plus a transaction log. It also assumes you know that per-file statistics enable data skipping.
What partitioning does
Hive-style partitioning splits a table into directories, one for each distinct value of the partition column. Every data file belongs to exactly one partition. The partition value is recorded in the transaction log, so the engine can skip whole partitions without opening any files.
CREATE TABLE events_partitioned (
event_id BIGINT,
user_id BIGINT,
event_date DATE,
payload STRING
)
PARTITIONED BY (event_date);A query such as WHERE event_date = '2024-03-01' reads only that day's files. This is partition pruning. It is the cheapest form of skipping, because it needs no file statistics.
When partitioning helps
Partitioning pays off when all of these hold:
- Most queries filter on the partition column.
- The column has low cardinality, meaning few distinct values. Dates are a common choice.
- Each partition holds a large amount of data. A common guideline is at least 1 GB per partition.
- You manage data by partition, for example by deleting or reprocessing one day at a time.
You can also limit maintenance to part of the table. OPTIMIZE accepts a WHERE clause on partition columns:
OPTIMIZE events_partitioned
WHERE event_date >= '2024-01-01';When partitioning hurts
Partitioning goes wrong in predictable ways.
| Problem | Cause | Effect |
|---|---|---|
| Over-partitioning | High-cardinality column such as `user_id` | Millions of directories, each with tiny files |
| Small partitions | Little data per value | Small-files problem inside every partition |
| Skewed partitions | A few values hold most rows | Uneven task sizes and slow jobs |
| Unfiltered column | Queries rarely use the partition column | No pruning, but all the overhead |
| Fixed choice | Changing the column means rewriting the table | Costly to correct a wrong guess |
Partitioning multiplies the small-files problem. If you partition by date and write hourly, each partition can collect many small files. OPTIMIZE has to compact each partition separately, so a file can never be merged with data from another partition.
A quick check
Before choosing a partition column, count how many distinct values it has and how much data each value would hold:
SELECT event_date, COUNT(*) AS row_count
FROM events
GROUP BY event_date
ORDER BY row_count DESC
LIMIT 20;If the counts are small or very uneven, partitioning will probably not help. Use DESCRIBE DETAIL to see the table's size and file count. Dividing sizeInBytes by numFiles gives the average file size.
Common errors
- Partitioning by default. Small and medium tables rarely benefit. Databricks generally recommends skipping partitions for tables under about 1 TB.
- Partitioning on a timestamp. A timestamp has nearly one value per row. Use a derived date column instead.
- Expecting
OPTIMIZEto fix a bad partition scheme. Compaction works inside partitions, so it cannot merge files across them. - Filtering on a function of the column. Wrapping the partition column in an expression may prevent pruning. Compare the column directly to a literal where possible.
Exercise
A table is partitioned by user_id. It has 5 million distinct users, and queries usually filter on event_date. Name two problems with this design and suggest a better layout.
Show answer
Two problems: (1) 5 million partitions means millions of directories with tiny files, which is the small-files problem at scale, and (2) queries filter on event_date, not user_id, so partition pruning never applies. A better layout is to partition by event_date (low cardinality, matches the filter), or to avoid partitioning and use Z-ordering or liquid clustering on event_date and user_id.