Database partitioning strategies
Splitting a huge table into smaller pieces so queries read less data and maintenance gets easier. Range, list, hash and vertical partitioning, how partition pruning works, and how partitioning relates to sharding.
In one sentence
Partitioning divides one large table into smaller pieces by a rule (by date, by region, by hash, or by columns), so each query only reads the pieces that can contain its answer.
Why partition a table
- Faster queries: with *partition pruning*, a query for March only reads the March partition.
- Easier maintenance: deleting a year of old data is dropping a partition, not a giant
DELETE. - Smaller indexes per partition, which fit better in memory.
- A step towards scaling out: partitions can later live on different machines (that's sharding).
Pick a strategy and a query below and see how many partitions the database has to read.
Partition 1
Jan
Partition 2
Feb
Partition 3
Mar
Partition 4
Apr
🔥 hot: all new orders land here
The strategies
| Strategy | Rule | Best for | Watch out |
|---|---|---|---|
| Range | created_at by month or year | Time-series data, logs, orders, archiving old data | The newest partition gets all the writes (hot partition) |
| List | region IN ('EU'), tenant = 'acme' | Data naturally grouped by category, region or customer | Uneven if one value is much bigger than the rest |
| Hash | hash(id) % N | Spreading writes evenly when there is no natural range | Range queries have to read every partition |
| Composite | Range by month, then hash by user inside | Very large tables with two main access patterns | More complexity to manage |
| Vertical | Move columns to another table | Wide tables with a few hot columns and big, rarely-read ones (blobs, JSON, text) | Joins when you need both parts |
CREATE TABLE orders (
id bigint, user_id bigint, total numeric, created_at timestamptz
) PARTITION BY RANGE (created_at);
CREATE TABLE orders_2026_03 PARTITION OF orders
FOR VALUES FROM ('2026-03-01') TO ('2026-04-01');Choosing the partition key
The partition key should match the filter in your most important queries. If almost every query says WHERE created_at > now() - interval '7 days', range by date is perfect. If they all say WHERE customer_id = ?, list or hash by customer is better.
Partitioning vs sharding
| Partitioning | Sharding | |
|---|---|---|
| Where the pieces live | Same database server | Different servers |
| Who routes queries | The database, transparently | Your app or a routing layer (or a distributed DB) |
| Solves | Big tables, slow scans, data retention | One machine can't handle the data or the writes |
| Complexity | Low to medium | High |
Sharding is horizontal partitioning across machines. Many teams partition first, and shard only when a single server isn't enough.
Key takeaways
- 01Partitioning splits a big table into pieces by a rule, inside one database.
- 02Range for time-based data, list for categories, hash for even spread, vertical for wide tables.
- 03Partition pruning only helps queries that filter by the partition key.
- 04Sharding is partitioning across servers: more scale, much more complexity.
Check your understanding
1 of 3
What is table partitioning?
Related concepts
Sharding
Sharding splits one big database into several independent databases (shards), each holding a slice of the rows, so storage and writes scale with the number of machines.
8 min read →Query optimization & indexes
An index is a sorted copy of one or more columns that lets the database jump to the rows it needs instead of reading the whole table.
9 min read →Read replicas
A read replica is a copy of the database that receives every change from the primary and answers read queries, so you can add read capacity without touching writes.
7 min read →