- Introduced by
- Michael Stonebraker and colleagues
- Year
- 1986
- Type
- Database scaling technique
- Family
- Database Scalability & Distribution
What does it do?
Database partitioning divides a large table into smaller segments according to a chosen partitioning key, so that a query can read only the segments it needs.
When the segments are placed on separate servers, the same idea is usually called sharding. The technique belongs to the study of distributed databases, and its origin is discussed under history.
The intuition behind the method
Suppose a table holds years of event records. Finding one day's events by scanning the whole table means reading data that has nothing to do with the query. If the table is split by date, a query for one day reads only that day's partition and skips the others. Partitions can also be placed on different machines, which spreads storage and read and write load across them; in this distributed form the arrangement is called sharding.
History
Ideas about dividing data across machines were developed in distributed database research during the 1980s. The technique later became standard in mainstream database systems, including the declarative partitioning of PostgreSQL and the built-in sharding of many NoSQL systems.
When to use it
Consider partitioning when a table is large enough that queries or maintenance tasks, such as deleting old data or rebuilding indexes, are slowed by its size, and when most queries filter on a column that can serve as the key. The method also fits data that divides naturally, such as records grouped by time or by customer. Do not partition a small table, or one whose queries rarely filter on a single column, because the layout then adds management effort without skipping any data. Choose sharding only when one server cannot hold or serve the data, since cross-shard queries and transactions require coordination that a single-server table does not.
Limitations and considerations
- A query that does not filter on the partitioning key must read every partition, so the layout gives it no benefit and may add overhead.
- A query spanning several partitions or shards must run against each one and combine the results. Across shards, joins and transactions require coordination that a single-server table avoids.
- Changing the partitioning key after data is loaded means redistributing every row, which is costly for a large table.
- With naive modulo hashing across servers, adding a server changes the target for many keys, so a large share of the data must move.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Partition pruning lets the query planner skip every partition whose key range cannot match a query's filter, so a query on one time range reads only the matching partitions.
- Removing old data becomes a metadata operation: detaching or dropping a whole partition avoids deleting rows one at a time.
- Partitions can often be backed up, indexed or compressed separately, so maintenance on one segment does not require handling the entire table at once.
- In sharded deployments, storage and request load are spread across several servers, which a single-node table cannot achieve.
Applications
- Time-series stores that partition measurements by day or hour, so that old data can be removed by dropping whole partitions.
- E-commerce platforms that shard customer data by region or customer ID, so that each server holds a subset of customers.
- Large user-data stores partitioned across servers, where the choice of key determines which users' records are kept together.
- Analytics fact tables partitioned by date, so that historical queries read only the relevant periods and older periods can be archived.
How it works
How it works
Start from the queries that matter most: which columns they filter on and how often. The partitioning key is normally a column that appears in those filters, because a query whose filter does not involve the key cannot skip any partition.
Estimate how rows would fall into partitions for the candidate key, using the actual value distribution where it is available. Uneven distributions are a common problem: a few heavily used customers, or a current date range that receives most new writes, can produce partitions far larger than the rest.
Choose how key values map to partitions. Range partitioning suits ordered keys such as dates. Hash partitioning spreads values evenly but cannot skip partitions for range filters. List partitioning assigns discrete values, such as regions, to specific partitions. Composite schemes combine these. The choice should follow the access pattern identified in the first step.
Create partitions with the database's native partitioning feature where one is available, so that the query planner can skip partitions automatically. Where data must span several servers, route each request to the correct server, either in the application or in a routing layer. This application-level sharding moves coordination across servers into your own code.
Track partition sizes and query times over time. Create new partitions before they are needed, and retire old ones by detaching or dropping them rather than deleting rows individually. Rebalance data when skew develops or servers are added. Operations that touch every partition, such as some schema changes, need separate planning.
Common pitfalls
- Choosing a key that does not appear in the main query filters, so no partitions are skipped and the partitioned layout yields little gain.
- Creating many small partitions, which increases the planning and coordination work done for each query.
- Overlooking skew: a time-based key can concentrate all new writes in the most recent partition, and a customer-based key can leave one partition holding a single very large customer.
- Assuming that uniqueness or foreign-key rules hold across the whole table. Many systems enforce such constraints only within a partition, or only when the partitioning key is part of the constraint, so this must be checked for the specific system.
- Relying on a feature's behaviour without checking it: pruning, constraint enforcement and rebalancing differ between database systems, so confirm each in the documentation of the system in use.
Frequently asked questions
What is the difference between partitioning and sharding?
Partitioning divides a table into independent chunks, possibly stored on the same server. Sharding distributes partitions across different servers. Sharding adds distributed coordination but enables horizontal scaling beyond a single machine.
How do I choose a good partitioning key?
Select a key based on: (1) the filters that most queries apply, (2) a spread of values that keeps partitions similar in size, and (3) a boundary that matches how data is retired or archived. A column with very few distinct values produces a few partitions that are each very large, and a column whose values cluster heavily produces skew.
What happens when I query across multiple partitions?
The database runs the query against each matching partition and combines the results. This is slower than a query that touches a single partition, but it can still be faster than scanning an unpartitioned table, provided the filter lets the database skip the others.
How do I handle growing data in a partition?
For growing tables, use a key that follows incoming data, such as time, and create new partitions ahead of need (for example daily or monthly). Archive or drop old partitions. Some systems can split an overgrown partition; check whether your system supports this before relying on it.
Sources
Stonebraker, M., & Schloss, G. A. (1986). Distributed INGRES to homogeneous and heterogeneous computer systems. Proceedings of the ACM SIGMOD International Conference on Management of Data, 64-77.
Open publication referenceJohnson, B. (2000). Distributed systems and databases (2nd ed.). New York: Morgan Kaufmann.
Open publication referenceGarcia-Molina, H., Ullman, J. D., & Widom, J. (2009). Database Systems: The Complete Book (2nd ed.). Pearson Education.
Open publication reference