A school office keeps every exam answer sheet from the whole year in one giant pile. When a parent asks to see their child’s March maths paper, someone has to dig through thousands of sheets. Now imagine the same sheets sorted into boxes labelled by month. To find a March paper, you open only the March box. At the end of the year, throwing away last year’s papers means emptying a few boxes, not sorting the pile.
That’s partitioning: splitting one big set of data into smaller, labelled pieces. This guide explains the two ways to split, the common schemes for deciding which piece a record goes in, and how partitioning differs from sharding.
What is partitioning?
Partitioning means dividing a large dataset (like a database table, an index or a stream of messages) into smaller parts called partitions. Each record belongs to exactly one partition, chosen by a rule.
Why bother?
- Faster queries. A query that only needs March reads only the March partition. Skipping the other partitions is called partition pruning.
- Easier maintenance. Deleting a whole year of old data becomes “drop that partition”, which is instant, instead of deleting millions of rows one by one.
- Smaller indexes. Each partition has its own smaller index, which fits in memory more easily.
- Parallel work. Different partitions can be processed at the same time, on different threads or machines.
- A path to scale. Once data is split into partitions, those partitions can be spread across many servers. That’s where sharding comes in.
Two ways to split: by rows or by columns
Horizontal partitioning (by rows). Each partition has the same columns but holds different rows. Orders from 2025 in one partition, 2026 in another. This is the most common meaning of “partitioning”, and it’s the kind sharding uses.
Vertical partitioning (by columns). Each partition holds the same rows but different columns. For example, keep a user’s name, email and city (small, read all the time) in one table, and their profile photo and long bio (large, read rarely) in another, linked by user ID. Everyday queries then read much less data.
How records are assigned: partitioning schemes
For horizontal partitioning, you need a rule that says which partition each row goes to. The rule uses a partition key, a column such as date, customer ID or country.
| Scheme | Rule | Good for | Watch out for |
|---|---|---|---|
| Range | Key falls between two values: Jan–Mar, Apr–Jun… | Time-based data, “last 30 days” queries, dropping old data | Hot partitions: all new writes land in the newest range |
| Hash | A hash of the key decides the partition | Spreading load evenly | Range queries (“all orders this week”) must check every partition |
| List | Key matches a listed value: India, UK, US… | Data naturally grouped by region or category | Uneven sizes if one value is much bigger |
| Composite | Two rules combined, such as range by month, then hash by customer | Big tables that need both | More complexity |
Choosing the key is the most important decision. A good key matches how you query the data (so most queries touch one partition) and spreads load evenly (so one partition isn’t doing all the work). Our guide on rebalancing and hotspots covers what happens when the key is uneven.
Partitioning vs sharding
These two words are often used as if they mean the same thing. The usual distinction:
- Partitioning is the general idea of splitting data into parts. The parts may all live on the same database server. PostgreSQL, MySQL and Oracle all support table partitioning inside one database.
- Sharding is horizontal partitioning where the parts are spread across different servers, each holding only its slice of the data. It’s how you scale beyond what one machine can hold.
So every shard is a partition, but not every partition is a shard. You can start by partitioning a big table on one server for speed and easy cleanup, and later move partitions onto separate servers if one machine isn’t enough. That later step is S · Sharding, and it’s also the Z axis in the scale cube.
When to partition
Partitioning helps when:
- A table is very large (often hundreds of millions of rows or more) and queries usually need a known slice of it, such as recent data.
- You regularly delete or archive old data by time.
- Indexes have grown too big to fit in memory.
It’s probably too early when a table is small, queries already run fast with good indexes, or you’d have to guess at a key without knowing how the data will be queried. Partitioning adds rules every developer must understand, so follow the YAGNI principle and measure first.
Common misconceptions
“Partitioning and sharding are the same.” Sharding is one kind of partitioning: horizontal partitions on separate servers. Partitioning can happen entirely inside one database.
“Partitioning always makes queries faster.” Only queries that filter on the partition key get faster. A query that doesn’t use the key may have to scan every partition and can even get slower.
“Any column works as a partition key.” A poor key creates hot partitions or forces queries to touch every partition. Pick it based on your real queries and load.
Key takeaways
- Partitioning splits a big dataset into smaller partitions, each record in exactly one.
- Horizontal partitioning splits rows; vertical partitioning splits columns.
- Common schemes: range, hash, list and composite, driven by a partition key.
- Partitions can live on one server; sharding spreads them across many servers.
- Choose the key from your real queries and load, and partition when size and access patterns call for it.
Frequently asked questions
What is partitioning in simple words?
Partitioning means splitting a large table or dataset into smaller pieces, each holding part of the data, so queries and maintenance only have to deal with the pieces they need.
What is the difference between horizontal and vertical partitioning?
Horizontal partitioning splits a table by rows, so each partition has the same columns but different records. Vertical partitioning splits it by columns, so each partition has the same records but different fields.
What is the difference between partitioning and sharding?
Partitioning is the general idea of dividing data into parts, which can stay on one server. Sharding is horizontal partitioning across multiple servers, so each server stores only its share of the data.
What is a partition key?
A partition key is the column used to decide which partition a record goes to, such as order date, customer ID or country. A good key matches common queries and spreads data and load evenly.
What is range vs hash partitioning?
Range partitioning puts records into partitions by value ranges, such as dates, which suits time-based queries. Hash partitioning uses a hash of the key to spread records evenly, which balances load but makes range queries touch every partition.
Comments