mysql partition management

Reference: https://www.cnblogs.com/ivictor/p/5025764.html

1. How to manage RANGE and LIST partitions

Take the partition table as an example

Copy code
CREATE TABLE members (
    id INT,
    fname VARCHAR(25),
    lname VARCHAR(25),
    dob DATE
)
PARTITION BY RANGE( YEAR(dob) ) (
    PARTITION p0 VALUES LESS THAN (1970),
    PARTITION p1 VALUES LESS THAN (1980),
    PARTITION p2 VALUES LESS THAN (1990)
);
Copy code

1. Delete a partition

ALTER TABLE members DROP PARTITION p1;

Note: If a partition is deleted, the data in the partition will be lost. Not only that, when you use the show create table members \ G; command to view the table creation statement, you will not be able to see any information about the deleted partition.

         For the RANGE partition, if the p1 partition is deleted, when the data is inserted, if the date is within the range of 1970 to 1980, the data will be allocated to the next partition, namely p2.

         For the LIST partition, if a partition is deleted, when inserting data, if the data belongs to this partition, the insertion will report an error.

         If you just delete the data without deleting the information of the partition, you can use the truncate command

ALTER TABLE members TRUNCATE PARTITION p1;

 

2. Add partition

ALTER TABLE members ADD PARTITION (PARTITION p3 VALUES LESS THAN (2000));

Note: To add a partition using the ADD command, it can only be added at the end of the partition list. In this example, it can only be added after 1990.

Of course, in the actual production environment, this limitation is too large, for example, I want to add a partition before the p0 partition, the interval is 1960, or add a 1975 partition between p1, at this time, using ADD will not meet For such requirements, you can use the ALTER TABLE ... REORGANIZE PARTITION command.

for example:

ALTER TABLE members REORGANIZE PARTITION p0 INTO (
    PARTITION s0 VALUES LESS THAN (1960),
    PARTITION s1 VALUES LESS THAN (1970)
);

The REORGANIZE command is actually quite flexible, not only can split partitions, but also can be used to merge partitions, such as:

ALTER TABLE members REORGANIZE PARTITION p0,p1,p2,p3 INTO (
    PARTITION m0 VALUES LESS THAN (1980),
    PARTITION m1 VALUES LESS THAN (2000)
);

note:

1> You cannot use the REORGANIZE PARTITION command to modify the partition type of a table. You can only use the ALTER TABLE ... PARTITION BY .... statement, for example:

ALTER TABLE members
    PARTITION BY HASH( YEAR(dob) )
    PARTITIONS 8;

2> REORGANIZE PARTITION syntax is as follows:

ALTER TABLE tbl_name
    REORGANIZE PARTITION partition_list
    INTO (partition_definitions);

The range of partitions in partition_definitions must cover the range of partitions in partition_list.

 

Second, how to manage HASH and KEY partition

Take the partition table as an example

Copy code
CREATE TABLE clients (
    id INT,
    fname VARCHAR(30),
    lname VARCHAR(30),
    signed DATE
)
PARTITION BY HASH( MONTH(signed) )
PARTITIONS 12;
Copy code

For HASH partition and KEY partition, the above RANGE and LIST partition syntax is not supported, such as DROP, TRUNCATE, REORGANIZE partition.

In fact, it only supports one type of "partition adjustment".

ALTER TABLE clients COALESCE PARTITION 4;

The purpose of this command is to cut 4 partitions of the clients table from 12 to 8.

ALTER TABLE clients ADD PARTITION PARTITIONS 6;

Similarly, this command adds 6 partitions to the clients table, from 12 to 18.

Guess you like

Origin www.cnblogs.com/pejsidney/p/12718819.html