- Simply using a
partition_optionsclause withALTER TABLEon a partitioned table repartitions the table according to the partitioning scheme defined by thepartition_options. This clause always begins withPARTITION BY, and follows the same syntax and other rules as apply to thepartition_optionsclause forCREATE TABLE(see Section 13.1.17, “CREATE TABLESyntax”, for more detailed information), and can also be used to partition an existing table that is not already partitioned. For example, consider a (nonpartitioned) table defined as shown here:CREATE TABLE t1 ( id INT, year_col INT );This table can be partitioned byHASH, using theidcolumn as the partitioning key, into 8 partitions by means of this statement:ALTER TABLE t1 PARTITION BY HASH(id) PARTITIONS 8;The table that results from using anALTER TABLE ... PARTITION BYstatement must follow the same rules as one created usingCREATE TABLE ... PARTITION BY. This includes the rules governing the relationship between any unique keys (including any primary key) that the table might have, and the column or columns used in the partitioning expression, as discussed in Section 18.5.1, “Partitioning Keys, Primary Keys, and Unique Keys”. TheCREATE TABLE ... PARTITION BYrules for specifying the number of partitions also apply toALTER TABLE ... PARTITION BY.ALTER TABLE ... PARTITION BYbecame available in MySQL 5.1.6.Thepartition_definitionclause forALTER TABLE ADD PARTITIONsupports the same options as the clause of the same name for theCREATE TABLEstatement. (See Section 13.1.17, “CREATE TABLESyntax”, for the syntax and description.) Suppose that you have the partitioned table created as shown here:CREATE TABLE t1 ( id INT, year_col INT ) PARTITION BY RANGE (year_col) ( PARTITION p0 VALUES LESS THAN (1991), PARTITION p1 VALUES LESS THAN (1995), PARTITION p2 VALUES LESS THAN (1999) );You can add a new partitionp3to this table for storing values less than2002as follows:ALTER TABLE t1 ADD PARTITION (PARTITION p3 VALUES LESS THAN (2002));
DROP PARTITIONcan be used to drop one or moreRANGEorLISTpartitions. This statement cannot be used withHASHorKEYpartitions; instead, useCOALESCE PARTITION(see below). Any data that was stored in the dropped partitions named in thepartition_nameslist is discarded. For example, given the tablet1defined previously, you can drop the partitions namedp0andp1as shown here:ALTER TABLE t1 DROP PARTITION p0, p1;
NoteDROP PARTITIONdoes not work with tables that use theNDBCLUSTERstorage engine. See Section 18.3.1, “Management ofRANGEandLISTPartitions”, and Section 17.1.6, “Known Limitations of MySQL Cluster”.ADD PARTITIONandDROP PARTITIONdo not currently supportIF [NOT] EXISTS.Renames of partitioned table are supported. You can rename individual partitions indirectly usingALTER TABLE ... REORGANIZE PARTITION; however, this operation makes a copy of the partition's data..COALESCE PARTITIONcan be used with a table that is partitioned byHASHorKEYto reduce the number of partitions bynumber. Suppose that you have created tablet2using the following definition:CREATE TABLE t2 ( name VARCHAR (30), started DATE ) PARTITION BY HASH( YEAR(started) ) PARTITIONS 6;You can reduce the number of partitions used byt2from 6 to 4 using the following statement:ALTER TABLE t2 COALESCE PARTITION 2;
The data contained in the lastnumberpartitions will be merged into the remaining partitions. In this case, partitions 4 and 5 will be merged into the first 4 partitions (the partitions numbered 0, 1, 2, and 3).To change some but not all the partitions used by a partitioned table, you can useREORGANIZE PARTITION. This statement can be used in several ways:- To merge a set of partitions into a single partition. This can be done by naming several partitions in the
partition_nameslist and supplying a single definition forpartition_definition. - To split an existing partition into several partitions. You can accomplish this by naming a single partition for
partition_namesand providing multiplepartition_definitions. - To change the ranges for a subset of partitions defined using
VALUES LESS THANor the value lists for a subset of partitions defined usingVALUES IN. - This statement may also be used without the
option on tables that are automatically partitioned usingpartition_namesINTO (partition_definitions)HASHpartitioning to force redistribution of data. (Currently, onlyNDBCLUSTERtables are automatically partitioned in this way.) This is useful in MySQL Cluster NDB 7.0 and later where, after you have added new MySQL Cluster data nodes online to an existing MySQL Cluster, you wish to redistribute existing MySQL Cluster table data to the new data nodes. In such cases, you should invoke the statement with theONLINEoption; in other words, as shown here:ALTER ONLINE TABLE
tableREORGANIZE PARTITION;You cannot perform other DDL concurrently with online table reorganization—that is, no other DDL statements can be issued while anALTER ONLINE TABLE ... REORGANIZE PARTITIONstatement is executing. For more information about adding MySQL Cluster data nodes online, see Section 17.5.13, “Adding MySQL Cluster Data Nodes Online”.ALTER ONLINE TABLE ... REORGANIZE PARTITIONdoes not work with tables which were created using theMAX_ROWSoption, because it uses the constantMAX_ROWSvalue specified in the originalCREATE TABLEstatement to determine the number of partitions required, so no new partitions are created. Beginning with MySQL Cluster NDB 7.0.32 and MySQL Cluster NDB 7.1.21, you can useALTER ONLINE TABLE ... MAX_ROWS=to increase the maxmimum number of rows for the table; after this,rowsALTER ONLINE TABLE ... REORGANIZE PARTITIONcan use this new, larger value to increase the number of partitions. The value ofrowsmust be greater than the value specified forMAX_ROWSin the originalCREATE TABLEstatement for this to work.Attempting to useREORGANIZE PARTITIONwithout theoption on explicitly partitioned tables results in the error REORGANIZE PARTITION without parameters can only be used on auto-partitioned tables using HASH partitioning.partition_namesINTO (partition_definitions)
NoteFor partitions that have not been explicitly named, MySQL automatically provides the default namesp0,p1,p2, and so on. As of MySQL 5.1.7, the same is true with regard to subpartitions.For more detailed information about and examples ofALTER TABLE ... REORGANIZE PARTITIONstatements, see Section 18.3.1, “Management ofRANGEandLISTPartitions”.
Collection of softwares, news, articles, commands, reports, blah, blah, blah
Search This Blog
Sunday, July 21, 2013
MySQL :: MySQL 5.1 Reference Manual :: 13.1.7.1 ALTER TABLE Partition Operations
MySQL :: MySQL 5.1 Reference Manual :: 13.1.7.1 ALTER TABLE Partition Operations:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment