Module 3 · Database designLesson 27 of 886 minIntermediate
Partitioning large tables
Split one huge table into smaller pieces inside the database, so queries only touch the pieces they need.
Try it
With "By range", watch the newest partition take most of the traffic. Switch to "hash mod N" and see the load even out.
Partitioning splits one big table into smaller physical pieces, called partitions, while it still looks like one table to your queries. It's closely related to sharding; the difference is mostly where the pieces live:
- Partitioning: pieces usually live in the same database server.
- Sharding: pieces live on different servers.
Ways to partition
- Range: by date or id range.
orders_2026_01,orders_2026_02... - List: by a set of values. Europe, US, Asia.
- Hash: by
hash(id) mod N, for an even spread.
Why it helps
- Partition pruning: a query for March only reads March's partition. Smaller indexes, less to scan.
- Easy deletes: dropping last year's partition is instant, versus deleting millions of rows.
- Maintenance: rebuild or back up one partition at a time.
Watch out for
- Hot partitions: with date ranges, today's partition gets all the new writes and most reads. That's fine inside one server, but it's exactly the problem to avoid when you shard across servers.
- Queries without the partition key must check every partition, which can be slower than one table.
- Too many partitions (thousands of tiny ones) add planning overhead.
Horizontal vs vertical
Everything above is horizontal partitioning: splitting rows. Vertical partitioning splits columns: keep the small, frequently read columns in one table, and big rarely-read ones (like a profile photo or long description) in another.