Create, add, delete MySQL table partition

Source: Internet
Author: User

1. Test add partition and delete Partition # # #添加删除range分区 (1) Create a partition:create table titles  (    emp_no       INT NOT NULL,    title        varchar (     not null,    from_date)    date            not null,     to_date     DATE,    KEY           (emp_no),    primary key  (emp_no,title ,  from_date))  partition by range columns (from_date) (partition p01 values  less than  (' 1985-12-31 '),partition p02 values less than  (' 1990-12-31 ') ),partition p03 values less than  (' 1995-12-31 '),partition p04 values  less than  (' 2000-12-,partition p05 values less than  (' 2005-12-31 '), partition p06 values  less than  (' 2010-12-31 '),partition p07 values less than  (' 2015-12-31 ') ),partition p08 values less than  (' 2020-12-31 '),partition p09 values  less than  (' 2025-12-31 '),partition p10 values less than  (' 2030-12-31 ')); Import Data Mysql> source titles.sql (2) Add partition: Note: cannot exceed p04 range, strictly increment each partition, i.e. minimum cannot be less than the previous partition under the new two partitions N01 and n02  Alter table titles reorganize partition p04 into ( partition n01  Values less than (' 1997-12-31 '),  partition n02 values less than (' 1998-12-31 ') ),  partition p04 values less than (' 2000-12-31 ')  );(3) Delete partition: Delete partition deletes data. Mysql> select count (*)  from titles where from_date< ' 1985-12-31 '; +--------- -+| count (*)  |+----------+|    18238 |+----------+1 row in set  (0.09 sec) mysql>  alter table titles drop partition p01; query ok, 0 rows affected  (0.05 sec) records: 0  duplicates:  0  warnings: 0mysql> select count (*)  from titles where from_ date< ' 1985-12-31 '; +----------+| count (*)  |+----------+|         0 |+----------+1 row in set  (0.04 sec) # # #添加删除list分区 (1) Create a list partition create  table titles  (    emp_no      INT NOT  Null,    title       varchar (   )   NOT NULL,    from_date   DATE             NOT NULL,    to_date     DATE,    KEY           (emp_no),    primary key  (Emp_no,title,  from_date))  partition by list columns (title) (Partition p0 values in   (' Assistant engineer '),partition p1 values in  (' Engineer '), PARTITION&NBSP;P2  values in  (' Manager '),partition p3 values in  (' Senior engineer '), partition p4 values in  (' Senior staff '),partition p5 values in  (' Staff '),partition p6 values in  (' Technique leader ')); Import Data titles.sql  Is the data exported from the Employees Database Mysql> source titles.sql (2) Add partition Mysql> alter table titles  add partition (Partition p7 values in (' CEO ')); query ok, 0 rows affected  (0.07 sec) Records: 0&nbsP; duplicates: 0  warnings: 0 (3) Delete partition: Mysql> alter table titles  drop partition p0; query ok, 0 rows affected  (0.04 sec) records: 0  duplicates:  0  warnings: 0## #添加删除hash分区 (1) Create a hash partition create table titles  (     emp_no      INT NOT NULL,    title        varchar (     NOT NULL,   )  from_date   DATE             Not null,    to_date     date,    key           (emp_no),    primary key  ( emp_no,title, from_date))  partition by hash (emp_no) partitions 4; Import data:mysql> source titles.sql (2) Delete hash partition 4 partitions reduced to two,4-2=2mysql> alter table titles  coalesce partition 2; query ok, 443308 rows affected  (12.41 sec) records: 443308   Duplicates: 0  warnings: 0 (3) Add a hash partition to 5,2+3=5mysql> alter table  titles add partition partitions 3; query ok, 443308 rows affected  (11.54 sec) records: 443308   duplicates: 0  warnings: 0## #添加删除key分区 (1) Create a key partition create table titles  (     emp_no      INT NOT NULL,     Title       varchar (     NOT NULL, )    from_date   DATE             not null,    to_date     date,    key          ( Emp_no),    primary key  (emp_no,title, from_date))  partition by  key (emp_no) partitions 4; Import Data mysql> source titles.sql (2) Delete partition, same as Hashmysql> alter  table titles coalesce partition 2; query ok, 443308 rows affected  (7.86 sec) records: 443308   Duplicates: 0  warnings: 0 (3) Add partition, same as hashmysql> alter table titles  add partition partitions 3; query ok, 443308 rows affected  (6.17 sec) records: 443308   Duplicates: 0  warnings: 0

Create, add, and delete MySQL table partitions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.