Partitioning
Definition
Partitioning
- In plain English
- Partitioning means splitting one large table into smaller pieces that the database manages separately.
- Think of it like
- It is like splitting a giant filing cabinet into one drawer per year.
Why it exists
Your bag-event table reaches three billion rows. Most searches need today, but old records make maintenance expensive.
Deleting expired records one by one does unnecessary work. Smaller pieces let you manage whole date ranges together.
How it works
Horizontal partitioning
- In plain English
- Horizontal partitioning splits a table by rows, so each piece holds some of the rows.
- Think of it like
- Like separate drawers for 2024 and 2025 receipts.
Vertical partitioning
- In plain English
- Vertical partitioning splits a table by columns, moving rarely used or bulky columns into their own table.
- Think of it like
- Like keeping photos in a separate album instead of inside every address-book page.
Partition key
- In plain English
- A partition key is the column whose value decides which piece a row belongs to.
- Think of it like
- Like the year written on each receipt.
The bag_events table branches into three daily partitions for November 20, 21, and 22.
-- Illustrative PostgreSQL partitioning syntax.
CREATE TABLE bag_events (
bag_id INTEGER NOT NULL,
scanned_at TIMESTAMP NOT NULL
) PARTITION BY RANGE (scanned_at);
CREATE TABLE bag_events_20261122
PARTITION OF bag_events
FOR VALUES FROM ('2026-11-22') TO ('2026-11-23');| Method | Splits by | Useful when | Example |
|---|---|---|---|
| Horizontal | Rows | Queries target one date range | Bag scans by day |
| Vertical | Columns | Common reads need only small fields | Pass details separate from photos |
This lesson keeps the pieces inside one database server. Sharding distributes pieces across servers.
Examples
Retire old scan days
You keep bag-scan events for ninety days. Queries for today include today’s time boundaries.
SELECT bag_id, scanned_at
FROM bag_events
WHERE scanned_at >= TIMESTAMP '2026-11-22 00:00:00'
AND scanned_at < TIMESTAMP '2026-11-23 00:00:00';Remove an expired daily partition after checking the retention boundary. This avoids deleting its rows individually, but locks and cleanup still take time.
The insight
Time-based partitions make bulk retention work cheaper.
Keep gate reads small
Each pass stores a 2 MB photo needed only at the ticket office. Gates only need pass status and expiry.
Gate readers access small pass fields; the ticket office accesses both pass fields and a separate photo table linked by pass ID.
Separate photos can reduce routine data transfer and storage reads. Measure the benefit because some databases already store large values separately.
The insight
Vertical partitioning separates bulky columns from frequent reads.
Read one race year
Your results table has fifteen years of history. This year’s leaderboard filters on race_year.
SELECT runner_id, finish_seconds
FROM results
WHERE race_year = 2026
ORDER BY finish_seconds
FETCH FIRST 10 ROWS ONLY;The year condition can let the database skip other years. It still needs an efficient way to sort this year’s results.
The insight
A usable partition-key filter helps queries skip irrelevant pieces.
Trade-offs
Pros
- Queries can skip irrelevant pieces.
- Old ranges can be removed together.
- Each piece has smaller local indexes.
Cons
- Queries may still touch every piece.
- Too many pieces add management work.
- An uneven key creates uneven pieces.
Use when
- Large tables have natural date ranges.
- Retention removes complete ranges.
Avoid when
- Modest tables already perform well.
- Queries seldom narrow data by a useful key.
Quick check
Takeaways
- Partitioning makes large tables easier to manage.
- Horizontal partitioning splits rows; vertical partitioning splits columns.
- Usable partition-key filters help queries skip unrelated pieces.