Partition Table related to MySQL

MySQL partition table is divided into index RANGE, LIST, HASH, KEY four types, partition table for the partition table can be established locally.

First, create

1. Create a range partitioned table

MySQL partition table partition key order_day must be included in the primary key, and will have a problem - the year when the threshold is exceeded, to 2013,2014, you need to manually create these partitions

CREATE TABLE sales (
    id INT AUTO_INCREMENT,
    amount DOUBLE NOT NULL,
    order_day DATETIME NOT NULL,
    PRIMARY KEY(id, order_day)
) ENGINE=Innodb PARTITION BY RANGE(YEAR(order_day)) (
    PARTITION p_01 VALUES LESS THAN (2010),
    PARTITION p_2011 VALUES LESS THAN (2011),
    PARTITION p_2012 VALUES LESS THAN (2012),
    PARTITION p_catchall VALUES LESS THAN MAXVALUE);

2. Create a hash partition table

The following statement indicates that each 100W of data to create a partition, and did not affect the threshold range

CREATE TABLE sales (
    id INT PRIMARY KEY AUTO_INCREMENT,
    amount DOUBLE NOT NULL,
    order_day DATETIME NOT NULL
) ENGINE=Innodb PARTITION BY HASH(id DIV 1000000);

 

Second, the partition table management operations

  • Check whether to support partition
mysql> show variables like "%part%";  
+-------------------+-------+  
| Variable_name     | Value |  
+-------------------+-------+  
| have_partitioning | YES   |  
+-------------------+-------+  
1 row in set (0.00 sec)  
  • Look-up table for each partition
SELECT PARTITION_NAME,TABLE_ROWS FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_NAME = 'history_uint';
  • Delete partition

Note You can not delete key or hash partitioning

alter table emp drop partition p1;
-- 一次性删除多个分区
alter table emp drop partition p1,p2;
  • Add District
alter table emp add partition (partition p3 values less than (4000));
alter table empl add partition (partition p3 values in (40));
  • Split Partition

Reorganize partition may be made to the partition table portion or all of the modified partition, with no data loss, the overall range resolution should be consistent before and after partitioning.

alter table te reorganize partition p1 into
(partition p1 values less than (100),
partition p3 values less than (1000)); ----不会丢失数据
  • Merging Partitions
-- Merge分区:把2个分区合并为一个
alter table te reorganize partition p1,p3 into
(partition p1 values less than (1000)); ----不会丢失数据
  • Redefine hash partition table
Alter table emp partition by hash(salary) partitions 7;
  • Redefine the range partition table
Alter table emp partition by range(salary)
(partition p1 values less than (2000),
partition p2 values less than (4000));
  • Delete all partitions table

Equivalent to change back to normal table, note that not delete data.

Alter table emp remove partitioning;--不会丢失数据
  • Re-partition

Equivalent to delete all records in the partition, and then insert them back, it can be used to organize partition debris.

ALTER TABLE emp rebuild partitionp1,p2;
  • Optimization partition

If you delete a large number of rows from the partition, or on-line with a variable length (VARCHAR, BLOB or TEXT columns of type) made a lot of changes, you can use "ALTER TABLE ... OPTIMIZE PARTITION" is not used to recover space and to defragment the partition data file.

ALTER TABLE emp optimize partition p1,p2;
  • Analysis of partition

Read and save the partition key distribution.

ALTER TABLE emp analyze partition p1,p2;
  • Check the partition

Check the partition, similar to the use of non-partitioned tables CHECK TABLE. This command can tell you the emp table partition p1, p2 whether the data or index has been destroyed. If you have been destroyed, you can use the repair command to repair the partition.

ALTER TABLE emp CHECK partition p1,p2;
  • Repair partition

Repair damaged partition

ALTER TABLE emp repair partition p1,p2;
Published 295 original articles · won praise 35 · views 80000 +

Guess you like

Origin blog.csdn.net/Hehuyi_In/article/details/105168083