Table Partitioning & Sharding

Table Partitioning & Sharding in MySQL: When an individual database table scales beyond tens of millions of rows (such as historical audit logs, telemetry…

When an individual database table scales beyond tens of millions of rows (such as historical audit logs, telemetry events, or financial transactions), even B-Tree indexes struggle to fit into server RAM.

Partitioning and Sharding are two distinct architectural strategies for breaking giant data sets into manageable, high-performance segments.


1. Table Partitioning vs. Database Sharding

StrategyArchitectureWhere Data LivesComplexity
PartitioningSingle database server; table is split into multiple physical sub-files.One server / disk array.Managed automatically by the MySQL engine.
ShardingMultiple completely independent database servers (nodes).Distributed across separate server instances worldwide.Managed by application logic or middleware (Vitess, ProxySQL).

2. MySQL Partitioning Methods

A. RANGE Partitioning (The Time-Series Standard)

Divides records based on column values falling within specific continuous ranges. This is the gold standard for date-based logs and orders:

CREATE TABLE historical_orders (
  id BIGINT NOT NULL,
  user_id INT NOT NULL,
  amount DECIMAL(10,2) NOT NULL,
  order_date DATE NOT NULL,
  PRIMARY KEY (id, order_date) -- Partition column MUST be part of the Primary Key!
)
PARTITION BY RANGE (YEAR(order_date)) (
  PARTITION p_2022 VALUES LESS THAN (2023),
  PARTITION p_2023 VALUES LESS THAN (2024),
  PARTITION p_2024 VALUES LESS THAN (2025),
  PARTITION p_future VALUES LESS THAN MAXVALUE
);

B. Partition Pruning (The Huge Performance Win)

When you query a partitioned table, MySQL's query optimizer evaluates the WHERE clause and physically skips scanning partitions that cannot possibly contain matching data:

EXPLAIN SELECT * FROM historical_orders 
WHERE order_date BETWEEN '2024-01-01' AND '2024-06-30';
-- EXPLAIN Output: partitions = "p_2024"
-- Partitions p_2022, p_2023, and p_future are 100% ignored! Disk I/O drops by 75%!

C. Instant Data Retention Purging

In standard tables, running DELETE FROM logs WHERE created_at < '2022-01-01' on 100 million rows generates billions of undo log bytes, locks tables, and can take 8 hours. With partitioning, you purge entire years of data instantly in milliseconds:

-- Instantly frees disk space and removes 50 million rows with zero transaction overhead!
ALTER TABLE historical_orders DROP PARTITION p_2022;

D. LIST & HASH Partitioning

  • LIST: Partitions based on discrete categorical lists (e.g. PARTITION p_us VALUES IN ('US', 'CA'), PARTITION p_eu VALUES IN ('UK', 'DE', 'FR')).
  • HASH: Automatically distributes rows evenly across N partitions based on an integer hash (e.g. PARTITION BY HASH(user_id) PARTITIONS 8;).

3. Database Sharding: Scaling Beyond a Single Machine

When a company's data volume outgrows even the largest physical server available (thousands of terabytes), engineers turn to Sharding.

In a sharded architecture:

  • Users 1 to 10,000,000 live on Database Node 1 (in Virginia).
  • Users 10,000,001 to 20,000,000 live on Database Node 2 (in Oregon).
  • Users 20,000,001 to 30,000,000 live on Database Node 3 (in Frankfurt).

The Sharding Key

The Sharding Key determines which server stores a given record (usually user_id or tenant_id).

  • The Challenge: Cross-shard joins (JOIN between a table on Node 1 and a table on Node 2) are impossible at the database layer and must be orchestrated in application memory.
  • Enterprise Solutions: Tools like Vitess (created by YouTube to scale MySQL to billions of users) handle sharding transparently while preserving SQL query semantics.