17.3.2. Management of HASH
and KEY
Partitions
Tables which are partitioned by hash or by key are very similar
to one another with regard to making changes in a partitioning
setup, and both differ in a number of ways from tables which
have been partitioned by range or list. For that reason, this
section addresses the modification of tables partitioned by hash
or by key only. For a discussion of adding and dropping of
partitions of tables that are partitioned by range or list, see
Section 17.3.1, “Management of RANGE
and LIST
Partitions”.
You cannot drop partitions from tables that are partitioned by
HASH
or KEY
in the same
way that you can from tables that are partitioned by
RANGE
or LIST
. However,
you can merge HASH
or KEY
partitions using the ALTER TABLE ... COALESCE
PARTITION
command. For example, suppose that you have
a table containing data about clients, which is divided into
twelve partitions. The clients
table is
defined as shown here:
CREATE TABLE clients (
id INT,
fname VARCHAR(30),
lname VARCHAR(30),
signed DATE
)
PARTITION BY HASH( MONTH(signed) )
PARTITIONS 12;
To reduce the number of partitions from twelve to six, execute
the following ALTER TABLE
command:
mysql> ALTER TABLE clients COALESCE PARTITION 6;
Query OK, 0 rows affected (0.02 sec)
COALESCE
works equally well with tables that
are partitioned by HASH
,
KEY
, LINEAR HASH
, or
LINEAR KEY
. Here is an example similar to the
previous one, differing only in that the table is partitioned by
LINEAR KEY
:
mysql> CREATE TABLE clients_lk (
-> id INT,
-> fname VARCHAR(30),
-> lname VARCHAR(30),
-> signed DATE
-> )
-> PARTITION BY LINEAR KEY(signed)
-> PARTITIONS 12;
Query OK, 0 rows affected (0.03 sec)
mysql> ALTER TABLE clients_lk COALESCE PARTITION 6;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
COALESCE
cannot be used to increase the
number of partitions, and if you attempt to do so, the result is
an error like the one shown:
mysql> ALTER TABLE clients COALESCE PARTITION 18;
ERROR 1478 (HY000): Cannot remove all partitions, use DROP TABLE instead
To increase the number of partitions for the
clients
table from 12 to 18. use
ALTER TABLE ... ADD PARTITION
as shown here:
ALTER TABLE clients ADD PARTITION PARTITIONS 18;
Note: ALTER TABLE ...
REORGANIZE PARTITION
cannot be used with tables that
are partitioned by HASH
or
KEY
.