Partitioning
- Definition: Splitting data of a single database/table into smaller, more manageable parts called partitions.
- Where it applies: Within a single database system.
- Goal: Improve performance, manageability, and query efficiency.
Types of Partitioning
-
Horizontal Partitioning (most common)
- Rows are divided across partitions.
- Example: A
Userstable partitioned byregionβ Asia users in one partition, Europe users in another.
-
Vertical Partitioning
- Columns are divided.
- Example: Put frequently accessed columns in one table, and rarely accessed (but large) columns in another.
-
Range, List, Hash Partitioning
- Based on value ranges, lists, or hashing of a key.
π Example (Partitioning a Transactions table by year):
Transactions_2023Transactions_2024Transactions_2025
Here, all partitions are inside one database server.
Sharding
- Definition: Splitting data across multiple database instances/servers (shards). Each shard holds a subset of data.
- Where it applies: Distributed databases.
- Goal: Scalability (handle huge data and high traffic by adding more servers).
How it works:
- Each shard is like a mini database with its own storage and compute.
- A shard key decides where data goes.
- Requires a shard router/coordinator to direct queries to the right shard.
π Example (Sharding a Users table by user_id):
- Shard 1 β users with IDs 1β1M
- Shard 2 β users with IDs 1M+1β2M
- Shard 3 β users with IDs 2M+1β3M
Here, data is split across different physical servers.
Key Differences
| Aspect | Partitioning | Sharding |
|---|---|---|
| Scope | Within a single database | Across multiple databases/servers |
| Purpose | Manageability + query optimization | Scalability + distribute load |
| Complexity | Handled internally by the DB engine | Needs shard key, router, and cluster setup |
| Performance | Faster queries on subsets of data | Handle very large datasets + high concurrency |
| Failure impact | One DB β risk of single point of failure | Each shard independent β higher availability |
β Simple way to remember:
- Partitioning = cutting your data into slices within one kitchen (DB server).
- Sharding = spreading your data across multiple kitchens (DB servers), each cooking a slice.
Partitioning is not about correctness β itβs about performance, manageability, scalability.
β Why do we partition a table instead of just adding a region column?
Because partitioning changes how the database stores and processes data internally.
A normal table:
- All rows are stored in the same physical space
- Searching/filtering has to scan more data
- Indexes become huge β slower
A partitioned table:
- Data is physically split into smaller chunks called partitions
- Each partition can be scanned, indexed, locked, and maintained independently
β Example: Users partitioned by region
Without partitioning (single table)
Users (20 million rows)
Columns: user_id, name, region, email, ...
A query like:
SELECT * FROM users WHERE region = 'Asia';
The DB:
- Checks the index on
region - Still needs to fetch rows from a huge table
Indexes on 20M rows are big, slow, expensive.
With partitioning (Asia partition, Europe partition)
Query:
SELECT * FROM users WHERE region = 'Asia';
DB skips all other partitions Only scans the Asia partition (maybe 3β4 million rows)
This is called partition pruning.
π Key Advantages of Partitioning
1. Faster queries due to partition pruning
Only relevant partitions are scanned.
2. Smaller indexes
Each partition has its own index β much smaller β faster.
3. Better maintenance
Want to delete all Europe users? Instead of:
DELETE FROM users WHERE region='Europe';
β¦which is slow and causes massive locking and logging,
You can do:
ALTER TABLE users DROP PARTITION europe;
Instant. No huge delete cost.
4. Better Data Distribution & Parallelism
Databases run partitions in parallel, boosting performance.
5. High availability / easier backups
You can backup/restore only one partition.
6. Avoiding βhotspotβ problems
If 90% users are from Asia, you can distribute read/write load better.
βοΈThen why not partition everything?
Partitioning is useful only for very large tables (millions of rows).
For small tables: partitioning = unnecessary overhead.
π₯ Summary (super simple)
| Without Partitioning | With Partitioning |
|---|---|
| One big table | Table split physically |
| Slow queries on large datasets | Faster due to partition pruning |
| Huge indexes | Small partition-specific indexes |
| Hard to delete large data sets | Drop partition instantly |
| More locking | Less locking |
π― Final Answer
Yes, you can have a single table with a region column.
But partitioning is used because:
π Queries get faster π Indexes get smaller π Maintenance becomes easier π Data is parallelized and distributed π Deletes / archiving become instant
Partitioning is a performance + scalability optimization, not a correctness requirement.
β How to create partitions
It depends on the type of database (Oracle, MySQL, PostgreSQL, etc.) but the concept is the same.
1οΈβ£ Range Partitioning
You divide data into ranges (like dates, numeric values, or region codes).
Example: Users table partitioned by region in MySQL:
CREATE TABLE users (
user_id INT,
name VARCHAR(100),
region VARCHAR(50)
)
PARTITION BY LIST COLUMNS(region) (
PARTITION p_asia VALUES IN ('Asia'),
PARTITION p_europe VALUES IN ('Europe'),
PARTITION p_america VALUES IN ('America')
);
- One table
users - Database internally stores 3 partitions:
p_asia,p_europe,p_america - Query like:
SELECT * FROM users WHERE region='Asia';
β DB scans only p_asia (partition pruning)
2οΈβ£ Range Partitioning by Date
Sometimes you partition by dates (common for logs, transactions):
CREATE TABLE orders (
order_id INT,
order_date DATE,
amount DECIMAL(10,2)
)
PARTITION BY RANGE (YEAR(order_date)) (
PARTITION p2023 VALUES LESS THAN (2024),
PARTITION p2024 VALUES LESS THAN (2025)
);
- Old partitions can be archived or dropped easily.
3οΈβ£ Hash Partitioning
Used when data is uniformly distributed but you donβt have natural ranges:
CREATE TABLE users (
user_id INT,
name VARCHAR(100)
)
PARTITION BY HASH(user_id)
PARTITIONS 4;
- Database distributes rows across 4 partitions automatically
- Good for load balancing
4οΈβ£ Key Points
- You donβt create multiple tables manually.
- Partitioning is part of table definition.
- Queries still see one logical table.
- DB handles storage, pruning, and indexing internally.
β‘ Advantages over separate tables
| Separate tables | Partitioned table |
|---|---|
| Manual joins if you need all regions | Single table query works naturally |
| Hard to manage schema changes | Schema change applied to all partitions at once |
| Hard to maintain indexes | Each partition has its own smaller index |
| Manual archiving | Can drop/archive partitions easily |