"Mysql Explained in a Simple Way" Reading and Combing Four

Click to view the collection

Transaction control

Open the transaction: start transaction
roll back to the place where the transaction started: RollBack
add save point: SavePoint point1 (save point with the same name, the later created will overwrite the first created. Delete the save point: release SavePoint point1)
roll back to the save point: RollBack To point1
Commit the transaction: Commit
commit and chain: After submitting the transaction, immediately start a new transaction.
Commit release or commit work: Disconnect the client after submitting the transaction.

MySQL partition

Partitioning is conducive to the management of very large tables, he adopted the logic of divide and conquer. Partitioning introduces the concept of partition key. The partition key is used to perform data aggregation based on a certain interval value (or range), a specific value list, or Hash function value. Let the data be distributed in different partitions according to the rules. Let one big object become many small objects.

Advantages of MySQL partitioning

1. Compared with a single disk or file system partition, it can store more data
. 2. Optimize query. When the partition condition is included in the where clause, you can scan only the necessary one or a few partitions. The count(*) function can be executed concurrently and then summarize the results
. 3. For data that has expired or does not need to be saved, you can delete the data by deleting the partitions related to the data
. 4. Distribute data queries across multiple disks to obtain more Large query throughput.

Partition type

1. Range partition: Based on a given continuous interval range, data is allocated to different partitions.
2. List partition: Similar to Range partition, the difference is that List partition is based on the enumerated value list partition, Range is based on the given continuous interval range partition
3. Columns partition: similar to Range and List, the difference is that the partition key can be used It is multi-column and can be non-integer
4. Hash partition: Based on the given number of partitions, the data is
modulated and assigned to different partitions 5. Key partition: similar to Hash partition, but uses the hash function provided by MySQL
6. Sub Partition: Also called compound partition or combined partition, that is, another partition is made under the primary partition to divide the data again.
In Mysql 5.7, all partitions except Key partition and Columns partition must be of type int.
You cannot use a key other than the primary key or unique key as the partition key, unless there is no primary key or unique column.

Range partition

example

create table test7(
	id int
)
Partition BY Range(id) (
	Partition p0 Values Less Than (10),
	Partition p1 Values Less Than (20),
	Partition p2 Values Less Than (30)
)

There are three areas with id as the partition key. They are less than 10, [10,20) [20,30). If you insert 30, an error will be reported.
And just like the switch case statement, the order of these three cannot be reversed

Insert into test7 VALUES(30)
> 1526 - Table has no partition for value 30
> 时间: 0s

Because there is no stipulation where data greater than or equal to 30 exists. So just add it. You can add data greater than or equal to 30 as a partition through the following statement

Alter Table test7 add Partition(Partition p3 Values Less Than(MAXVALUE))
> OK
> 时间: 0.037s

Insert statement

Insert into test7 VALUES(30)
> Affected rows: 1
> 时间: 0.003s

If you want to add a partition after using Values ​​Less Than (MAXVALUE). Can only re-create the partition

Alter Table test7 Partition BY Range(id) (
	Partition p0 Values Less Than (10),
	Partition p1 Values Less Than (20),
	Partition p2 Values Less Than (30)
)
> OK
> 时间: 0.172s

The partition key also supports expressions (such as Year (date)).
Starting from version 5.5, Mysql has improved the Range partitioning function and provides Range Columns partitioning. This partitioning method supports non-integer partitioning.

Range zone applicable environment

1. When you need to delete expired data, you can delete the data in the p0 partition with a simple Alter Table test7 Drop Partition p0.
2. Frequently run queries that include the partition key. Mysql can quickly determine which partitions the data to be searched for

List partition

Create List Partition

create table test8(
	id int
)
Partition BY List(id) (
	Partition p0 Values in (1,2,3),
	Partition p1 Values in (4,5,6),
	Partition p2 Values in (10)
)
> OK
> 时间: 0.062s

In the List partition, there is no such a way that Values ​​Less Than (MAXVALUE) in the Range partition can represent all other values. All conditions must exist in the list.
If you want to define a non-integer List. You can use List Columns.
Other operations are similar to Range

Columns partition

Columns can be subdivided into List Columns and Range Columns
examples

create table test9(
	id int,
	idd int
)
Partition BY List Columns(id,idd)
 (
	Partition p0 Values in ( (1,2021) ,(11,3021),(11,31021)  ),
	Partition p1 Values in ( (2,2023) ),
	Partition p2 Values in ( (3,2022))
)
> OK
> 时间: 0.065s
create table test10(
	id int,
	idd int
)
Partition BY Range Columns(id,idd)
 (
	Partition p0 Values Less Than(10,10),
	Partition p1 Values Less Than(10,20),
	Partition p2 Values Less Than(MAXVALUE,MAXVALUE)
)
> OK
> 时间: 0.059s

Note:
The partition key of Columns cannot be
the first partition key of the expression Range Columns. It must be in order and cannot be crossed.
Columns can use non-integer as the partition key. The types that can be used are
integer type: tinyint smallint mediumint int bigint
date and time type: date datetime
character type: char varchar binary varbinary

Hash partition

Hash partition can be subdivided into regular Hash partition and linear Hash partition.
Conventional Hash partition is calculated by modulo operation.
Conventional Hash partition example

create table test11(
	id int
)
Partition BY Hash(id) Partitions 10
> OK
> 时间: 0.196s

id is the partition key, divided into 10 areas. If id = 0, then partition p0. If the id is 11, in the p1 partition (modulo operation),
if you want to add a partition, you must change the number of partitions. In this case, the previous data must be replaced with the partition. This will bring huge expenses.
If you use linear partitioning, Mysql can handle it faster when partitioning maintenance (addition, deletion, merge, and split partition). However, the linear partition is not evenly distributed compared to the conventional Hash partition.
Linear partitioning is obtained by the power of 2 algorithm.
The statement of linear partitioning only needs to add a linear before Hash(id).

create table test12(
	id int
)
Partition BY Linear Hash(id) Partitions 10
> OK
> 时间: 0.184s

The partition key of Hash partition can only be integer, and expressions can be used

Key partition

The Key partition is very similar to the Hash partition, and there is also a Liner Key.
The Key partition can specify zero or more keys as the partition key.
When 0 is specified, the primary key will be specified as the partition key by default. If there is no primary key, specify a non-empty unique key as the partition key. If there are none, it cannot be 0. The
key partition can use all data types except Blob and Text as the partition key.
Example

create table test13(
	id int
)
Partition BY Key(id) Partitions 10
> OK
> 时间: 0.227s

Subpartition

Sub-partitioning is the operation of re-partitioning the partition. Now supports sub-partitions for Range and List and Columns partitions.
Each partition has the same number of sub-partitions.
If you want to display the creation of sub-partitions, then each partition must be displayed and created.
Key Linear Key or Hash Liner Hash can be used to create sub-partitions. Sub-partitions are
implicitly created.

create table test14(
	id int
)
Partition BY List Columns(id)
SubPartition BY Linear Key(id) SubPartitions 10
(
	Partition p0 Values in (12)
)

Explicitly create subpartitions

create table test15(
	id int
)
Partition BY List Columns(id)
SubPartition BY Linear Key(id) 
(
		Partition p0 Values in (12)(
			SubPartition sp0,
			SubPartition sp1
		),
		Partition p1 Values in (13)(
			SubPartition sp2,
			SubPartition sp3
		)
)
> OK
> 时间: 0.075s

MySQL partition handling NULL worthy way

MySQL does not forbid the partition key to be NULL. NULL is generally expressed as the minimum value or zero value
. NULL is regarded as the value of 0 in the Hash Key partition. In the LIst partition, NULLs need to be enumerated in the list. As the minimum value in the Range table.

Partition management

MySQL provides commands for adding, deleting, redefining, merging, splitting, and splitting swap partitions. These operations can be implemented through Alter Table3 commands.

Range and List partition management

Their partition management is very similar.

redefine

Alter table 表名 Partition By Range/List(分区键) (Partition Values.....)

Add to

Alter Table 表名 add Partition(Partition 分区名 Values XXX)

The Range partition can only be added from the right end, and the List cannot add duplicate values

delete

Alter Table 表名 Drop Partition 分区名 #删除数据
alter table 表名 Remove Partitioning #不删除数据

Delete the data in the partition at the same time

Reorganization

Alter Table 表名 Reorganized Partition 分区名 into(
	Partition 分区名 Values....,
	Partition 分区名 Values....
) #拆分

Alter Table 表名 Reorganized Partition 分区名,分区名 into(
	Partition 分区名 Values....,
) #重组

Range and List splits must cover the same partition as before. Reorganization can only reorganize adjacent partitions. Redefining a partition cannot change the partition type

Hash and Key partition management

redefine

Alter table 表名 Partition By Hash/Key(分区键) Partitions n

Merge partition

Alter Table 表名 Coalescs Partition num #num为减少num个分区

Increase partition

alter table 表名 add Partition Partitions num#num为增加num个分区

delete

alter table 表名  remove Partitioning#不删除数据

Swap partition

Alter Table pt Exchange Partition p With Table nt

It is possible to exchange data between a partition or sub-partition p in the partition table pt and the ordinary table nt,
but the following conditions need to be met:
1. The table nt cannot be a partition table. If the partition table exchange is implemented, an intermediate table can be added, which is completed by two exchanges.
2. The table nt cannot be a temporary table
. 3. The structure of the table pt and nt must be exactly the same. Including the name of the index and the column of the index must be the same (create table t1 like t2; alter table t1 remove Partitioning)
4. There can be no foreign keys on the table nt, nor can there be foreign keys referencing nt
5. All data on the nt table are Should be within the range of partition p

Note:
1. The swap partition will not trigger any triggers
. 2. The value of the auto-increment column in the table will be reset
. 3. The Ignore keyword in the swap partition command will not affect

Guess you like

Origin blog.csdn.net/qq_30033509/article/details/114445137