8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

MySQL 8.4 参考手册 / 26 分区 / 26.3 分区管理
中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 94%

26.3 分区管理26.3 Partition Management

There are a number of ways using SQL statements to modify partitioned tables; it is possible to add, drop, redefine, merge, or split existing partitions using the partitioning extensions to the ALTER TABLE statement. There are also ways to obtain information about partitioned tables and partitions. We discuss these topics in the sections that follow.

Note

分区表的所有分区必须具有相同数量的子分区;表创建后无法更改子分区。All partitions of a partitioned table must have the same number of subpartitions; it is not possible to change the subpartitioning once the table has been created.

要更改表的分区方案,只需使用带有partition_options选项的ALTER TABLE语句,该选项的语法与用于创建分区表的CREATE TABLE语法相同;此选项始终以关键字CREATE TABLE开头。假设使用以下PARTITION BY语句创建了一个按范围分区的表:To change a table's partitioning scheme, it is necessary only to use the ALTER TABLE statement with a partition_options option, which has the same syntax as that as used with CREATE TABLE for creating a partitioned table; this option (also) always begins with the keywords PARTITION BY. Suppose that the following CREATE TABLE statement was used to create a table that is partitioned by range:

CREATE TABLE trb3 (id INT, name VARCHAR(50), purchased DATE)
    PARTITION BY RANGE( YEAR(purchased) ) (
        PARTITION p0 VALUES LESS THAN (1990),
        PARTITION p1 VALUES LESS THAN (1995),
        PARTITION p2 VALUES LESS THAN (2000),
        PARTITION p3 VALUES LESS THAN (2005)
    );

要重新分区此表,使其根据 id 列的值按键分为两个分区,可以使用以下语句:To repartition this table so that it is partitioned by key into two partitions using the id column value as the basis for the key, you can use this statement:

ALTER TABLE trb3 PARTITION BY KEY(id) PARTITIONS 2;

这在表结构上的效果与删除表并使用 CREATE TABLE trb3 PARTITION BY KEY(id) PARTITIONS 2; 重新创建表相同。This has the same effect on the structure of the table as dropping the table and re-creating it using CREATE TABLE trb3 PARTITION BY KEY(id) PARTITIONS 2;.

ALTER TABLE ... ENGINE [=] ... 仅更改表使用的存储引擎,并保持表的分区方案不变。该语句仅在目标存储引擎支持分区时才能成功执行。您可以使用 ALTER TABLE ... REMOVE PARTITIONING 来移除表的分区;详见 第 15.1.9 节,“ALTER TABLE 语句”。ALTER TABLE ... ENGINE [=] ... changes only the storage engine used by the table, and leaves the table's partitioning scheme intact. The statement succeeds only if the target storage engine provides partitioning support. You can use ALTER TABLE ... REMOVE PARTITIONING to remove a table's partitioning; see Section 15.1.9, “ALTER TABLE Statement”.

Important

在给定的 PARTITION BY 语句中,只能使用一个 ADD PARTITION、DROP PARTITION、REORGANIZE PARTITION、REORGANIZE PARTITION 或 COALESCE PARTITION 子句。如果您(例如)希望删除一个分区并重新组织表中剩余的分区,则必须在两个单独的 ALTER TABLE 语句中完成(一个使用 ALTER TABLE,然后第二个使用 DROP PARTITION)。Only a single PARTITION BY, ADD PARTITION, DROP PARTITION, REORGANIZE PARTITION, or COALESCE PARTITION clause can be used in a given ALTER TABLE statement. If you (for example) wish to drop a partition and reorganize a table's remaining partitions, you must do so in two separate ALTER TABLE statements (one using DROP PARTITION and then a second one using REORGANIZE PARTITION).

您可以使用 ALTER TABLE ... TRUNCATE PARTITION 删除一个或多个选定分区的所有行。You can delete all rows from one or more selected partitions using ALTER TABLE ... TRUNCATE PARTITION.