当前位置:网站首页>MySQL partition explanation and operation statement
MySQL partition explanation and operation statement
2022-07-07 08:43:00 【Blue sky ⊙ white clouds】
One 、 Detailed explanation of table partition function
mysql The data in the database exists on the disk in the form of files , Default on /mysql/data below ( Can pass my.cnf Medium datadir Check it out. ),
A table mainly corresponds to three documents , One is frm Storage table structure , One is myd Storing table data , One is myi A table index . If a table has too much data , that myd,myi It's going to be big , Finding data can be slow ,
We can make use of mysql Partition function of , Physically, three files corresponding to this table , Divide into many small pieces , So what? , When we look up a piece of data , You don't have to look it all up , Just know where this piece of data is , Then you can find it in that piece .
If the table data is too large , Maybe a disk can't fit , This is the time , We can allocate data to different disks .
Table partitioning , According to certain rules , Decompose a table in a database into smaller ones , Easy to manage parts . Logically speaking , There's only one watch , But the bottom layer is made up of multiple physical partitions .
2、 The difference between table partition and sub table
table : It refers to passing certain rules , Break a table into several different tables . For example, user order records are divided into multiple tables according to time .
The difference between a sub table and a partition is : The partition logically has only one table , The sub table is to decompose a table into multiple tables .
3、 What's the benefit of table partitioning ?
(1)、 Compared to a single disk or file system partition , Can store more data .
(2)、 For data that has lost its preservation significance , You can usually delete partitions that are related to that data , It's easy to delete that data . By contraries , In some cases , The process of adding new data can be realized by adding a new partition for those new data , It's easy to implement .
(3)、 Some queries can be greatly optimized , This is mainly by satisfying a given WHERE Statement data can only be stored in one or more partitions , So you don't have to look up the rest of the partitions . Because partitions can be modified after the partition table is created , So when I didn't do that the first time I configured the partition scheme , You can reorganize the data , To improve the efficiency of common queries .
(4)、 It involves, for example SUM() and COUNT() So aggregate function queries , It's easy to do parallel processing . A simple example of such a query is “SELECT salesperson_id, COUNT (orders) as order_total FROM sales GROUP BY salesperson_id;”. adopt “ parallel ”, This means that the query can be performed simultaneously on each partition , The final result is just the result of summing up all the partitions .
5)、 Distribute data queries across multiple disks , To get more query throughput .
4、 The limitations of partitioned tables
(1)、 A watch can only have at most 1024 Zones .
(2)、 MySQL5.1 in , The partition expression must be an integer , Or an expression that returns an integer . stay MySQL5.5 Support for non integer expression partitioning is provided in .
(3)、 If the partition field has a primary key or a unique index column , So many primary key columns and unique index columns must be included . namely : Partition fields either don't contain primary keys or index columns , Either include all primary keys and index columns .
(4)、 Foreign key constraints cannot be used in partition tables .
(5)、MySQL Partition for all data and indexes in a table , You can't partition only the table data, not the index , You can't just partition the index, not the table , You can't partition only a part of the data in a table .
5、 How to judge the present MySQL Partition support or not ?
mysql> show variables like '%partition%';
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| have_partitioning | YES |
+-------------------+-------+
1 row in set (0.00 sec)
Two 、 Partition operation statement
1. Create partitions
(1) establish range Partition
According to the range of values :
drop table if exists employees;
create table employees(
id int not null,
fname varchar(30),
lname varchar(30),
hired date not null default '1970-01-01',
separated date not null default '9999-12-31',
job_code int not null default 0,
store_id int not null default 0
)engine=myisam default charset=utf8
partition by range(store_id)(
partition p0 values less than (6),
partition p1 values less than (11),
partition p2 values less than (16),
partition p3 values less than (21),
partition p4 values less than MAXVALUE
);
according to DATE、DATETIME Range :
add to COLUMNS Keywords can define non - integer Range and multi column range , But be careful COLUMNS Only column names can be in parentheses , Function not supported ; When multiple column ranges , The multi column range must show an increasing trend :
drop table if exists member;
create table member(
firstname varchar(25) not null,
lastname varchar(25) not null,
username varchar(16) not null,
email varchar(35),
joined date not null
)
partition by range columns(joined)(
partition p0 values less than ('1960-01-01'),
partition p1 values less than ('1970-01-01'),
partition p2 values less than ('1980-01-01'),
partition p3 values less than ('1990-01-01'),
partition p4 values less than maxvalue
)
According to the multi column range :
drop table if exists rc3;
create table rc3(
a int,
b int
)
partition by range columns(a,b)(
partition p0 values less than (0,10),
partition p1 values less than (10,20),
partition p2 values less than (20,30),
partition p3 values less than (30,40),
partition p4 values less than (40,50),
partition p5 values less than (maxvalue,maxvalue)
)
drop table if exists staff;
create table staff(
id int not null,
fname varchar(30),
lname varchar(30),
hired date not null default '1970-01-01',
separated date not null default '9999-12-31',
job_code int not null default 0,
store_id int not null default 0
)engine=myisam default charset=utf8
partition by range(year(separated))(
partition p0 values less than (1991),
partition p1 values less than (1996),
partition p2 values less than (2001),
partition p4 values less than MAXVALUE
);
- When you need to delete a partition “ old ” Data time , Just delete the partition . If you use the partition scheme given in the most recent example above , You just need to use ”alter table
staff drop partition
p0;” To delete all the information in 1991 All the lines corresponding to employees who stopped working years ago . For a table with a large number of rows , It's better than running a program like ”delete from staff
WHERE year(separated) <= 1990;” Such a one DELETE Queries are much more efficient . - Want to use a date or time value , Or a column containing values that grow from some other series .
- Often run queries that depend directly on the columns used to split the table . for example , When performing a task such as ”select count(*) from staff where
year(separated) = 200 group by
store_id;” In such a query ,MySQL You can quickly determine that only partitions p2 Need to scan , This is because it is impossible for the remaining partitions to contain the WHERE Any record of clause .
(2)LIST Partition
It's like pressing RANGE Partition , The difference lies in LIST Partitioning is based on matching column values to a certain value in a set of discrete values .
LIST Partition by using “PARTITION BY LIST(expr)” To achieve , among “expr” Is a column value or a value based on a column value 、 And returns an integer value expression , And then through “VALUES IN (value_list)” Define each partition in the same way , among “value_list” Is a comma separated list of integers .
Suppose there is 20 A video store , Distribution in 4 It's a distribution area , As shown in the following table :
====================
region The store ID Number
North Area 3, 5, 6, 9, 17
east area 1, 2, 10, 11, 19, 20
West Area 4, 12, 13, 14, 18
Central area 7, 8, 15, 16
drop table if exists staff;
create table staff(
id int not null,
fname varchar(30),
lname varchar(30),
hired date not null default '1970-01-01',
separated date not null default '9999-12-31',
job_code int not null default 0,
store_id int not null default 0
)
partition by list(store_id)(
partition pNorth values in (3,5,6,9,17),
partition pEast values in (1,2,10,11,19,20),
partition pWest values in (4,12,13,14,18),
partition pCentral values in (7,8,15,16)
);
This makes it easy to add or delete employee records in specified regions in the table . for example , Suppose all the video stores in the west end are sold to other companies . So all the records related to employees working in the west end video store ( That's ok ) You can use queries “ALTER TABLE staff DROP PARTITION pWest;” To delete , It has the same effect as DELETE( Delete )“DELETE FROM staff WHERE store_id IN (4,12,13,14,18);” Compare to , Much more effective .
If you try to insert a column value ( Or the return value of a partition expression ) When not in a row in the partition value list , that “INSERT” The query will fail with an error .
When there is an error inserting multiple pieces of data , If the table's engine supports transactions (Innodb), No data will be inserted ; If transactions are not supported , The data before the error will be inserted , The latter will not execute .
And Range The partitions are the same , add to COLUMNS Keywords can support non integer and multi column .
(3)HASH Partition
Hash Partitioning is mainly used to ensure that data is evenly distributed in a predetermined number of partitions ,Hash The parentheses can only be integer columns or functions that return fixed integers , In fact, it is to use the returned integer to modulo the partition number .
To use HASH Partition to split a table , To be in CREATE TABLE Add a “PARTITION BY HASH (expr)” Clause , among “expr” Is an expression that returns an integer . It can just be that the field type is MySQL The name of a column of integers . Besides , You probably need to add another one at the end “PARTITIONS num” Clause , among num It's a nonnegative integer , It represents the number of partitions that the table will be divided into .
If it doesn't include one PARTITIONS Clause , Then the number of partitions will default to 1.
drop table if exists staff;
create table staff(
id int not null,
fname varchar(30),
lname varchar(30),
hired date not null default '1970-01-01',
separated date not null default '9999-12-31',
job_code int not null default 0,
store_id int not null default 0
)
partition by hash(store_id)
partitions 4; # Number of partitions
drop table if exists staff;
create table staff(
id int not null,
fname varchar(30),
lname varchar(30),
hired date not null default '1970-01-01',
separated date not null default '9999-12-31',
job_code int not null default 0,
store_id int not null default 0
)
partition by hash(year(hired))
partitions 4;
Hash Partitions also exist with traditional Hash The same question as the sub table , Poor scalability .MySQL Provides a similar to Hash Partition method of - linear Hash Partition , Just add... When defining the partition LINEAR keyword .
drop table if exists staff;
create table staff(
id int not null,
fname varchar(30),
lname varchar(30),
hired date not null default '1970-01-01',
separated date not null default '9999-12-31',
job_code int not null default 0,
store_id int not null default 0
)
partition by linear hash(year(hired))
partitions 4;
Linear hash function , The difference between it and regular hashing is , The linear hash function uses a linear 2 The power of (powers-of-two) Algorithm , The conventional hash uses the modulus of the hash function value .
(4)KEY Partition
Key Zoning and Hash Partition is very similar , It's just Hash Functions are different , Define as Hash Replace the keyword with Key that will do , Again Key Partitions also have correspondence and linearity Hash Linearity of Key Partition method .
drop table if exists staff;
create table staff(
id int not null,
fname varchar(30),
lname varchar(30),
hired date not null default '1970-01-01',
separated date not null default '9999-12-31',
job_code int not null default 0,
store_id int not null default 0
)
partition by key(store_id)
partitions 4;
stay KEY Use keywords in partitions LINEAR And in HASH Use in partitions has the same effect , The number of the partition is through 2 The power of (powers-of-two) The algorithm gets , Not through the modulus algorithm .
in addition , When the table has a primary key or unique index, you can omit Key Column names in parentheses ,Mysql Will be based on the primary key - The order of the unique index is selected , When the unique index cannot be found, an error is reported .
2. Add partition :
alter table table_name
reorganize partition p2 into
(partition p0 values less than(7),
partition p1 values less than(10),
partition p2 values less than(16);
Be careful : No more than p2 The scope of the , Strictly increment each partition , That is, the minimum cannot be less than the previous partition , Two new partitions are added below n01 and n02.
Illustration : Now let's revise p2 This partition , Because the partition range is specified 16 within , So in p2 The range of partitions added in can only be limited to p1-p2 Between , Here is 10-16, Otherwise, there will be an error .
3. Delete partition , Delete data
alter table table_name drop partition p1;
4. Partition merging
routine HASH And linear HASH The principle of increasing contraction partition is the same . After adding and shrinking partitions, the original data will be redistributed according to the number of existing partitions .HASH Partition cannot be deleted , So it can't be used DROP PARTITION Delete the partition ;
Only through ALTER TABLE … COALESCE PARTITION num To merge partitions , there num Is the number of partitions subtracted ;
Can pass ALTER TABLE … ADD PARTITION PARTITIONS num To add partitions , Here is num It is the number of partitions added on the original basis .
(1) Merge partitions
ALTER TABLE tblinhash COALESCE PARTITION 3; # subtract 3 Zones
Be careful : Subtract two partitions , The data will be redistributed according to the existing partitions
SELECT PARTITION_NAME,PARTITION_METHOD,PARTITION_EXPRESSION,PARTITION_DESCRIPTION,TABLE_ROWS,SUBPARTITION_NAME,SUBPARTITION_METHOD,SUBPARTITION_EXPRESSION
FROM information_schema.PARTITIONS WHERE TABLE_SCHEMA=SCHEMA() AND TABLE_NAME='employees';
(2) Add partitions
ALTER TABLE tblinhash add PARTITION partitions 4; # increase 4 Zones
When in 3 Add 4 After a partition ,‘2003-04-14’ From the original p1 Turned into p3, And the other record is from the original p2 Turned into p6;
(3) Remove table partition ( Just remove the partition , Data will not be deleted .)
ALTER TABLE tablename REMOVE PARTITIONING ;
Be careful : Use remove To remove a partition is to remove only the definition of the partition , It doesn't delete data ;
drop PARTITION Dissimilarity , The latter will be deleted along with the data .
边栏推荐
- 2-3查找树
- leetcode134. gas station
- Input of mathematical formula of obsidan
- POJ - 3616 Milking Time(DP+LIS)
- 联想混合云Lenovo xCloud:4大产品线+IT服务门户
- [hard core science popularization] working principle of dynamic loop monitoring system
- Three series of BOM elements
- 調用華為遊戲多媒體服務的創建引擎接口返回錯誤碼1002,錯誤信息:the params is error
- Are you holding back on the publicity of the salary system for it posts such as testing, development, operation and maintenance?
- [Yu Yue education] higher vocational English reference materials of Nanjing Polytechnic University
猜你喜欢

Quick sorting (detailed illustration of single way, double way, three way)

The single value view in Splunk uses to replace numeric values with text

Input of mathematical formula of obsidan

AVL平衡二叉搜索树

SSM 整合

Greenplum6.x搭建_安装

2 - 3 arbre de recherche

GFS distributed file system
![FPGA knowledge accumulation [6]](/img/db/c3721c3e842ddf4c1088a3f54e9f2a.jpg)
FPGA knowledge accumulation [6]

idea里使用module项目的一个bug
随机推荐
Opencv learning note 3 - image smoothing / denoising
GOLand idea intellij 无法输入汉字
Implementation of navigation bar at the bottom of applet
IP guard helps energy enterprises improve terminal anti disclosure measures to protect the security of confidential information
ES6_ Arrow function
Leetcode 1984. Minimum difference in student scores
登山小分队(dfs)
uniapp 微信小程序监测网络
opencv之图像分割
南京商品房买卖启用电子合同,君子签助力房屋交易在线网签备案
Input of mathematical formula of obsidan
Three usage scenarios of annotation @configurationproperties
How to realize the high temperature alarm of the machine room in the moving ring monitoring system
What is the method of manual wiring in PCB design in 22protel DXP_ Chengdu electromechanical Development Undertaking
let const
Mock.js用法详解
数据分片介绍
[Yu Yue education] basic reference materials of electrical and electronic technology of Nanjing Institute of information technology
A bug using module project in idea
Download and install orcale database11.2.0.4