- 26.3.1 RANGE 和 LIST 分区的管理26.3.1 Management of RANGE and LIST Partitions
- 26.3.2 HASH 和 KEY 分区的管理26.3.2 Management of HASH and KEY Partitions
- 26.3.3 分区与子分区与表的交换26.3.3 Exchanging Partitions and Subpartitions with Tables
- 26.3.4 分区维护26.3.4 Maintenance of Partitions
- 26.3.5 获取分区信息26.3.5 Obtaining Information About Partitions
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.
关于按
RANGE或LIST分区的表的分区管理信息,请参见 第 26.3.1 节,“RANGE 和 LIST 分区管理”。For information about partition management in tables partitioned byRANGEorLIST, see Section 26.3.1, “Management of RANGE and LIST Partitions”.关于管理
HASH和KEY分区的讨论,请参见 第 26.3.2 节,“HASH 和 KEY 分区管理”。For a discussion of managingHASHandKEYpartitions, see Section 26.3.2, “Management of HASH and KEY Partitions”.关于 MySQL 8.4 中提供的获取分区表和分区信息的机制,请参见 第 26.3.5 节,“获取分区信息”。See Section 26.3.5, “Obtaining Information About Partitions”, for a discussion of mechanisms provided in MySQL 8.4 for obtaining information about partitioned tables and partitions.
关于对分区执行维护操作的讨论,请参见 第 26.3.4 节,“分区维护”。For a discussion of performing maintenance operations on partitions, see Section 26.3.4, “Maintenance of Partitions”.
分区表的所有分区必须具有相同数量的子分区;表创建后无法更改子分区。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”.
在给定的 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.