System Design Academy

Saving…

Lesson 8 of 10 in Module 30 of 54 lessons complete

Partitioning

Module 3, lesson 8Concept11 min

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.

Daily pieces remain parts of one logical table.

The bag_events table branches into three daily partitions for November 20, 21, and 22.

PostgreSQL: the lower date is included and the upper date is excluded.
SQL
-- 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');
MethodSplits byUseful whenExample
HorizontalRowsQueries target one date rangeBag scans by day
VerticalColumnsCommon reads need only small fieldsPass 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.

The filter lets PostgreSQL skip partitions outside today’s bounds.
SQL
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.

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

You must delete bag events older than 90 days every night from a huge table. What helps most?
Moving a bulky, rarely used photo column into its own table is…

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.

Try the quick check above whenever you feel ready.

Previous: Replication