MySQL Partitioning in 8.0

Mydbops
Jan 31, 2025
5
Mins to Read
All

Efficient database management is critical for maintaining high performance as data grows. MySQL partitioning helps in distributing data logically, ensuring faster queries and optimized storage. With the introduction of Subpartitioning in MySQL 8.0, data organization can now be even more refined, making large-scale data handling more efficient.

In this blog, we will explore the concept of MySQL partitions, the different types of partitioning strategies available, how to implement partitions, and best practices for using them in real-world applications.

Overview of Partitioning in MySQL

Partitioning is a technique used to split large tables into smaller, more manageable pieces called partitions. Each partition is treated as a separate unit but together forms a complete table. Partitioning improves query performance, maintenance, and overall data management—especially when dealing with very large tables.

Large MySQL Table Partition 1 Partition 2 Partition 3 MySQL Partitioning Overview

There are several partition types in MySQL, each suited to different use cases based on data distribution needs: RANGE, LIST, HASH, KEY, and COLUMN. The choice of partitioning method depends on the specific data characteristics and the performance requirements.

Types of Partitioning in MySQL

RANGE partitioning 

RANGE partitioning in MySQL is a data partitioning technique where a large table is divided into smaller partitions based on a specified range of column values like dates or numeric intervals. Each partition holds data that falls within a specific range, optimizing data handling and query speed. This is particularly useful when you need to group data by time periods or numerical ranges.

MySQL Range Partitioning 2015 2020 2025 2030 Partition p0 Partition p1 Partition p2
CREATE TABLE sales (
 sale_id INT, 
 sale_date DATE, 
 amount DECIMAL(10, 2) 
) 
PARTITION BY RANGE (YEAR(sale_date)) ( 
PARTITION p0 VALUES LESS THAN (2015), 
PARTITION p1 VALUES LESS THAN (2020), 
PARTITION p2 VALUES LESS THAN (2025)
);

HASH partitioning 

HASH partitioning in MySQL divides a table into partitions based on the hash value of a designated column’s contents. Hash partitioning automatically assigns values to partitions based on hashing. This method distributes data evenly across partitions to achieve balanced storage and optimal query performance. This is helpful when there is no natural range to partition by but you need an even distribution of data.

HASH Partitioning Input Data ID: 1 ID: 2 ID: 3 ID: 4 HASH MOD(4) Partition 0 Partition 1 Partition 2 Partition 3
CREATE TABLE users (
 u_id INT, 
 name VARCHAR(100) 
) 
PARTITION BY HASH (u_id) PARTITIONS 4;

LIST partitioning

In MySQL, range partitioning and LIST partitioning are comparable. Each division is explicitly defined, much like in range partitioning. However, in list partitioning, instead of using contiguous ranges of values as in range partitioning, partitions are created and assigned based on predefined value lists for a column. This is useful when data is categorically distributed into specific groups.

LIST Partitioning Customer Data Chennai Mumbai Delhi Chennai Mumbai Partition p0 (Chennai) Partition p1 (Mumbai) Partition p2 (Delhi)
CREATE TABLE customers ( 
 customer_id INT, 
 name VARCHAR(100), 
 city VARCHAR(50)
 ) 
PARTITION BY LIST (region) ( 
PARTITION p0 VALUES IN ('Chennai'),
PARTITION p1 VALUES IN (Mumbai), 
PARTITION p2 VALUES IN ('Delhi') 
);

KEY partitioning

KEY partitioning is similar to HASH partitioning, except that only one or more columns to be evaluated are specified, and the MySQL server provides its own hashing function. It is typically used when the partitioning key is not directly available in the column values but is derived from them. It is useful when the column's hash distribution is important for data management.

KEY Partitioning Input Data id: PRIMARY KEY name: VARCHAR email: VARCHAR date: DATE MySQL Internal Hash Partition 0 Partition 1 Partition 2 Partition 3


CREATE TABLE employee (
 id INT, 
 department TEXT 
) 
PARTITION BY KEY (id) PARTITIONS 4;

What’s New in MySQL 8.0 Partitioning?

MySQL 8.0 introduced the ability to create subpartitions within partitions, allowing for a second level of partitioning. This is an important feature because it provides a way to optimize data management further by allowing you to subdivide partitions in a more granular way.This feature extends MySQL’s partitioning capabilities by adding an extra level of partitioning within each primary partition.

Subpartitioning in MySQL 8.0

Subpartitioning is an additional level of partitioning within each existing partition. The idea is to further subdivide the data in each partition using another partitioning method. This can help optimize query performance and management for very large datasets 

MySQL Partitioning with Subpartitioning Primary Partition (Range) Subpartition 1 Subpartition 2 Subpartition 3
CREATE TABLE sales ( 
id INT NOT NULL, 
sale_date DATE, 
amount DECIMAL(10,2),
PRIMARY KEY (id, sale_date)
 ) 
PARTITION BY RANGE (YEAR(sale_date)) -- Primary partitioning by year SUBPARTITION BY HASH (MONTH(sale_date)) -- Subpartitioning by month
(
PARTITION p0 VALUES LESS THAN (2022), 
PARTITION p1 VALUES LESS THAN (2023), 
PARTITION p2 VALUES LESS THAN (2024) 
);

MySQL Subpartitioning Input Data id: INT date_joined: DATE department: INT RANGE YEAR(date) Partition 2022 Partition 2023 Partition 2024 HASH dept Sub 0 Sub 1 HASH dept Sub 0 Sub 1 HASH dept Sub 0 Sub 1

MySQL 8.0 supports two types of subpartitioning:

  • Range Subpartitioning: Data within each partition is divided into subpartitions based on a range of values.
  • List Subpartitioning: Data within each partition is divided into subpartitions based on a predefined list of values.

Implementation of Subpartitioning

MySQL Table Subpartitioning Structure Primary Partition 1 Subpartition 1.1 Subpartition 1.2 Primary Partition 2 Subpartition 2.1 Subpartition 2.2 Primary Partition Subpartition Data Blocks

Primary Partitioning: First, you define the primary partitioning scheme for a table, which can be done using range, list, hash, or key partitioning.

Subpartitioning: After defining the primary partitioning, you can apply a secondary subpartitioning scheme. This secondary partitioning can use either range or list subpartitioning, but it must be applied to one of the columns used in the primary partitioning.

Example with create statement

CREATE TABLE employee ( 
id INT, 
date_joined DATE, 
department INT 
) 
PARTITION BY RANGE (YEAR(date_joined)) 
SUBPARTITION BY HASH (value)
( 
PARTITION p0 VALUES LESS THAN (2000), 
PARTITION p1 VALUES LESS THAN (2005), 
PARTITION p2 VALUES LESS THAN (2010), 
PARTITION p3 VALUES LESS THAN (2015) 
);

In the example above:

  • The table is partitioned by range on the date_joined column using the YEAR() function.
  • Each partition is then subpartitioned using a hash on the department column.

Benefits of Subpartitioning     

• Improved Performance: Subpartitioning allows for finer control over query optimization, especially for complex queries where specific subpartitions can be targeted, reducing the amount of data scanned.

• Better Data Management: Subpartitioning enables better control over data distribution, making it easier to perform operations like backups or maintenance on specific subpartitions.

• Parallel Query Execution: In some cases, subpartitioning can help MySQL's optimizer parallelize queries more effectively, improving performance for certain types of operations.

With MySQL 8.0, subpartitioning enables more precise data distribution, enhancing large table manageability and performance. You may customize data storage and query to fit your unique workload and needs by combining partitioning and subpartitioning.

Partitioning is an essential technique for managing large datasets and improving the performance of MySQL databases. With the introduction of subpartitioning in MySQL 8.0, you can now refine data distribution even further, ensuring optimal performance and more efficient query execution. By leveraging the different partitioning methods, such as RANGE, LIST, HASH, and KEY, and combining them with subpartitioning, you can tailor your database management strategy to meet the demands of your specific workload.

If you're ready to take your MySQL performance to the next level with expert partitioning and subpartitioning, Mydbops is here to help. Contact us now for MySQL Consulting and Managed Services to ensure your database is running at its best.

No items found.

About the Author

Subscribe Now!

Subscribe here to get exclusive updates on upcoming webinars, meetups, and to receive instant updates on new database technologies.

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.