SoFunction
Updated on 2025-03-03

MySQL database partition concept and use

Database partitioning is a technology that splits large tables or indexes into smaller, more manageable parts. Partitioning can improve query performance and management efficiency, especially when processing large amounts of data. The following are the basic concepts, types and specific implementation methods for implementing database partitions.

1. Basic concepts of partitioning

A database partition is the distribution of data from a table into multiple physical storage areas (partitions), which are logically still a table. Each partition can be processed separately, such as performing different backups, indexes, or storage.

2. Partition type

There are mainly the following types of database partitions:

  • Range Partitioning: Partition according to the value range of a certain column. For example, divide data into different partitions based on date range.
  • List Partitioning: Partition according to the discrete values ​​of a column. For example, partition by region or category.
  • Hash Partitioning: Use a hash function to allocate data to multiple partitions, suitable for evenly distributing data.
  • Key Partitioning: Similar to hash partition, but uses MySQL's built-in hash algorithm.

3. Implement partitioning in MySQL

Here are the steps and examples of how to implement partitions in MySQL:

3.1 Create a partition table

Here is an example of creating a scoped partition:

CREATE TABLE sales (
    id INT NOT NULL,
    amount DECIMAL(10, 2) NOT NULL,
    order_date DATE NOT NULL,
    PRIMARY KEY (id, order_date)
) 
PARTITION BY RANGE (YEAR(order_date)) (
    PARTITION p2020 VALUES LESS THAN (2021),
    PARTITION p2021 VALUES LESS THAN (2022),
    PARTITION p2022 VALUES LESS THAN (2023),
    PARTITION pFuture VALUES LESS THAN MAXVALUE
);

3.2 Insert data

When inserting data, MySQL will automatically put the data into the corresponding partition according to partition rules:

INSERT INTO sales (id, amount, order_date) VALUES 
(1, 100.00, '2020-05-01'),
(2, 150.00, '2021-03-15'),
(3, 200.00, '2022-08-20');

3.3 Query data

When querying, you can specify a specific partition through conditions to improve query efficiency:

SELECT * FROM sales WHERE order_date >= '2022-01-01';

3.4 Modify the partition

If you need to add a new partition, you can use the following command:

ALTER TABLE sales 
ADD PARTITION (PARTITION p2023 VALUES LESS THAN (2024));

3.5 View partition information

To view the partition information of a table, you can use:

SELECT 
    PARTITION_NAME, 
    PARTITION_ORDINAL_POSITION, 
    PARTITION_METHOD, 
    TABLE_ROWS 
FROM 
    information_schema.partitions 
WHERE 
    table_name = 'sales';

4. Specific examples

Below are specific examples of range partitions, list partitions, hash partitions, and key partitions for MySQL databases.

4.1 Range Partitioning

Divide the data into different partitions according to the value range of a column. Usually used for date or numeric data.

Example:

CREATE TABLE orders (
    order_id INT NOT NULL,
    order_date DATE NOT NULL,
    amount DECIMAL(10, 2) NOT NULL,
    PRIMARY KEY (order_id, order_date)
) 
PARTITION BY RANGE (YEAR(order_date)) (
    PARTITION p2020 VALUES LESS THAN (2021),
    PARTITION p2021 VALUES LESS THAN (2022),
    PARTITION p2022 VALUES LESS THAN (2023),
    PARTITION pFuture VALUES LESS THAN MAXVALUE
);

In this example,ordersThe table is by yearorder_dateColumns are partitioned in range.

4.2 List Partitioning

The data is divided into different partitions according to the discrete value of a column.

Example:

CREATE TABLE employees (
    emp_id INT NOT NULL,
    emp_name VARCHAR(50) NOT NULL,
    department VARCHAR(20) NOT NULL,
    PRIMARY KEY (emp_id, department)
) 
PARTITION BY LIST (department) (
    PARTITION pSales VALUES IN ('Sales'),
    PARTITION pHR VALUES IN ('HR'),
    PARTITION pIT VALUES IN ('IT'),
    PARTITION pOthers VALUES IN ('Finance', 'Marketing', 'Admin')
);

In this example,employeesTable baseddepartmentThe column values ​​are partitioned in the list.

4.3 Hash Partitioning

Use a hash function to allocate data to multiple partitions, suitable for evenly distributing data.

Example:

CREATE TABLE products (
    product_id INT NOT NULL,
    product_name VARCHAR(100) NOT NULL,
    price DECIMAL(10, 2) NOT NULL,
    PRIMARY KEY (product_id)
) 
PARTITION BY HASH (product_id) 
PARTITIONS 4;  -- Distribute data to4Partitions

In this example,productsTable basedproduct_idThe column uses hash partitions, and the data will be evenly distributed into 4 partitions.

4.4 Key Partitioning

Similar to hash partitioning, but uses MySQL's built-in hash algorithm for partitioning.

Example:

CREATE TABLE transactions (
    transaction_id INT NOT NULL,
    amount DECIMAL(10, 2) NOT NULL,
    transaction_date DATE NOT NULL,
    PRIMARY KEY (transaction_id)
) 
PARTITION BY KEY (transaction_id) 
PARTITIONS 3;  -- Distribute data to3Partitions

In this example,transactionsTable basedtransaction_idThe columns are key partitioned using MySQL's built-in hash algorithm, and the data will be evenly distributed into 3 partitions.

5. Things to note

  • Design partitioning strategies: Before implementing partitions, you need to carefully analyze the data access pattern to select the appropriate partitioning strategy.
  • Primary key constraints: The primary key of the partition table needs to contain the partition key.
  • Performance monitoring: Partitioning can improve performance, but can also lead to additional complexity, so performance performance needs to be monitored.
  • Specific conditions and restrictions: Some SQL operations may be subject to partition restrictions, such as foreign key constraints and certain aggregate operations.

Summarize

Database partitioning is an effective strategy to manage large data sets, which can improve query performance and management efficiency. Implementing partitioning in MySQL requires selecting the appropriate partition type according to specific business needs and designing a reasonable partitioning plan.

This is the end of this article about the concept and use of MySQL database partitions. For more related contents of MySQL database partitions, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!