Skip to the content.

Partitioning

Types of Partitioning

  1. Horizontal Partitioning (most common)

    • Rows are divided across partitions.
    • Example: A Users table partitioned by region β†’ Asia users in one partition, Europe users in another.
  2. Vertical Partitioning

    • Columns are divided.
    • Example: Put frequently accessed columns in one table, and rarely accessed (but large) columns in another.
  3. Range, List, Hash Partitioning

    • Based on value ranges, lists, or hashing of a key.

πŸ‘‰ Example (Partitioning a Transactions table by year):

Here, all partitions are inside one database server.


Sharding

How it works:

πŸ‘‰ Example (Sharding a Users table by user_id):

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 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:

A partitioned table:


βœ… 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:

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')
);
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)
);

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;

4️⃣ Key Points


⚑ 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