Table Partitioning Principles: Range, List, Hash, Key
Table Partitioning Principles: Range, List, Hash, Key
When a single table grows beyond tens or hundreds of millions of rows, maintaining B+Tree index depths and performing table maintenance becomes increasingly painful.
Table Partitioning allows a single logical table to be physically split into smaller, independent chunks (partitions) across storage, governed by a Partitioning Key.
The Fundamental Rule of Partitioning in MySQL
order_date if your primary key is simply order_id; the primary key must be composite: PRIMARY KEY (order_id, order_date).The Four Core Partitioning Types
1. RANGE Partitioning
Assigns rows to partitions based on whether the partitioning key value falls within a specified discrete range. Ideal for date-based data archiving:
2. LIST Partitioning
Assigns rows based on matching one of a set of discrete enumerated values. Ideal for geographic segmentation:
3. HASH Partitioning
Distributes rows evenly across a predetermined number of partitions using a user-defined expression:
4. KEY Partitioning
Similar to HASH, but MySQL uses its internal MD5-based hashing algorithm on the columns:
Multiple Choice Questions
1. What is the fundamental requirement regarding Primary Keys when partitioning a table in MySQL?
A. Partitioned tables cannot have primary keys B. The partitioning column MUST be included in all unique keys and primary keys on the table C. Primary keys must be UUIDs D. Primary keys must be AUTO_INCREMENT Answer: B Explanation: MySQL mandates that every unique constraint and primary key must encompass the partitioning column to enforce uniqueness locally.
2. Which partitioning type is most suitable for archiving time-series logs by year or month?
A. KEY Partitioning B. HASH Partitioning C. RANGE Partitioning D. COMPOSITE LIST Answer: C Explanation: RANGE partitioning assigns rows by value thresholds (e.g., VALUES LESS THAN (2025)), making time-based archiving clean and efficient.
3. What does VALUES LESS THAN MAXVALUE accomplish in RANGE partitioning?
A. Throws an error B. Acts as a catch-all bucket for any values exceeding the highest defined threshold C. Limits table size to 4GB D. Closes the partition Answer: B Explanation: MAXVALUE serves as an open-ended catch-all partition preventing insert failures for future values.
4. Which partitioning type distributes rows evenly using an internal MySQL hashing algorithm across a set number of buckets?
A. LIST B. KEY C. RANGE D. INTERVAL Answer: B Explanation: KEY partitioning uses MySQL's internal hashing function to distribute rows evenly across N defined partition buckets.
5. If a table is partitioned into 4 partitions, how does the application query it?
A. By querying the individual partition tables explicitly (SELECT FROM table_p1) B. Transparently querying the logical table name; MySQL routes the query to the proper physical partition C. Using stored procedures only D. Using external sharding proxies Answer: B Explanation:* Partitioning is transparent to client applications; queries address the logical table name and the engine routes requests internally.
Creating & Managing Partitioned Tables
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Advanced Analytical JSON Queries | Creating & Managing Partitioned Tables |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.