how to drop partition without dropping data in MySQL?

前端 未结 4 1655
执笔经年
执笔经年 2021-02-03 23:11

I have a table like:

create table registrations( 
id int not null auto_increment primary key,
name varchar(50),
mobile_number varchar(13)) 
engine=innodb 
partit         


        
相关标签:
4条回答
  • 2021-02-03 23:23

    You can reorganize the partition p0 using the ALTER TABLE .. REORGANIZE PARTITION command.

    http://dev.mysql.com/doc/refman/5.5/en/partitioning-management-range-list.html

    If you intend to change the partitioning of a table without losing data, use ALTER TABLE ... REORGANIZE PARTITION

    ALTER TABLE registrations 
    REORGANIZE PARTITION p0 INTO (
        PARTITION p0 VALUES LESS THAN (10000),
        PARTITION p0 VALUES LESS THAN (20000)
    );
    

    Note that this will not make sense until you actually create several partitions e.g.

    ALTER TABLE registrations 
    REORGANIZE PARTITION p0 INTO (
        PARTITION p0 VALUES LESS THAN (10000),
        PARTITION p1 VALUES LESS THAN (20000),
        PARTITION p2 VALUES LESS THAN MAXVALUE
    );
    

    Have a look at RANGE partitioning in MySQL

    If your partition p2 is becoming too large you can split it the same way.

    0 讨论(0)
  • 2021-02-03 23:31

    Rearrange partitions doesn't require drop all existing partitions. You can specified the new partitioning in the ALTER TABLE syntax directly, and no data will be lost.

    ALTER TABLE registrations
    PARTITION by RANGE(id) (
    PARTITION p1 VALUES LESS THAN (10000),
    PARTITION p2 VALUES LESS THAN (20000),
    PARTITION p3 VALUES LESS THAN (30000),
    PARTITION p4 VALUES LESS THAN (40000),
    PARTITION p5 VALUES LESS THAN (MAXVALUE);
    

    P.S. Tested with MySQL 5.7.11

    0 讨论(0)
  • 2021-02-03 23:36
    ALTER TABLE tbl REMOVE PARTITIONING;
    
    0 讨论(0)
  • 2021-02-03 23:40

    If you want to rearrange the data while keeping the partitions,
    you can take a look at REORGANIZE PARTITION and COALESCE PARTITION clauses of ALTER TABLE
    command.
    http://dev.mysql.com/doc/refman/5.1/en/alter-table.html

    0 讨论(0)
提交回复
热议问题