SDSystem DesignConcepts to case studies
System DesignDSA
x
0/311
Part 1: Foundations
Part 2: APIs and Communication
Part 3: Caching
Part 4: Data and Storage
  • Indexing
  • Partitioning
  • Replication
  • Sharding
  • Query Optimization
  • Denormalization
  • Materialized Views
  • Connection Pooling
  • Replication Models
  • Replication Lag
  • Quorum (R + W > N)
  • Shard Keys, Hot Shards and Resharding
  • Cross-Shard Joins
  • Backups, RPO, RTO and Disaster Recovery
  • Point-in-Time Recovery
  • Data Archiving and TTL
Part 5: Architecture
Part 6: Distributed Systems
Part 7: Advanced Data Topics
Part 8: Operations and Security
Part 9: Estimation and Interview Method
Part 10: Case Studies
System Design/Data and Storage/Database Scaling

Partitioning

Cut one big table into smaller pieces that are easier to manage.

⏱ 13 min readIntermediateMust KnowInterview FavouriteHas visualizer
SummaryConceptReal-Life ExampleWhy?Where?How It WorksStepsKey TermsPros & ConsMistakesReal WorldRead NextInterview Q&ACheat-Sheet

01Quick Summary

Partitioning splits one big table into smaller pieces called partitions. To your queries it still looks like one table, but the database stores and reads it in parts.

  • The split is decided by a partition key: a date, a customer id, a region.
  • Three main rules: range (date ranges), hash (spread evenly), list (named groups).
  • A query that filters on the key reads only the right partitions. This is partition pruning.
  • It also makes maintenance easier: drop an old month in one step instead of deleting millions of rows.
  • Partitioning is not the same as sharding. Partitions usually live in one database server. Shards live on different servers.

02Concept: What is it?

As a table grows to hundreds of millions of rows, everything gets heavier: indexes are huge, scans take long, backups and vacuum jobs run for hours, and deleting old data blocks the table. Partitioning limits the size of each piece. Each partition is a smaller table with its own, smaller indexes.

There are two directions. Horizontal partitioning splits by rows (this month's orders in one piece, last month's in another). Vertical partitioning splits by columns (keep the small hot columns in one table and the big, rarely used ones, such as a long description or an image, in another). Most people mean horizontal when they say "partitioning".

One logical table, many physical partitions
👤AppSELECT ... FROM orders
→↓one table name
🧭Partition routeruses the key
→↓
Partitions of orders
2026-012M rows
2026-022M rows
2026-032M rows
...
The application sends one query. The database decides which partitions to read.
🧠Partitioning vs sharding
Partitioning divides data inside a database to make it easier to manage and query. Sharding divides data across several database servers to scale out. Sharding is partitioning plus distribution.

03Real-Life Example

🍵The Filing Cabinet With Drawers
An office keeps ten years of invoices. In one giant pile, finding "March 2024" means digging through everything. So the clerk uses a cabinet with one drawer per month. To find March 2024 she opens one drawer. At the end of the year she empties the oldest drawers into storage in a minute, instead of pulling out invoices one by one.
Filing cabinet
Partitioned table
The whole cabinet
The logical table
One drawer
One partition
Label on the drawer (month)
Partition key and range
Going straight to the right drawer
Partition pruning
Searching every drawer for a customer name
Query without the key reads all partitions
Emptying the oldest drawer at year end
Dropping or detaching an old partition
The current-month drawer is always crowded
A hot partition (skew)
Query for March only touches one drawer
❓WHERE month = Mar
→↓prune
Janskipped
Febskipped
Marread
Aprskipped
Read Skipped

04Why Do We Need It?

⚡Read less data
Pruning skips partitions that cannot match, so queries touch a fraction of the table.
🗑️Cheap clean-up
Drop or detach a whole old partition. No slow row-by-row delete.
🧰Easier maintenance
Backup, vacuum, reindex and archive one partition at a time.
🌳Smaller indexes
Each partition has its own index that fits in memory more easily.
💾Storage tiers
Keep recent partitions on fast disks and old ones on cheap storage.
🔀Spread the writes
Hash partitioning spreads inserts and reduces contention on one hot area.
Make each piece small enough to handle, and make most queries need only a few pieces.

05Where Do We Use It?

🧾Time-based data
Orders, logs, events and metrics, split by day or month.
🏢Multi-tenant apps
Split by tenant or customer id.
🌍Regional data
List partitions by country, handy for local rules.
📦Archiving and retention
Keep 13 months online, move older partitions away.
📊Data warehouses
Large fact tables partitioned by date for fast reports.
🧮Big tables with hot columns
Vertical split of rarely read, bulky columns.

06Types / Parts / How It Works

The main partitioning methods

MethodHow rows are placedGood forWatch out
RangeBy a range of the key: dates, ids, numbers.Time data, retention, range queriesThe newest partition is hot. Uneven sizes.
Hashhash(key) mod N chooses the partition.Even spread, many equal lookupsRange queries read all partitions. Changing N moves lots of data.
ListA fixed list of values per partition (EU, US...).Regions, categories, tenantsNew values need a new partition. Sizes can be very uneven.
Composite (sub-partitioning)Two levels, such as range by month then hash by customer.Large tables that need bothMore complex to design and run.
VerticalSplit columns into separate tables.Hot columns vs big cold onesJoins are needed to rebuild a row.
Three ways to cut the same table
Range (month)
Jan-Mar
Apr-Jun
Jul-Sep
Hash (customer)
hash % 3 = 0
hash % 3 = 1
hash % 3 = 2
List (region)
EU
US
APAC
The key and the rule decide what a query can skip.
Range partitioning in PostgreSQL (declarative partitioning)
CREATE TABLE orders (
  id          bigint,
  customer_id bigint,
  order_date  date NOT NULL,
  total       numeric
) PARTITION BY RANGE (order_date);

CREATE TABLE orders_2026_03 PARTITION OF orders
  FOR VALUES FROM ('2026-03-01') TO ('2026-04-01');

-- retention: remove a whole month quickly
ALTER TABLE orders DETACH PARTITION orders_2026_01;
DROP TABLE orders_2026_01;
Range
Good for dates
  • Date queries prune well
  • Easy retention by dropping old ranges
  • Newest partition takes all writes
  • Sizes can be uneven
Hash
Good for spread
  • Even sizes and even writes
  • Equal lookups on the key prune well
  • Range queries read all partitions
  • Re-partitioning moves lots of rows
VisualizerRange vs hash vs list, pruning and skewRun the month query on Range, then on Hash. Then raise the skew slider and watch one partition grow.
Partitioning method
Query

SELECT * FROM orders WHERE order_month = 'Nov'

Each column is a partition, height = rows inside. Bright = read by the query, faint = skipped (pruned).
150
150
150
150
Jan-Mar
Apr-Jun
Jul-Sep
Oct-Dec
Partitions read
1/4
pruned
Rows scanned
150
of 600
Rows returned
50
Skew (max / avg)
1.0x
1.0 = perfectly even
Partition pruning: only 1 of 4 partitions are read (150 of 600 rows), because the filter matches the partition key.

A simplified model: 600 orders with fixed, made-up data (no random numbers), and each partition is scanned without an index. Real databases can also use indexes inside each partition, and the exact pruning rules depend on the database. Try this: a range split makes date queries cheap but sends all new writes to the latest partition; a hash split spreads writes but cannot prune date ranges.

07How to Use It / Steps / Implementation

Choosing a partition key and rule

  1. 1
    Ask what most queries filter on
    The key should appear in the WHERE clause of your main queries. Otherwise pruning does nothing.
  2. 2
    Check data and write spread
    Look for a key that gives pieces of similar size and spreads busy writes. Avoid a key where one value dominates.
  3. 3
    Pick the rule
    Range for time and retention. Hash for even spread. List for fixed categories.
  4. 4
    Size the partitions
    Not too big (slow, hard to maintain) and not too small. Thousands of tiny partitions slow planning.
  5. 5
    Automate creating and dropping
    Create next month’s partition ahead of time and drop old ones on a schedule, with a job or an extension.
  6. 6
    Index each partition properly
    Indexes still matter inside a partition. Primary and unique keys usually must include the partition key.
  7. 7
    Check the plan
    Use EXPLAIN to see that only the expected partitions are scanned.
⚠️Partitioning does not replace indexing
Pruning removes whole partitions, but inside the partitions you still need good indexes. Also, a query that does not filter on the partition key reads every partition, which can be slower than one big table because of the extra overhead.
Rule of thumbpartition when a table is very large and queries naturally cut by one key

08Key Terms to Know

TermMeaning in easy words
PartitionOne piece of a table
Partition keyThe column(s) that decide which partition a row goes to
Horizontal partitioningSplitting a table by rows
Vertical partitioningSplitting a table by columns
Partition pruningSkipping partitions that cannot contain matching rows
Range / hash / listThe three main ways to assign rows to partitions
SkewUneven sizes or load between partitions
Hot partitionA partition that gets much more traffic than the others
Sub-partitioningPartitioning each partition again with another rule
Detach / drop partitionRemove a whole partition in one fast step

09Pros, Cons and Trade-offs

✅ Pros
  • Queries read fewer rows (pruning)
  • Fast retention and archiving
  • Smaller indexes and maintenance jobs
  • Spread writes and reduce contention
  • Can place old data on cheaper storage
⚠️ Cons
  • Queries without the key read every partition
  • The key choice is hard to change later
  • More objects to manage and monitor
  • Unique keys must include the partition key
  • Skew can create hot partitions

Trade-offs

ChoiceWe gainWe lose
Range by timeGreat pruning and retentionNewest partition is the hot one
Hash by idEven spreadNo range pruning, hard to resize
List by regionNatural fit for local rulesUneven sizes, new values need setup
Many small partitionsFine-grained pruningPlanning cost and metadata overhead
Few large partitionsLess overheadLess benefit, still heavy to maintain

10Common Problems and Mistakes

Common mistakes

  1. A key that queries do not use. No pruning, so you pay overhead and gain nothing.
  2. Partitioning too early. A table of a few million rows rarely needs it. Try indexes first.
  3. Forgetting to create future partitions. Inserts fail when no partition fits the date.
  4. Too many partitions. Thousands of tiny ones slow query planning and use memory.
  5. A skewed key. One region or one tenant holds most of the data.
  6. Confusing it with sharding. Partitions in one server do not add servers or capacity by themselves.

Things to watch

🔥Hot partition
Time-range partitioning sends all new writes to the last partition.
🔑Unique constraints
Many databases need the partition key inside each unique index.
🔁Re-partitioning cost
Changing the key or number of hash partitions means moving lots of rows.
🔗Cross-partition queries
Joins, sorts and aggregates across partitions cost more.

11Real-World Examples

🐘PostgreSQL
Declarative range, list and hash partitioning, with automatic pruning at plan and run time.
🐬MySQL
Range, list, hash and key partitioning. Unique keys must contain the partition columns.
🏛️Oracle and SQL Server
Mature partitioning features, often used for large warehouses and archival.
☁️BigQuery and warehouses
Tables partitioned by date so a query pays only for the days it reads.
🧱Kafka and Cassandra
Use the word partition for the unit of distribution, which is closer to sharding.
📝Log and event stores
Daily partitions are dropped on a schedule to enforce retention.
📝Note
These are general ideas. Syntax, limits and meaning of "partition" differ by system, so check the docs.
Mini case: an events table with 13 months online
⏰Nightly job
→↓create
📅events_next
→↓detach oldest
📦events_oldto archive storage
A nightly job creates tomorrow's partition and detaches the one that fell out of the window.

12Related Topics to Read Next

Sharding→

Partitioning across servers to scale out.

Indexing→

Still needed inside every partition.

Query Optimization→

How pruning and plans make queries faster.

Consistent Hashing→

A better way to spread keys when the node count changes.

Time Series Databases→

Time partitioning is built into their design.

Replication→

Copies of partitions or shards for safety and read scale.

13Interview Questions with Short Answers

14Quick Revision Cheat-Sheet

  • ✔What: one logical table stored as many smaller partitions.
  • ✔Key: choose a column your queries filter on.
  • ✔Rules: range (time), hash (even), list (categories).
  • ✔Win: pruning reads fewer partitions; drop old data in one step.
  • ✔Cost: no key in WHERE means a read of all partitions.
  • ✔Watch: skew, hot newest partition, too many partitions.
  • ✔Not sharding: partitions are usually on one server, shards are on many.
  • ✔One line: “Cut the table by the thing you filter on, so most queries read a small piece.”
← Previous
Indexing
Next →
Replication