Partitioning-related clauses for ALTER TABLE
can be used with partitioned tables for repartitioning, to add, drop, discard, import, merge, and split partitions, and to perform partitioning maintenance.ALTER TABLE
的分区相关子句可以与分区表一起使用,用于重新分区、添加、删除、放弃、导入、合并和拆分分区,以及执行分区维护。
Simply using a 只需在分区表上使用带有partition_options
clause with ALTER TABLE
on a partitioned table repartitions the table according to the partitioning scheme defined by the partition_options
. ALTER TABLE
的partition_options
子句,即可根据partition_options
定义的分区方案重新分区表。This clause always begins with 该子句始终以PARTITION BY
, and follows the same syntax and other rules as apply to the partition_options
clause for CREATE TABLE
(for more detailed information, see Section 13.1.20, “CREATE TABLE Statement”), and can also be used to partition an existing table that is not already partitioned. PARTITION BY
开头,并遵循适用于CREATE TABLE
的partition_options
子句相同的语法和其他规则(有关更多详细信息,请参阅第13.1.20节,“CREATE TABLE语句”),还可用于对尚未分区的现有表进行分区。For example, consider a (nonpartitioned) table defined as shown here:例如,考虑如下定义的(非分区)表:
CREATE TABLE t1 ( id INT, year_col INT );
This table can be partitioned by 通过以下语句,可以使用id列作为分区键,通过HASH
, using the id
column as the partitioning key, into 8 partitions by means of this statement:HASH
将该表分区为8个分区:
ALTER TABLE t1 PARTITION BY HASH(id) PARTITIONS 8;
MySQL supports an MySQL支持通过ALGORITHM
option with [SUB]PARTITION BY [LINEAR] KEY
. [SUB]PARTITION BY [LINEAR] KEY
(按[LINEAR]键进行[SUB]分区)的ALGORITHM
(算法)选项。ALGORITHM=1
causes the server to use the same key-hashing functions as MySQL 5.1 when computing the placement of rows in partitions; ALGORITHM=2
means that the server employs the key-hashing functions implemented and used by default for new KEY
partitioned tables in MySQL 5.5 and later. ALGORITHM=1
导致服务器在计算分区中的行位置时使用与MySQL 5.1相同的密钥散列函数;ALGORITHM=2
意味着服务器使用默认情况下为MySQL 5.5及更高版本中的新密钥分区表实现和使用的密钥散列函数。(Partitioned tables created with the key-hashing functions employed in MySQL 5.5 and later cannot be used by a MySQL 5.1 server.) (使用MySQL 5.5及更高版本中使用的密钥散列函数创建的分区表不能由MySQL 5.1服务器使用。)Not specifying the option has the same effect as using 不指定该选项的效果与使用ALGORITHM=2
. ALGORITHM=2
的效果相同。This option is intended for use chiefly when upgrading or downgrading 此选项主要用于在MySQL 5.1和更高版本的MySQL之间升级或降级[LINEAR] KEY
partitioned tables between MySQL 5.1 and later MySQL versions, or for creating tables partitioned by KEY
or LINEAR KEY
on a MySQL 5.5 or later server which can be used on a MySQL 5.1 server.[LINEAR] KEY
分区表,或在MySQL 5.5或更高版本的服务器上创建可在MySQL 5.1服务器上使用的按KEY
或LINEAR KEY
分区的表。
The table that results from using an 使用ALTER TABLE ... PARTITION BY
statement must follow the same rules as one created using CREATE TABLE ... PARTITION BY
. ALTER TABLE ... PARTITION BY
语句生成的表必须遵循与使用CREATE 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 24.6.1, “Partitioning Keys, Primary Keys, and Unique Keys”. 这包括管理表可能具有的任何唯一键(包括任何主键)与分区表达式中使用的列之间关系的规则,如第24.6.1节,“分区键、主键和唯一键”中所述。The 用于指定分区数的CREATE TABLE ... PARTITION BY
rules for specifying the number of partitions also apply to ALTER TABLE ... PARTITION BY
.CREATE TABLE ... PARTITION BY
规则也适用于ALTER TABLE ... PARTITION BY
。
The partition_definition
clause for ALTER TABLE ADD PARTITION
supports the same options as the clause of the same name for the CREATE TABLE
statement. ALTER TABLE ADD PARTITION
的partition_definition
子句支持与CREATE TABLE
语句的同名子句相同的选项。(See Section 13.1.20, “CREATE TABLE Statement”, for the syntax and description.) (有关语法和说明,请参阅第13.1.20节,“CREATE TABLE语句”。)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 partition 您可以将新分区p3
to this table for storing values less than 2002
as follows:p3
添加到此表中,以存储小于2002的值,如下所示:
ALTER TABLE t1 ADD PARTITION (PARTITION p3 VALUES LESS THAN (2002));
DROP PARTITION
can be used to drop one or more RANGE
or LIST
partitions. DROP PARTITION
可用于删除一个或多个RANGE
分区或LIST
分区。This statement cannot be used with 此语句不能与HASH
or KEY
partitions; instead, use COALESCE PARTITION
(see later in this section). HASH
分区或KEY
分区一起使用;而是使用COALESCE PARTITION
(参见本节后面的内容)。Any data that was stored in the dropped partitions named in the 存储在partition_names
list is discarded. partition_names
列表中命名的已删除分区中的任何数据都将被丢弃。For example, given the table 例如,给定前面定义的表t1
defined previously, you can drop the partitions named p0
and p1
as shown here:t1
,可以删除名为p0
和p1
的分区,如下所示:
ALTER TABLE t1 DROP PARTITION p0, p1;
DROP PARTITION
does not work with tables that use the NDB
storage engine. DROP PARTITION
不适用于使用NDB
存储引擎的表。See Section 24.3.1, “Management of RANGE and LIST Partitions”, and Section 23.1.7, “Known Limitations of NDB Cluster”.请参阅第24.3.1节,“范围和列表分区的管理”和第23.1.7节,“NDB集群的已知限制”。
ADD PARTITION
and DROP PARTITION
do not currently support IF [NOT] EXISTS
.ADD PARTITION
和DROP PARTITION
当前不支持IF [NOT] EXISTS
。
The DISCARD PARTITION ... TABLESPACE
and IMPORT PARTITION ... TABLESPACE
options extend the Transportable Tablespace feature to individual InnoDB
table partitions. DISCARD PARTITION ... TABLESPACE
和IMPORT PARTITION ... TABLESPACE
选项将可传输表空间功能扩展到单个InnoDB
表分区。Each 每个InnoDB
table partition has its own tablespace file (.ibd
file). InnoDB
表分区都有自己的表空间文件(.ibd
文件)。The Transportable Tablespace feature makes it easy to copy the tablespaces from a running MySQL server instance to another running instance, or to perform a restore on the same instance. 可传输表空间特性使得将表空间从一个正在运行的MySQL服务器实例复制到另一个正在运行的实例或在同一实例上执行恢复变得非常容易。Both options take a comma-separated list of one or more partition names. 这两个选项都采用一个或多个分区名称的逗号分隔列表。For example:例如:
ALTER TABLE t1 DISCARD PARTITION p2, p3 TABLESPACE;
ALTER TABLE t1 IMPORT PARTITION p2, p3 TABLESPACE;
When running 当在子分区表上运行DISCARD PARTITION ... TABLESPACE
and IMPORT PARTITION ... TABLESPACE
on subpartitioned tables, both partition and subpartition names are allowed. DISCARD PARTITION ... TABLESPACE
和IMPORT PARTITION ... TABLESPACE
时,既允许使用分区名称,又允许使用子分区名称。When a partition name is specified, subpartitions of that partition are included.指定分区名称时,将包括该分区的子分区。
The Transportable Tablespace feature also supports copying or restoring partitioned 可传输表空间功能还支持复制或恢复已分区的InnoDB
tables. InnoDB
表。For more information, see Section 15.6.1.3, “Importing InnoDB Tables”.有关更多信息,请参阅第15.6.1.3节,“导入InnoDB
表”。
Renames of partitioned tables are supported. 支持对分区表进行重命名。You can rename individual partitions indirectly using 您可以使用ALTER TABLE ... REORGANIZE PARTITION
; however, this operation copies the partition's data.ALTER TABLE ... REORGANIZE PARTITION
间接重命名各个分区;但是,此操作复制分区的数据。
To delete rows from selected partitions, use the 要从选定分区中删除行,请使用TRUNCATE PARTITION
option. TRUNCATE PARTITION
选项。This option takes a list of one or more comma-separated partition names. 此选项接受一个或多个以逗号分隔的分区名称的列表。Consider the table 考虑由该语句创建的表t1
created by this statement:t1
:
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), PARTITION p3 VALUES LESS THAN (2003), PARTITION p4 VALUES LESS THAN (2007) );
To delete all rows from partition 要删除分区p0
, use the following statement:p0
中的所有行,请使用以下语句:
ALTER TABLE t1 TRUNCATE PARTITION p0;
The statement just shown has the same effect as the following 刚才显示的语句与以下DELETE
statement:DELETE
语句具有相同的效果:
DELETE FROM t1 WHERE year_col < 1991;
When truncating multiple partitions, the partitions do not have to be contiguous: This can greatly simplify delete operations on partitioned tables that would otherwise require very complex 在截断多个分区时,分区不必是连续的:这可以大大简化分区表上的删除操作,否则,如果使用WHERE
conditions if done with DELETE
statements. DELETE
语句,则需要非常复杂的WHERE
条件。For example, this statement deletes all rows from partitions 例如,此语句删除分区p1
and p3
:p1
和p3
中的所有行:
ALTER TABLE t1 TRUNCATE PARTITION p1, p3;
An equivalent 此处显示了等效的DELETE
statement is shown here:DELETE
语句:
DELETE FROM t1 WHERE (year_col >= 1991 AND year_col < 1995) OR (year_col >= 2003 AND year_col < 2007);
If you use the 如果使用ALL
keyword in place of the list of partition names, the statement acts on all table partitions.ALL
关键字代替分区名称列表,则该语句将作用于所有表分区。
TRUNCATE PARTITION
merely deletes rows; it does not alter the definition of the table itself, or of any of its partitions.TRUNCATE PARTITION
只删除行;它不会改变表本身或其任何分区的定义。
To verify that the rows were dropped, check the 要验证行是否已被删除,请使用以下查询检查INFORMATION_SCHEMA.PARTITIONS
table, using a query such as this one:INFORMATION_SCHEMA.PARTITIONS
表:
SELECT PARTITION_NAME, TABLE_ROWS FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_NAME = 't1';
COALESCE PARTITION
can be used with a table that is partitioned by HASH
or KEY
to reduce the number of partitions by number
. COALESCE PARTITION
可以与按哈希或键分区的表一起使用,以按数量减少分区的数量。Suppose that you have created table 假设您创建了表t2
as follows:t2
,如下所示:
CREATE TABLE t2 ( name VARCHAR (30), started DATE ) PARTITION BY HASH( YEAR(started) ) PARTITIONS 6;
To reduce the number of partitions used by 要将t2
from 6 to 4, use the following statement:t2
使用的分区数从6减少到4,请使用以下语句:
ALTER TABLE t2 COALESCE PARTITION 2;
The data contained in the last 最后number
partitions is merged into the remaining partitions. number
分区中包含的数据将合并到其余分区中。In this case, partitions 4 and 5 are merged into the first 4 partitions (the partitions numbered 0, 1, 2, and 3).在这种情况下,分区4和5合并到前4个分区中(编号为0、1、2和3的分区)。
To change some but not all the partitions used by a partitioned table, you can use 要更改分区表使用的部分分区,但不是全部分区,可以使用REORGANIZE PARTITION
. REORGANIZE PARTITION
。This statement can be used in several ways:此语句可通过多种方式使用:
To merge a set of partitions into a single partition. 要将一组分区合并为单个分区。This is done by naming several partitions in the 这是通过在partition_names
list and supplying a single definition for partition_definition
.partition_names
列表中命名多个分区并为partition_definition
提供单个定义来实现的。
To split an existing partition into several partitions. 要将现有分区拆分为多个分区。Accomplish this by naming a single partition for 通过为partition_names
and providing multiple partition_definitions
.partition_names
命名单个分区并提供多个partition_definitions
来实现这一点。
To change the ranges for a subset of partitions defined using 要更改使用VALUES LESS THAN
or the value lists for a subset of partitions defined using VALUES IN
.VALUES LESS THAN
定义的分区子集的范围,或更改使用VALUES IN
定义的分区子集的值列表。
For partitions that have not been explicitly named, MySQL automatically provides the default names 对于未显式命名的分区,MySQL自动提供默认名称p0
, p1
, p2
, and so on. p0
、p1
、p2
等。The same is true with regard to subpartitions.子分区也是如此。
For more detailed information about and examples of 有关ALTER TABLE ... REORGANIZE PARTITION
statements, see Section 24.3.1, “Management of RANGE and LIST Partitions”.ALTER TABLE ... REORGANIZE PARTITION
语句的更多详细信息和示例,请参阅第24.3.1节,“范围和列表分区的管理”。
To exchange a table partition or subpartition with a table, use the 要与表交换表分区或子分区,请使用ALTER TABLE ... EXCHANGE PARTITION
statement—that is, to move any existing rows in the partition or subpartition to the nonpartitioned table, and any existing rows in the nonpartitioned table to the table partition or subpartition.ALTER TABLE ... EXCHANGE PARTITION
语句,即将分区或子分区中的任何现有行移动到非分区表,并将非分区表中的任何现有行移动到表分区或子分区。
For usage information and examples, see Section 24.3.3, “Exchanging Partitions and Subpartitions with Tables”.有关使用信息和示例,请参阅第24.3.3节,“用表交换分区和子分区”。
Several options provide partition maintenance and repair functionality analogous to that implemented for nonpartitioned tables by statements such as 有几个选项提供分区维护和修复功能,类似于CHECK TABLE
and REPAIR TABLE
(which are also supported for partitioned tables; for more information, see Section 13.7.3, “Table Maintenance Statements”). CHECK TABLE
和REPAIR TABLE
等语句为非分区表实现的功能(分区表也支持这些功能;有关更多信息,请参阅第13.7.3节,“表维护语句”)。These include 这些包括ANALYZE PARTITION
, CHECK PARTITION
, OPTIMIZE PARTITION
, REBUILD PARTITION
, and REPAIR PARTITION
. ANALYZE PARTITION
(分析分区)、CHECK PARTITION
(检查分区)、OPTIMIZE PARTITION
(优化分区)、REBUILD PARTITION
(重建分区)和REPAIR PARTITION
(修复分区)。Each of these options takes a 这些选项中的每一个都采用一个partition_names
clause consisting of one or more names of partitions, separated by commas. partition_names
子句,该子句由一个或多个分区名称组成,用逗号分隔。The partitions must already exist in the target table. 分区必须已经存在于目标表中。You can also use the 您还可以使用ALL
keyword in place of partition_names
, in which case the statement acts on all table partitions. ALL
关键字代替partition_names
,在这种情况下,该语句作用于所有表分区。For more information and examples, see Section 24.3.4, “Maintenance of Partitions”.有关更多信息和示例,请参阅第24.3.4节,“分区维护”。
InnoDB
does not currently support per-partition optimization; ALTER TABLE ... OPTIMIZE PARTITION
causes the entire table to rebuilt and analyzed, and an appropriate warning to be issued. InnoDB
目前不支持每个分区的优化;ALTER TABLE ... OPTIMIZE PARTITION
会导致重建和分析整个表,并发出相应的警告。(Bug #11751825, Bug #42822) To work around this problem, use 要绕过此问题,请改用ALTER TABLE ... REBUILD PARTITION
and ALTER TABLE ... ANALYZE PARTITION
instead.ALTER TABLE ... REBUILD PARTITION
和ALTER TABLE ... ANALYZE PARTITION
。
The 未分区的表不支持ANALYZE PARTITION
, CHECK PARTITION
, OPTIMIZE PARTITION
, and REPAIR PARTITION
options are not supported for tables which are not partitioned.ANALYZE PARTITION
(分析分区)、CHECK PARTITION
(检查分区)、OPTIMIZE PARTITION
(优化分区)和REPAIR PARTITION
选项。
REMOVE PARTITIONING
enables you to remove a table's partitioning without otherwise affecting the table or its data. REMOVE PARTITIONING
使您能够移除表的分区,而不会影响表或其数据。This option can be combined with other 此选项可以与其他ALTER TABLE
options such as those used to add, drop, or rename columns or indexes.ALTER TABLE
选项组合使用,例如用于添加、删除或重命名列或索引的选项。
Using the 将ENGINE
option with ALTER TABLE
changes the storage engine used by the table without affecting the partitioning. ENGINE
选项与ALTER TABLE
一起使用会更改表使用的存储引擎,而不会影响分区。The target storage engine must provide its own partitioning handler. 目标存储引擎必须提供自己的分区处理程序。Only the 只有InnoDB
and NDB
storage engines have native partitioning handlers; NDB
is not currently supported in MySQL 8.0.InnoDB
和NDB
存储引擎具有本机分区处理程序;MySQL 8.0目前不支持NDB
。
It is possible for an ALTER TABLE
statement to contain a PARTITION BY
or REMOVE PARTITIONING
clause in an addition to other alter specifications, but the PARTITION BY
or REMOVE PARTITIONING
clause must be specified last after any other specifications.ALTER TABLE
语句可以在其他alter规范文档之外包含PARTITION BY
子句或REMOVE PARTITIONING
子句,但PARTITION BY
子句或REMOVE PARTITIONING
子句必须在任何其他规范文档之后最后指定。
The ADD PARTITION
, DROP PARTITION
, COALESCE PARTITION
, REORGANIZE PARTITION
, ANALYZE PARTITION
, CHECK PARTITION
, and REPAIR PARTITION
options cannot be combined with other alter specifications in a single ALTER TABLE
, since the options just listed act on individual partitions. ADD PARTITION
(添加分区)、DROP PARTITION
(删除分区)、COALESCE PARTITION
(合并分区)、REORGANIZE PARTITION
(重新组织分区)、ANALYZE PARTITION
(分析分区)、CHECK PARTITION
(检查分区)和REPAIR PARTITION
(修复分区)选项不能与单个ALTER TABLE
中的其他alter规范文档组合,因为刚才列出的选项作用于各个分区。For more information, see Section 13.1.9.1, “ALTER TABLE Partition Operations”.有关更多信息,请参阅第13.1.9.1节,“更改表分区操作”。
Only a single instance of any one of the following options can be used in a given 在给定的ALTER TABLE
statement: PARTITION BY
, ADD PARTITION
, DROP PARTITION
, TRUNCATE PARTITION
, EXCHANGE PARTITION
, REORGANIZE PARTITION
, or COALESCE PARTITION
, ANALYZE PARTITION
, CHECK PARTITION
, OPTIMIZE PARTITION
, REBUILD PARTITION
, REMOVE PARTITIONING
.ALTER TABLE
语句中只能使用以下任一选项的单个实例:PARTITION BY
、ADD PARTITION
、DROP PARTITION
、TRUNCATE PARTITION
、EXCHANGE PARTITION
、REORGANIZE PARTITION
或COALESCE PARTITION
、ANALYZE PARTITION
、CHECK PARTITION
、OPTIMIZE PARTITION
、REBUILD PARTITION
、REMOVE PARTITIONING
。
For example, the following two statements are invalid:例如,以下两条语句无效:
ALTER TABLE t1 ANALYZE PARTITION p1, ANALYZE PARTITION p2; ALTER TABLE t1 ANALYZE PARTITION p1, CHECK PARTITION p2;
In the first case, you can analyze partitions 在第一种情况下,您可以使用一条语句和一个p1
and p2
of table t1
concurrently using a single statement with a single ANALYZE PARTITION
option that lists both of the partitions to be analyzed, like this:ANALYZE PARTITION
选项同时分析表t1
的分区p1
和p2
,该选项列出了要分析的两个分区,如下所示:
ALTER TABLE t1 ANALYZE PARTITION p1, p2;
In the second case, it is not possible to perform 在第二种情况下,不可能同时对同一表的不同分区执行ANALYZE
and CHECK
operations on different partitions of the same table concurrently. ANALYZE
和CHECK
操作。Instead, you must issue two separate statements, like this:相反,您必须发布两个单独的声明,如下所示:
ALTER TABLE t1 ANALYZE PARTITION p1; ALTER TABLE t1 CHECK PARTITION p2;
子分区当前不支持REBUILD
operations are currently unsupported for subpartitions. REBUILD
操作。The 子分区明确禁止使用REBUILD
keyword is expressly disallowed with subpartitions, and causes ALTER TABLE
to fail with an error if so used.REBUILD
关键字,如果使用REBUILD
关键字,会导致ALTER TABLE
失败并出现错误。
当要检查或修复的分区包含任何重复的密钥错误时,CHECK PARTITION
and REPAIR PARTITION
operations fail when the partition to be checked or repaired contains any duplicate key errors.CHECK PARTITION
和REPAIR PARTITION
操作失败。
For more information about these statements, see Section 24.3.4, “Maintenance of Partitions”.有关这些语句的更多信息,请参阅第24.3.4节,“分区的维护”。