Partition Pruning Mechanics & Horizontal Scaling
Partition Pruning Mechanics & Horizontal Scaling
The ultimate performance benefit of table partitioning is Partition Pruning.
Partition pruning occurs when the MySQL Query Optimizer analyzes the query's WHERE clause and determines that only a specific subset of partitions could possibly contain matching rows. The optimizer skips (prunes) all other partitions entirely, reading only 10% or 5% of the total disk data!
Verifying Partition Pruning with EXPLAIN
To verify whether your query is pruning partitions, run EXPLAIN (or EXPLAIN PARTITIONS in older versions):
EXPLAIN Output:
| id | select_type | table | partitions | type | rows |
|---|---|---|---|---|---|
| 1 | SIMPLE | orders_range | p2025 | range | 15000 |
Notice the partitions column: it displays p2025 only! Partitions p2023, p2024, and p_future were completely ignored! Disk I/O was reduced by 75%.
The Pruning Disasters: How Queries Break Pruning
Just like index SARGability, poorly formed SQL queries can prevent the optimizer from pruning partitions, forcing it to scan every partition in the table:
Disaster 1: Non-Matching Partition Expression
If your table is partitioned by YEAR(order_date), and your query filters by:
Disaster 2: Querying on Non-Partitioning Columns
If you query by customer ID without mentioning order_date:
Because MySQL has no way of knowing which year customer 101 placed their order in, it must scan every single partition!
Horizontal Scaling & Table Maintenance Architecture
Partitioning provides substantial benefits for massive data warehouses:
- 1Parallel Maintenance: You can rebuild, analyze, or optimize individual partitions without locking the rest of the table:
ALTER TABLE orders_range OPTIMIZE PARTITION p2025;
- 1Buffer Pool Efficiency: Hot working sets (e.g., current year data in
p2026) fit neatly into RAM buffer pool memory, while cold historical partitions stay on disk. - 2Partition-Level Index Trees: Instead of a single massive 50GB B+Tree with 5 levels of depth, each partition maintains its own smaller 5GB B+Tree with 3 levels of depth, accelerating index traversals!
Multiple Choice Questions
1. What is "Partition Pruning" in MySQL?
A. Deleting corrupted records automatically B. The query optimizer eliminating non-relevant partitions from disk scanning based on query WHERE conditions C. Renaming partitions D. Archiving tables to zip files Answer: B Explanation: Partition pruning skips partitions that cannot possibly contain matching rows based on the query's filter conditions.
2. Which column in EXPLAIN reveals which partitions MySQL actually scanned for the query?
A. type B. key C. partitions D. Extra Answer: C Explanation: The partitions column in EXPLAIN lists the specific partitions evaluated by the query execution plan.
3. What happens if a query against a partitioned table omits the partitioning column from its WHERE filter?
A. The query fails with an error B. MySQL must scan every single partition in the table, eliminating all pruning advantages C. The query returns random rows D. It only checks partition 1 Answer: B Explanation: Without criteria on the partitioning key, MySQL has no basis to exclude partitions and must evaluate all of them.
4. How does partitioning keep individual B+Tree indexes smaller and faster?
A. It compresses strings with gzip B. Indexes in MySQL partitioned tables are local to each partition, meaning each partition has its own smaller, shallower B+Tree C. It deletes primary keys D. It replaces B-Trees with flat lists Answer: B Explanation: MySQL partitions maintain local indexes per partition, resulting in shallower B+Trees with fewer page hops per search.
5. Can you optimize a single specific partition without running OPTIMIZE TABLE on the entire multi-terabyte dataset?
A. No, tables can only be optimized as a whole B. Yes, using ALTER TABLE tbl OPTIMIZE PARTITION p_name; C. Only in MySQL Cluster D. Only on read-only tables Answer: B Explanation: MySQL supports granular maintenance commands at the partition level (e.g., OPTIMIZE PARTITION, CHECK PARTITION).
InnoDB Architecture: Buffer Pool, Redo Log, Undo Log
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Creating & Managing Partitioned Tables | InnoDB Architecture: Buffer Pool, Redo Log, Undo Log |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.