Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to drop partition without dropping data in MySQL?

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  partition by range(id) ( partition p0 values less than (10000), partition p0 values less than (20000), partition p0 values less than max value); 

Not exactly like above but similar to that....

Now assume that my table has 200000 rows and now I want to remove partitions on the table and reorganize them in accordance to requirement without MAX VALUE in it.

Can any one help me to rearrange partition without dropping data or dropping table and recreating it ?

like image 571
vidyadhar Avatar asked Jan 07 '13 08:01

vidyadhar


People also ask

How do I delete a partition in MySQL?

Use ALTER TABLE ... REMOVE PARTITIONING to remove a table's partitioning. See Section 13.1. 7, “ALTER TABLE Statement”. Only a single PARTITION BY , ADD PARTITION , DROP PARTITION , REORGANIZE PARTITION , or COALESCE PARTITION clause can be used in a given ALTER TABLE statement.

Does drop partition delete data?

Data itself are stored in files in the partition location(folder). If you drop partition of external table, the location remain untouched, but unmounted as partition (metadata about this partition is deleted). You can have few versions of partition location unmounted (for example previous versions).

How do I drop a partition in SQL?

The ALTER TABLE… DROP PARTITION command deletes a partition and any data stored on that partition. The ALTER TABLE… DROP PARTITION command can drop partitions of a LIST or RANGE partitioned table.

Can we create partition on existing table in MySQL?

This table can be partitioned by HASH , using the id column as the partitioning key, into 8 partitions by means of this statement: ALTER TABLE t1 PARTITION BY HASH(id) PARTITIONS 8; MySQL supports an ALGORITHM option with [SUB]PARTITION BY [LINEAR] KEY .


2 Answers

ALTER TABLE tbl REMOVE PARTITIONING; 
like image 189
user2643317 Avatar answered Sep 23 '22 16:09

user2643317


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.

like image 39
Michel Feldheim Avatar answered Sep 23 '22 16:09

Michel Feldheim