按哈希或按键分区的表在修改分区设置方面非常相似,而它们在许多方面又与按范围或列表分区的表不同。因此,本节仅讨论按哈希或按键分区的表的修改。有关添加和删除按范围或列表分区的表的分区的讨论,请参见第 26.3.1 节,“范围和列表分区管理”。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 26.3.1, “Management of RANGE and LIST Partitions”.
对于使用 HASH 或 KEY 分区的表,不能像对使用 RANGE 或 LIST 分区的表那样删除分区。然而,可以使用 HASH 来合并 KEY 或 ALTER TABLE ... COALESCE
PARTITION 分区。假设一个包含客户数据的 clients 表被分成了 12 个分区,如下所示创建: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 ALTER TABLE ... COALESCE
PARTITION. Suppose that a clients
table containing data about clients is divided into 12
partitions, created as shown here:
CREATE TABLE clients (
id INT,
fname VARCHAR(30),
lname VARCHAR(30),
signed DATE
)
PARTITION BY HASH( MONTH(signed) )
PARTITIONS 12;
要将分区数量从 12 个减少到 8 个,请执行以下 ALTER
TABLE 语句:To reduce the number of partitions from 12 to 8, execute the
following
ALTER
TABLE statement:
mysql> ALTER TABLE clients COALESCE PARTITION 4;
Query OK, 0 rows affected (0.02 sec)
COALESCE 同样适用于按 HASH、KEY、LINEAR HASH 或 LINEAR KEY 分区的表。下面是一个与前一个类似的例子,唯一的区别是表按 LINEAR KEY 分区: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 4;Query OK, 0 rows affected (0.06 sec) Records: 0 Duplicates: 0 Warnings: 0
COALESCE PARTITION 后面的数字表示要合并到其余分区中的分区数量——换句话说,它是要从表中移除的分区数量。The number following COALESCE PARTITION is
the number of partitions to merge into the remainder—in
other words, it is the number of partitions to remove from the
table.
试图移除比表中更多的分区会导致像这样的错误:Attempting to remove more partitions than are in the table results in an error like this one:
mysql> ALTER TABLE clients COALESCE PARTITION 18;
ERROR 1478 (HY000): Cannot remove all partitions, use DROP TABLE instead
要将 clients 表的分区数量从 12 增加到 18,可使用如下所示的 ALTER TABLE ... ADD PARTITION: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 6;