Table of Contents
Overview
Partitioning effect
Partition type
1.range partitioning
2.list partition
3 .hash partition
Regular hash partition
Linear hash partition
4.key分区
columns 与子分区
1.columns分区
2.子分区
分区管理
1.range或list分区
2.hash或key分区
Home Database Mysql Tutorial Detailed introduction to partitions in mysql

Detailed introduction to partitions in mysql

May 08, 2017 pm 02:58 PM

Overview

Before, when I saw the partition, I pinched it, it was so tall. Yesterday I finally learned what partitioning is, but it’s nothing more than that. To summarize today, a good memory is not as good as a bad writing.
MySQL Supports partitioning function starting from 5.1. One sentence for partitioning is: Divide a table into multiple areas (pages/files) according to certain rules (range/list/hash/key, etc.) for storage. For mysql application development, there is no difference between partitioning and non-partitioning (that is, it is transparent to the application). It's like "breaking it into pieces" in a breakout battle. MySQL supports most storage engines (such as MyISAM, InnoDB, Memory, etc.) to create partitions, but does not support MERGE and CSV to create partitions. All partitions in the same partition table must be of the same storage engine. Make an example:

#创建一个5个hash分区的myisam表
CREATE TABLE `test`.`partition_t1`(  
  `id` INT UNSIGNED NOT NULL,
  `username` VARCHAR(30) NOT NULL,
  `email` VARCHAR(30) NOT NULL,
  `birth_date` DATE NOT NULL
) ENGINE=MYISAM
PARTITION BY HASH(MONTH(birth_date))
PARTITIONS 5;
Copy after login

Detailed introduction to partitions in mysql

Example result

Partitioning effect

  • Can store more data (maximum limit of a single file in the system)

  • Optimize the query, in the where clause, if the partition condition is included, only one needs to be scanned Or partially partition to improve query efficiency. When it comes to functions such as sum(), it can be processed in parallel on the partitions and the results are finally summarized.

  • For expired or unnecessary data, you can delete related partitions to quickly delete the data.

  • By distributing data queries across multiple disks, the concurrency capability of a single table is improved, and the disk I/O performance is also improved.

Partition type

is divided into 4 types:

  • range partition: based on a given The continuous interval range of the data is allocated to different partitions.

  • List partitioning: similar to range partitioning, the difference is that list is partitioned based on the enumerated value list, and range is partitioned based on the range.

  • Hash partition: Based on the given number of partitions, allocate data to different partitions (modulo/linear)

  • key partition: Similar to hash partitioning.

In MySQL5.1, range, list, and hash partitioning require that the partition key must be int. MySQL 5.5 and above supports non-integer range and list partitions, namely: range columns and list columns.
Note: No matter what kind of partitioning, there is either no primary key/unique key on the partition table, or one of the partition keys must be a primary key/unique key.

1.range partitioning

Range partitioning uses value ranges (intervals) to divide partitions. The intervals must be continuous and cannot overlap each other. Use values ​​less thanOperator performs partition definition.

Example 1:

CREATE TABLE `test`.`partition_t2`(  
  `id` INT UNSIGNED NOT NULL,
  `username` VARCHAR(30) NOT NULL,
  `email` VARCHAR(30) NOT NULL,
  `birth_date` DATE NOT NULL
) ENGINE=MYISAM
PARTITION BY RANGE(id)(
   PARTITION t21 VALUES LESS THAN (10),
   PARTITION t22 VALUES LESS THAN (20),
   PARTITION t23 VALUES LESS THAN MAXVALUE
);
Copy after login

The above example defines a range partition table containing 3 partitions (t21, t22, t23). This is somewhat similar Same as the <a href="http://www.php.cn/code/5745.html" target="_blank">switch statement</a> in high-level languages. The explanation is as follows: when id<10, it is in the t21 partition; when 20>id>=10, it is in the t22 partition; when id>=20, it is in the t23 partition.

Example 2:

CREATE TABLE `test`.`partition_t3`(  
  `id` INT UNSIGNED NOT NULL,
  `username` VARCHAR(30) NOT NULL,
  `email` VARCHAR(30) NOT NULL,
  `birth_date` DATE NOT NULL
) ENGINE=MYISAM
PARTITION BY RANGE COLUMNS(birth_date)(
   PARTITION t31 VALUES LESS THAN (&#39;1996-01-01&#39;),
   PARTITION t32 VALUES LESS THAN (&#39;2006-01-01&#39;),
   PARTITION t33 VALUES LESS THAN (&#39;2038-01-01&#39;)
);
Copy after login

MySQL5.5 improves range partitioning and provides range columns partitioning to support non-integer partitioning.

2.list partition

list partition creates a discrete value list (similar to enum type data in mysql) to divide the partition, use values ​​inoperator to partition. List partitions do not need to be declared in any particular order. Lists are similar to ranges in many ways.

CREATE TABLE `test`.`partition_t4`(  
  `id` INT UNSIGNED NOT NULL,
  `username` VARCHAR(30) NOT NULL,
  `email` VARCHAR(30) NOT NULL,
  `birth_date` DATE NOT NULL
) ENGINE=MYISAM
PARTITION BY LIST(id)(
   PARTITION t41 VALUES IN (1,2),
   PARTITION t42 VALUES IN (3,6),
   PARTITION t43 VALUES IN (5,4),
   PARTITION t44 VALUES IN (7,8)
);
Copy after login

The above example is that when the id is 1 or 2, it is in the t41 partition; when the id is 3 or 6, it is in the t42 partition, and so on...

3 .hash partition

Hash partition is mainly used to disperse hotspot reads to ensure that data is distributed as evenly as possible among a predetermined number of partitions. When a table performs hash partitioning, MySQL applies a hash function to the partition key to determine which of the n partitions the data should be placed in. Hash partition supports two hash functions (partitioning methods): modulo algorithm (default hash partitioning method) and linear power-of-2 algorithm (liner hash partitioning) .

Regular hash partition

#顶部引例就是常规hash分区
Copy after login
  • mysql does not recommend using hashexpressions involving multiple columns.

  • Conventional hashing brings too much cost in partition management and is not suitable for the needs of flexible partitions. See: Consistent Hash Algorithm

  • Because of the management problems of conventional hash partitioning, all mysql introduces linear hash partitioning.

    Linear hash partition

    CREATE TABLE `test`.`partition_t5`(  
    `id` INT UNSIGNED NOT NULL,
    `username` VARCHAR(30) NOT NULL,
    `email` VARCHAR(30) NOT NULL,
    `birth_date` DATE NOT NULL
    ) ENGINE=MYISAM
    PARTITION BY LINEAR HASH(id)
    PARTITIONS 5;
    Copy after login

    In the above example, a linear hash partition of 5 partitions is created.

  • Advantages of linear hash partitioning: MySQL can handle partition maintenance faster;

  • 线性hash分区缺点:分区各个分区之间数据分布不太均衡。

4.key分区

  • hash分区允许用户自定义的表达式,而key分区不允许使用用户自定义的表达式。

  • hash分区只支持整数分区,key分区支持除了blob或text类型之外的其他数据类型分区。

  • 与hash分区不同,创建key分区表的时候,可以不指定分区键,默认会选择使用主键/唯一键作为分区键,没有主键/唯一键,必须指定分区键。

CREATE TABLE `test`.`partition_t6`(  
  `id` INT UNSIGNED NOT NULL,
  `username` VARCHAR(30) NOT NULL,
  `email` VARCHAR(30) NOT NULL,
  `birth_date` DATE NOT NULL
) ENGINE=MYISAM
PARTITION BY LINEAR KEY(email)
PARTITIONS 5;
Copy after login

columns 与子分区

1.columns分区

columns 包括range columns与list columns 支持非整型的分区键。columns分区支持多列分区

CREATE TABLE `test`.`partition_t7`(  
  `a` INT UNSIGNED NOT NULL,
  `b` INT UNSIGNED NOT NULL
)
PARTITION BY RANGE COLUMNS(a,b)(
    PARTITION p0 VALUES LESS THAN (0,10),
    PARTITION p1 VALUES LESS THAN (10,10),
    PARTITION p2 VALUES LESS THAN (10,20),
    PARTITION p3 VALUES LESS THAN (10,35),
    PARTITION p4 VALUES LESS THAN (10,MAXVALUE),
    PARTITION p5 VALUES LESS THAN (MAXVALUE,MAXVALUE)
);
Copy after login

判断依据:(a<10) or ((1=10) and (10<10))。

2.子分区

子分区是分区表中对每一个分区的再次分割,又被称为复合分区。MySQL从MySQL5.1开始支持对通过range和list的表再进行子分区,子分区即可以hash分区,也可以使用key分区。子分区适合保存非常大量的数据记录。

CREATE TABLE partition_t8(id INT,purchased DATE)
PARTITION BY RANGE(YEAR(purchased))
SUBPARTITION BY HASH(TO_DAYS(purchased))
SUBPARTITIONS 2(
    PARTITION p0 VALUES LESS THAN (1990),
    PARTITION p1 VALUES LESS THAN (2000),
    PARTITION p2 VALUES LESS THAN MAXVALUE
);
Copy after login

Detailed introduction to partitions in mysql

mysql子分区

分区管理

MySQL5.1提供添加、删除、重定义、合并、拆分分区命令。

1.range或list分区

#删除分区
alter table partition_t8 drop partition p2;
#添加一个分区
alter table partition_t8 add partition(
    partition p4 values less than (2030)
    )
#重定义一个分区
alter table partition_t8 reorganize partition p3 into(
    partition p2 values less than (2005),
    partition p3 values less than (2015)
);
Copy after login
  • 只能从range分区列表最大端增加分区。

  • 增加list分区,不能添加一个包含现有分区值列表中的任意值分区,也就是说对一个固定的分区键值,必须指定并且只能指定一个唯一的分区。

  • 重新定义range分区,只能够重新定义相邻的分区,同时重新定义的分区区间必须和原分区区间覆盖相同的区间。

2.hash或key分区

#减少分区数,(如将分区数减少到2)
alter table partition_t8 coalesce partition 2;
#增加分区数(如:为分区数增加了8)
alter table partiton_t8 add partition partitions 8;
Copy after login
  • coalesce不能用来增加分区数量。

【相关推荐】

1. 免费mysql在线视频教程

2. MySQL最新手册教程

3. 数据库设计那些事

The above is the detailed content of Detailed introduction to partitions in mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

See all articles