Lab 04: Table Partitioning

Time: 45 minutes | Level: Advanced | DB: MySQL 8.0, PostgreSQL 15

Overview

Table partitioning splits large tables into smaller physical segments while maintaining a single logical table view. This enables partition pruning (query only relevant partitions), faster maintenance operations, and better data lifecycle management.


Step 1: MySQL RANGE Partitioning by Year

docker run -d --name mysql-lab -e MYSQL_ROOT_PASSWORD=rootpass mysql:8.0
for i in $(seq 1 30); do docker exec mysql-lab mysql -uroot -prootpass -e "SELECT 1" 2>/dev/null && break || sleep 2; done

docker exec mysql-lab mysql -uroot -prootpass <<'EOF'
CREATE DATABASE partdb;
USE partdb;

-- Range partition by order year
CREATE TABLE orders (
  id         INT NOT NULL AUTO_INCREMENT,
  customer   VARCHAR(100),
  amount     DECIMAL(10,2),
  order_date DATE NOT NULL,
  PRIMARY KEY (id, order_date)   -- partition key must be in PK
) 
PARTITION BY RANGE (YEAR(order_date)) (
  PARTITION p2021 VALUES LESS THAN (2022),
  PARTITION p2022 VALUES LESS THAN (2023),
  PARTITION p2023 VALUES LESS THAN (2024),
  PARTITION p2024 VALUES LESS THAN (2025),
  PARTITION p_future VALUES LESS THAN MAXVALUE
);

-- Verify partition structure
SELECT 
  PARTITION_NAME,
  PARTITION_ORDINAL_POSITION,
  PARTITION_EXPRESSION,
  PARTITION_DESCRIPTION,
  TABLE_ROWS
FROM information_schema.PARTITIONS
WHERE TABLE_NAME = 'orders' AND TABLE_SCHEMA = 'partdb';
EOF

📸 Verified Output:

💡 When partitioning by a function like YEAR(), MySQL uses a generated column internally. The partition key must be part of every unique key (including PRIMARY KEY).


Step 2: Insert Data and Observe Partition Distribution

📸 Verified Output:


Step 3: Partition Pruning with EXPLAIN

📸 Verified Output:

💡 Partition pruning happens when the WHERE clause includes the partition key. This is why choosing the right partition key is critical — it must match your most common query patterns.


Step 4: LIST and HASH Partitioning

📸 Verified Output:


Step 5: Partition Maintenance — ADD, DROP, REORGANIZE

📸 Verified Output:

💡 DROP PARTITION is instant — it removes the underlying file. Much faster than DELETE WHERE year = 2021 on billions of rows. This is the key benefit of time-based partitioning!


Step 6: PostgreSQL Declarative Partitioning

📸 Verified Output:


Step 7: PostgreSQL Partition Pruning with EXPLAIN

📸 Verified Output:


Step 8: Capstone — Hash Partition + Attach/Detach Pattern

📸 Verified Output:


Summary

Partition Type
Use Case
Key Syntax

RANGE

Time-series, date-based data

PARTITION BY RANGE (YEAR(col))

LIST

Categorical data (region, status)

PARTITION BY LIST COLUMNS(col)

HASH

Even distribution, no natural key

PARTITION BY HASH (col) PARTITIONS N

KEY (MySQL)

Like HASH but MySQL manages function

PARTITION BY KEY (col) PARTITIONS N

Pruning

WHERE clause on partition key

EXPLAIN shows partitions: column

DROP PARTITION

Archive old data (instant!)

ALTER TABLE t DROP PARTITION p_old

ATTACH/DETACH

PostgreSQL zero-downtime maintenance

ALTER TABLE t DETACH PARTITION p

Key Takeaways

  • Partition key must match queries — wrong key = full table scan across all partitions

  • DROP PARTITION is instant — drops the data file, no row-by-row delete

  • Pruning requires the key in WHERE — always filter by the partition column

  • PostgreSQL pg_inherits tracks partition hierarchy — query it to see partition tree

  • ATTACH/DETACH enables zero-downtime partition maintenance in PostgreSQL

Last updated