本节中提供了有关 DDL 操作的在线支持详情、语法示例和使用说明,具体主题如下。Online support details, syntax examples, and usage notes for DDL operations are provided under the following topics in this section.
下表提供了在线 DDL 对索引操作的支持概述。星号表示附加信息、例外或依赖关系。详情请参见 语法和使用说明。The following table provides an overview of online DDL support for index operations. An asterisk indicates additional information, an exception, or a dependency. For details, see Syntax and Usage Notes.
表 17.15 索引操作的在线 DDL 支持Table 17.15 Online DDL Support for Index Operations
| 操作Operation | 即时Instant | 就地In Place | 重建表Rebuilds Table | 允许并发 DMLPermits Concurrent DML | 仅修改元数据Only Modifies Metadata |
|---|---|---|---|---|---|
| 创建或添加二级索引Creating or adding a secondary index | 否No | 是Yes | 否No | 是Yes | 否No |
| 删除索引Dropping an index | 否No | 是Yes | 否No | 是Yes | 是Yes |
| 重命名索引Renaming an index | 否No | 是Yes | 否No | 是Yes | 是Yes |
添加 FULLTEXT 索引Adding a FULLTEXT index |
否No | 是*Yes* | 否*No* | 否No | 否No |
添加 SPATIAL 索引Adding a SPATIAL index |
否No | 是Yes | 否No | 否No | 否No |
| 更改索引类型Changing the index type | 是Yes | 是Yes | 否No | 是Yes | 是Yes |
语法和使用说明Syntax and Usage Notes
创建或添加二级索引Creating or adding a secondary index
CREATE INDEX
nameONtable(col_list);ALTER TABLE
tbl_nameADD INDEXname(col_list);在创建索引的过程中,表仍然可以进行读写操作。
CREATE INDEX语句仅在访问表的所有事务完成后才会结束,这样索引的初始状态就能反映表的最新内容。The table remains available for read and write operations while the index is being created. TheCREATE INDEXstatement only finishes after all transactions that are accessing the table are completed, so that the initial state of the index reflects the most recent contents of the table.支持在线 DDL 添加二级索引意味着,通常你可以通过先创建没有二级索引的表,然后在数据加载完成后再添加二级索引,从而加快创建和加载表及相关索引的总体过程。Online DDL support for adding secondary indexes means that you can generally speed the overall process of creating and loading a table and associated indexes by creating the table without secondary indexes, then adding secondary indexes after the data is loaded.
新创建的二级索引只包含在
CREATE INDEX或ALTER TABLE语句执行完成时表中的已提交数据。它不包含任何未提交的值、值的旧版本,或已标记为删除但尚未从旧索引中移除的值。A newly created secondary index contains only the committed data in the table at the time theCREATE INDEXorALTER TABLEstatement finishes executing. It does not contain any uncommitted values, old versions of values, or values marked for deletion but not yet removed from the old index.一些因素会影响此操作的性能、空间使用和语义。有关详细信息,请参见第 17.12.8 节,“在线 DDL 限制”。Some factors affect the performance, space usage, and semantics of this operation. For details, see Section 17.12.8, “Online DDL Limitations”.
删除索引Dropping an index
DROP INDEX
nameONtable;ALTER TABLE
tbl_nameDROP INDEXname;在索引被删除的过程中,表仍然可以进行读写操作。
DROP INDEX语句仅在所有访问该表的事务完成后才会执行完毕,从而使索引的初始状态反映表的最新内容。The table remains available for read and write operations while the index is being dropped. TheDROP INDEXstatement only finishes after all transactions that are accessing the table are completed, so that the initial state of the index reflects the most recent contents of the table.重命名索引Renaming an index
ALTER TABLE
tbl_nameRENAME INDEXold_index_nameTOnew_index_name, ALGORITHM=INPLACE, LOCK=NONE;添加
FULLTEXT索引Adding aFULLTEXTindexCREATE FULLTEXT INDEX
nameON table(column);如果没有用户定义的
FTS_DOC_ID列,添加第一个FULLTEXT索引会重建表。可以在不重建表的情况下添加额外的FULLTEXT索引。Adding the firstFULLTEXTindex rebuilds the table if there is no user-definedFTS_DOC_IDcolumn. AdditionalFULLTEXTindexes may be added without rebuilding the table.添加
SPATIAL索引Adding aSPATIALindexCREATE TABLE geom (g GEOMETRY NOT NULL); ALTER TABLE geom ADD SPATIAL INDEX(g), ALGORITHM=INPLACE, LOCK=SHARED;
更改索引类型(
USING {BTREE | HASH})Changing the index type (USING {BTREE | HASH})ALTER TABLE
tbl_nameDROP INDEX i1, ADD INDEX i1(key_part,...) USING BTREE, ALGORITHM=INSTANT;
下表提供了在线 DDL 对主键操作支持的概览。星号表示额外的信息、例外或依赖。请参阅 语法和使用说明。The following table provides an overview of online DDL support for primary key operations. An asterisk indicates additional information, an exception, or a dependency. See Syntax and Usage Notes.
表 17.16 主键操作的在线 DDL 支持Table 17.16 Online DDL Support for Primary Key Operations
| 操作Operation | 即时Instant | 就地In Place | 重建表Rebuilds Table | 允许并发 DMLPermits Concurrent DML | 仅修改元数据Only Modifies Metadata |
|---|---|---|---|---|---|
| 添加主键Adding a primary key | 否No | 是*Yes* | 是*Yes* | 是Yes | 否No |
| 删除主键Dropping a primary key | 否No | 否No | 是Yes | 否No | 否No |
| 删除一个主键并添加另一个Dropping a primary key and adding another | 否No | 是Yes | 是Yes | 是Yes | 否No |
语法和使用说明Syntax and Usage Notes
添加主键Adding a primary key
ALTER TABLE
tbl_nameADD PRIMARY KEY (column), ALGORITHM=INPLACE, LOCK=NONE;就地重建表。数据会被大幅重组,因此这是一个开销很大的操作。
ALGORITHM=INPLACE在某些条件下不允许使用,如果列必须转换为NOT NULL。Rebuilds the table in place. Data is reorganized substantially, making it an expensive operation.ALGORITHM=INPLACEis not permitted under certain conditions if columns have to be converted toNOT NULL.重构 聚集索引 总是需要复制表数据。因此,最好在创建表时定义 主键,而不是在之后执行
ALTER TABLE ... ADD PRIMARY KEY。Restructuring the clustered index always requires copying of table data. Thus, it is best to define the primary key when you create a table, rather than issuingALTER TABLE ... ADD PRIMARY KEYlater.当你创建一个
UNIQUE或PRIMARY KEY索引时,MySQL 必须做一些额外的工作。对于UNIQUE索引,MySQL 会检查表中是否存在键的重复值。对于PRIMARY KEY索引,MySQL 还会检查PRIMARY KEY列中是否包含NULL。When you create aUNIQUEorPRIMARY KEYindex, MySQL must do some extra work. ForUNIQUEindexes, MySQL checks that the table contains no duplicate values for the key. For aPRIMARY KEYindex, MySQL also checks that none of thePRIMARY KEYcolumns contains aNULL.当您使用
ALGORITHM=COPY子句添加主键时,MySQL 会将相关列中的NULL值转换为默认值:数字为 0,基于字符的列和 BLOB 为空字符串,DATETIME为 0000-00-00 00:00:00。这是一种非标准行为,Oracle 建议您不要依赖。仅当SQL_MODE设置包含strict_trans_tables或strict_all_tables标志时,才允许使用ALGORITHM=INPLACE添加主键;当SQL_MODE设置为严格模式时,允许使用ALGORITHM=INPLACE,但如果请求的主键列包含NULL值,语句仍可能失败。ALGORITHM=INPLACE的行为更符合标准规范。When you add a primary key using theALGORITHM=COPYclause, MySQL convertsNULLvalues in the associated columns to default values: 0 for numbers, an empty string for character-based columns and BLOBs, and 0000-00-00 00:00:00 forDATETIME. This is a non-standard behavior that Oracle recommends you not rely on. Adding a primary key usingALGORITHM=INPLACEis only permitted when theSQL_MODEsetting includes thestrict_trans_tablesorstrict_all_tablesflags; when theSQL_MODEsetting is strict,ALGORITHM=INPLACEis permitted, but the statement can still fail if the requested primary key columns containNULLvalues. TheALGORITHM=INPLACEbehavior is more standard-compliant.如果你创建一个没有主键的表,
InnoDB会为你选择一个主键,这可能是第一个在NOT NULL列上定义的UNIQUE键,或者是系统生成的键。为了避免不确定性及额外隐藏列可能带来的空间需求,请在CREATE TABLE语句中指定PRIMARY KEY子句。If you create a table without a primary key,InnoDBchooses one for you, which can be the firstUNIQUEkey defined onNOT NULLcolumns, or a system-generated key. To avoid uncertainty and the potential space requirement for an extra hidden column, specify thePRIMARY KEYclause as part of theCREATE TABLEstatement.MySQL 通过将原始表中的现有数据复制到具有所需索引结构的临时表来创建新的聚簇索引。一旦数据完全复制到临时表中,原始表将被重命名为不同的临时表名。包含新聚簇索引的临时表被重命名为原始表的名称,并且原始表从数据库中删除。MySQL creates a new clustered index by copying the existing data from the original table to a temporary table that has the desired index structure. Once the data is completely copied to the temporary table, the original table is renamed with a different temporary table name. The temporary table comprising the new clustered index is renamed with the name of the original table, and the original table is dropped from the database.
适用于二级索引操作的在线性能增强不适用于主键索引。InnoDB 表的行存储在一个聚集索引中,该索引是基于主键组织的,形成一些数据库系统所称的“索引组织表”。由于表结构与主键紧密相关,因此重新定义主键仍然需要复制数据。The online performance enhancements that apply to operations on secondary indexes do not apply to the primary key index. The rows of an InnoDB table are stored in a clustered index organized based on the primary key, forming what some database systems call an “index-organized table”. Because the table structure is closely tied to the primary key, redefining the primary key still requires copying the data.
当对主键进行操作时使用
ALGORITHM=INPLACE,即使数据仍然被复制,它仍然比使用ALGORITHM=COPY更高效,因为:When an operation on the primary key usesALGORITHM=INPLACE, even though the data is still copied, it is more efficient than usingALGORITHM=COPYbecause:对于
ALGORITHM=INPLACE,不需要撤销日志或相关的重做日志。这些操作会给使用ALGORITHM=COPY的 DDL 语句增加开销。No undo logging or associated redo logging is required forALGORITHM=INPLACE. These operations add overhead to DDL statements that useALGORITHM=COPY.辅助索引条目是预先排序的,因此可以按顺序加载。The secondary index entries are pre-sorted, and so can be loaded in order.
不使用更改缓冲区,因为对辅助索引没有随机访问插入操作。The change buffer is not used, because there are no random-access inserts into the secondary indexes.
删除主键Dropping a primary key
ALTER TABLE
tbl_nameDROP PRIMARY KEY, ALGORITHM=COPY;只有
ALGORITHM=COPY支持在同一个ALTER TABLE语句中删除主键而不添加新的主键。OnlyALGORITHM=COPYsupports dropping a primary key without adding a new one in the sameALTER TABLEstatement.删除一个主键并添加另一个Dropping a primary key and adding another
ALTER TABLE
tbl_nameDROP PRIMARY KEY, ADD PRIMARY KEY (column), ALGORITHM=INPLACE, LOCK=NONE;数据会被大量重组,使其成为一个开销较大的操作。Data is reorganized substantially, making it an expensive operation.
下表提供了列操作的在线 DDL 支持概览。星号表示附加信息、例外或依赖。详情请参见 语法和使用说明。The following table provides an overview of online DDL support for column operations. An asterisk indicates additional information, an exception, or a dependency. For details, see Syntax and Usage Notes.
表 17.17 列操作的在线 DDL 支持Table 17.17 Online DDL Support for Column Operations
| 操作Operation | 即时Instant | 就地In Place | 重建表Rebuilds Table | 允许并发 DMLPermits Concurrent DML | 仅修改元数据Only Modifies Metadata |
|---|---|---|---|---|---|
| 添加列Adding a column | 是*Yes* | 是Yes | 否*No* | 是*Yes* | 是Yes |
| 删除列Dropping a column | 是*Yes* | 是Yes | 是Yes | 是Yes | 是Yes |
| 重命名列Renaming a column | 是*Yes* | 是Yes | 否No | 是*Yes* | 是Yes |
| 重新排序列Reordering columns | 否No | 是Yes | 是Yes | 是Yes | 否No |
| 设置列默认值Setting a column default value | 是Yes | 是Yes | 否No | 是Yes | 是Yes |
| 更改列数据类型Changing the column data type | 否No | 否No | 是Yes | 否No | 否No |
扩展 VARCHAR 列大小Extending VARCHAR column size |
否No | 是Yes | 否No | 是Yes | 是Yes |
| 删除列默认值Dropping the column default value | 是Yes | 是Yes | 否No | 是Yes | 是Yes |
| 更改自增值Changing the auto-increment value | 否No | 是Yes | 否No | 是Yes | 否*No* |
将列设置为 NULLMaking a column NULL |
否No | 是Yes | 是*Yes* | 是Yes | 否No |
将列设置为 NOT NULLMaking a column NOT NULL |
否No | 是*Yes* | 是*Yes* | 是Yes | 否No |
修改 ENUM 或 SET 列的定义Modifying the definition of an ENUM or
SET column |
是Yes | 是Yes | 否No | 是Yes | 是Yes |
语法和使用说明Syntax and Usage Notes
添加列Adding a column
ALTER TABLE
tbl_nameADD COLUMNcolumn_namecolumn_definition, ALGORITHM=INSTANT;INSTANT是 MySQL 8.4 的默认算法。INSTANTis the default algorithm in MySQL 8.4.当
INSTANT算法添加列时,适用以下限制:The following limitations apply when theINSTANTalgorithm adds a column:一个语句不能将添加列与不支持
INSTANT算法的其他ALTER TABLE操作组合在一起。A statement cannot combine the addition of a column with otherALTER TABLEactions that do not support theINSTANTalgorithm.INSTANT算法可以在表中的任何位置添加列。TheINSTANTalgorithm can add a column at any position in the table.不能向使用
ROW_FORMAT=COMPRESSED的表、具有FULLTEXT索引的表、位于数据字典表空间的表或临时表中添加列。临时表仅支持ALGORITHM=COPY。Columns cannot be added to tables that useROW_FORMAT=COMPRESSED, tables with aFULLTEXTindex, tables that reside in the data dictionary tablespace, or temporary tables. Temporary tables only supportALGORITHM=COPY.当
INSTANT算法添加列时,MySQL 会检查行大小,如果添加超过限制,会抛出以下错误。MySQL checks the row size when theINSTANTalgorithm adds a column, and throws the following error if the addition exceeds the limit.错误 4092 (HY000):无法使用 ALGORITHM=INSTANT 添加列,因为此后最大可能行大小超过最大允许行大小。尝试使用 ALGORITHM=INPLACE/COPY。ERROR 4092 (HY000): Column can't be added with ALGORITHM=INSTANT as after this max possible row size crosses max permissible row size. Try ALGORITHM=INPLACE/COPY.
使用
INSTANT算法添加列后,表的内部表示中的最大列数不能超过 1022。错误消息为:The maximum number of columns in the internal representation of the table cannot exceed 1022 after column addition with theINSTANTalgorithm. The error message is:错误 4158 (HY000):无法再使用 ALGORITHM=INSTANT 向
tbl_name添加列。请尝试使用 ALGORITHM=INPLACE/COPYERROR 4158 (HY000): Column can't be added totbl_namewith ALGORITHM=INSTANT anymore. Please try ALGORITHM=INPLACE/COPYINSTANT算法无法向系统架构表添加或删除列,例如内部的mysql表。TheINSTANTalgorithm can not add or drop columns to system schema tables, such as the internalmysqltable.具有函数索引的列不能使用
INSTANT算法删除。A column with a functional index cannot be dropped using theINSTANTalgorithm.
在同一个
ALTER TABLE语句中可以添加多个列。例如:Multiple columns may be added in the sameALTER TABLEstatement. For example:ALTER TABLE t1 ADD COLUMN c2 INT, ADD COLUMN c3 INT, ALGORITHM=INSTANT;
在每次执行
ALTER TABLE ... ALGORITHM=INSTANT操作后,如果该操作增加一个或多个列、删除一个或多个列,或在同一次操作中同时增加和删除一个或多个列,都会创建一个新的行版本。INFORMATION_SCHEMA.INNODB_TABLES.TOTAL_ROW_VERSIONS列用于跟踪表的行版本数量。每次立即添加或删除列时,该值都会增加。初始值为 0。A new row version is created after eachALTER TABLE ... ALGORITHM=INSTANToperation that adds one or more columns, drops one or more columns, or adds and drops one or more columns in the same operation. TheINFORMATION_SCHEMA.INNODB_TABLES.TOTAL_ROW_VERSIONScolumn tracks the number of row versions for a table. The value is incremented each time a column is instantly added or dropped. The initial value is 0.mysql> SELECT NAME, TOTAL_ROW_VERSIONS FROM INFORMATION_SCHEMA.INNODB_TABLES WHERE NAME LIKE 'test/t1'; +---------+--------------------+ | NAME | TOTAL_ROW_VERSIONS | +---------+--------------------+ | test/t1 | 0 | +---------+--------------------+当使用表重建操作
ALTER TABLE或OPTIMIZE TABLE重建一个即时添加或删除列的表时,TOTAL_ROW_VERSIONS值将重置为 0。允许的最大行版本数量为 64(在 MySQL 9.1.0 中为 255),因为每个行版本都需要额外的表元数据空间。当达到行版本限制时,使用ALGORITHM=INSTANT的ADD COLUMN和DROP COLUMN操作将被拒绝,并显示建议使用COPY或INPLACE算法重建表的错误消息。When a table with instantly added or dropped columns is rebuilt by table-rebuildingALTER TABLEorOPTIMIZE TABLEoperation, theTOTAL_ROW_VERSIONSvalue is reset to 0. The maximum number of row versions permitted is 64 (255 as of MySQL 9.1.0), as each row version requires additional space for table metadata. When the row version limit is reached,ADD COLUMNandDROP COLUMNoperations usingALGORITHM=INSTANTare rejected with an error message that recommends rebuilding the table using theCOPYorINPLACEalgorithm.错误 4092 (HY000):表 test/t1 达到最大行版本。无法再立即添加或删除列。请使用 COPY/INPLACE。ERROR 4092 (HY000): Maximum row versions reached for table test/t1. No more columns can be added or dropped instantly. Please use COPY/INPLACE.
以下
INFORMATION_SCHEMA列提供对立即添加的列的附加元数据。有关更多信息,请参考这些列的描述。参见 第 28.4.9 节,“INFORMATION_SCHEMA INNODB_COLUMNS 表” 和 第 28.4.23 节,“INFORMATION_SCHEMA INNODB_TABLES 表”。The followingINFORMATION_SCHEMAcolumns provide additional metadata for instantly added columns. Refer to the descriptions of those columns for more information. See Section 28.4.9, “The INFORMATION_SCHEMA INNODB_COLUMNS Table”, and Section 28.4.23, “The INFORMATION_SCHEMA INNODB_TABLES Table”.INNODB_COLUMNS.DEFAULT_VALUEINNODB_COLUMNS.HAS_DEFAULTINNODB_TABLES.INSTANT_COLS
在添加自动递增列时,不允许并发 DML 操作。数据会被大量重新组织,这是一个开销很大的操作。至少需要
ALGORITHM=INPLACE, LOCK=SHARED。Concurrent DML is not permitted when adding an auto-increment column. Data is reorganized substantially, making it an expensive operation. At a minimum,ALGORITHM=INPLACE, LOCK=SHAREDis required.如果使用
ALGORITHM=INPLACE来添加列,表将被重建。The table is rebuilt ifALGORITHM=INPLACEis used to add a column.删除列Dropping a column
ALTER TABLE
tbl_nameDROP COLUMNcolumn_name, ALGORITHM=INSTANT;INSTANT是 MySQL 8.4 的默认算法。INSTANTis the default algorithm in MySQL 8.4.在使用
INSTANT算法删除列时,适用以下限制:The following limitations apply when theINSTANTalgorithm is used to drop a column:删除列不能与同一语句中其他不支持
ALGORITHM=INSTANT的ALTER TABLE操作组合。Dropping a column cannot be combined in the same statement with otherALTER TABLEactions that do not supportALGORITHM=INSTANT.无法从使用
ROW_FORMAT=COMPRESSED的表、具有FULLTEXT索引的表、位于数据字典表空间中的表或临时表中删除列。临时表仅支持ALGORITHM=COPY。Columns cannot be dropped from tables that useROW_FORMAT=COMPRESSED, tables with aFULLTEXTindex, tables that reside in the data dictionary tablespace, or temporary tables. Temporary tables only supportALGORITHM=COPY.
可以在同一个
ALTER TABLE语句中删除多个列;例如:Multiple columns may be dropped in the sameALTER TABLEstatement; for example:ALTER TABLE t1 DROP COLUMN c4, DROP COLUMN c5, ALGORITHM=INSTANT;
每次使用
ALGORITHM=INSTANT添加或删除列时,都会创建一个新的行版本。INFORMATION_SCHEMA.INNODB_TABLES.TOTAL_ROW_VERSIONS列用于跟踪表的行版本数量。每次即时添加或删除列时,该值会增加。初始值为 0。Each time a column is added or dropped usingALGORITHM=INSTANT, a new row version is created. TheINFORMATION_SCHEMA.INNODB_TABLES.TOTAL_ROW_VERSIONScolumn tracks the number of row versions for a table. The value is incremented each time a column is instantly added or dropped. The initial value is 0.mysql> SELECT NAME, TOTAL_ROW_VERSIONS FROM INFORMATION_SCHEMA.INNODB_TABLES WHERE NAME LIKE 'test/t1'; +---------+--------------------+ | NAME | TOTAL_ROW_VERSIONS | +---------+--------------------+ | test/t1 | 0 | +---------+--------------------+当使用表重建操作
ALTER TABLE或OPTIMIZE TABLE重建一个即时添加或删除列的表时,TOTAL_ROW_VERSIONS值将重置为 0。允许的最大行版本数量为 64(在 MySQL 9.1.0 中为 255),因为每个行版本都需要额外的表元数据空间。当达到行版本限制时,使用ALGORITHM=INSTANT的ADD COLUMN和DROP COLUMN操作将被拒绝,并显示建议使用COPY或INPLACE算法重建表的错误消息。When a table with instantly added or dropped columns is rebuilt by table-rebuildingALTER TABLEorOPTIMIZE TABLEoperation, theTOTAL_ROW_VERSIONSvalue is reset to 0. The maximum number of row versions permitted is 64 (255 as of MySQL 9.1.0), as each row version requires additional space for table metadata. When the row version limit is reached,ADD COLUMNandDROP COLUMNoperations usingALGORITHM=INSTANTare rejected with an error message that recommends rebuilding the table using theCOPYorINPLACEalgorithm.错误 4092 (HY000):表 test/t1 达到最大行版本。无法再立即添加或删除列。请使用 COPY/INPLACE。ERROR 4092 (HY000): Maximum row versions reached for table test/t1. No more columns can be added or dropped instantly. Please use COPY/INPLACE.
如果使用除
ALGORITHM=INSTANT以外的算法,数据会被大量重新组织,这会是一个昂贵的操作。If an algorithm other thanALGORITHM=INSTANTis used, data is reorganized substantially, making it an expensive operation.重命名列Renaming a column
ALTER TABLE
tblCHANGEold_col_namenew_col_namedata_type, ALGORITHM=INSTANT;为了允许并发 DML,请保持相同的数据类型,仅更改列名。To permit concurrent DML, keep the same data type and only change the column name.
当你保持相同的数据类型和
[NOT] NULL属性,仅更改列名时,该操作总是可以在线执行。When you keep the same data type and[NOT] NULLattribute, only changing the column name, the operation can always be performed online.重命名被另一个表引用的列仅允许使用
ALGORITHM=INPLACE。如果你使用ALGORITHM=INSTANT、ALGORITHM=COPY或其他造成该操作使用这些算法的条件,则ALTER TABLE语句会失败。Renaming a column referenced from another table is only permitted withALGORITHM=INPLACE. If you useALGORITHM=INSTANT,ALGORITHM=COPY, or some other condition that causes the operation to use those algorithms, theALTER TABLEstatement fails.ALGORITHM=INSTANT支持重命名虚拟列;ALGORITHM=INPLACE不支持。ALGORITHM=INSTANTsupports renaming a virtual column;ALGORITHM=INPLACEdoes not.ALGORITHM=INSTANT和ALGORITHM=INPLACE在同一语句中添加或删除虚拟列时不支持重命名列。在这种情况下,只支持ALGORITHM=COPY。ALGORITHM=INSTANTandALGORITHM=INPLACEdo not support renaming a column when adding or dropping a virtual column in the same statement. In this case, onlyALGORITHM=COPYis supported.重新排序列Reordering columns
要重新排序列,请在
CHANGE或MODIFY操作中使用FIRST或AFTER。To reorder columns, useFIRSTorAFTERinCHANGEorMODIFYoperations.ALTER TABLE
tbl_nameMODIFY COLUMNcol_namecolumn_definitionFIRST, ALGORITHM=INPLACE, LOCK=NONE;数据会被大量重组,使其成为一个开销较大的操作。Data is reorganized substantially, making it an expensive operation.
更改列数据类型Changing the column data type
ALTER TABLE
tbl_nameCHANGE c1 c1 BIGINT, ALGORITHM=COPY;更改列的数据类型仅支持
ALGORITHM=COPY。Changing the column data type is only supported withALGORITHM=COPY.扩展
VARCHAR列大小ExtendingVARCHARcolumn sizeALTER TABLE
tbl_nameCHANGE COLUMN c1 c1 VARCHAR(255), ALGORITHM=INPLACE, LOCK=NONE;The number of length bytes required by a
VARCHARcolumn must remain the same. ForVARCHARcolumns of 0 to 255 bytes in size, one length byte is required to encode the value. ForVARCHARcolumns of 256 bytes in size or more, two length bytes are required. As a result, in-placeALTER TABLEonly supports increasingVARCHARcolumn size from 0 to 255 bytes, or from 256 bytes to a greater size. In-placeALTER TABLEdoes not support increasing the size of aVARCHARcolumn from less than 256 bytes to a size equal to or greater than 256 bytes. In this case, the number of required length bytes changes from 1 to 2, which is only supported by a table copy (ALGORITHM=COPY). For example, attempting to changeVARCHAR使用就地方式将单字节字符集的列大小从 VARCHAR(255) 改为 VARCHAR(256)ALTER TABLE返回此错误:The number of length bytes required by aVARCHARcolumn must remain the same. ForVARCHARcolumns of 0 to 255 bytes in size, one length byte is required to encode the value. ForVARCHARcolumns of 256 bytes in size or more, two length bytes are required. As a result, in-placeALTER TABLEonly supports increasingVARCHARcolumn size from 0 to 255 bytes, or from 256 bytes to a greater size. In-placeALTER TABLEdoes not support increasing the size of aVARCHARcolumn from less than 256 bytes to a size equal to or greater than 256 bytes. In this case, the number of required length bytes changes from 1 to 2, which is only supported by a table copy (ALGORITHM=COPY). For example, attempting to changeVARCHARcolumn size for a single byte character set from VARCHAR(255) to VARCHAR(256) using in-placeALTER TABLEreturns this error:ALTER TABLE
tbl_nameALGORITHM=INPLACE, CHANGE COLUMN c1 c1 VARCHAR(256); ERROR 0A000: ALGORITHM=INPLACE is not supported. Reason: Cannot change column type INPLACE. Try ALGORITHM=COPY.NoteVARCHAR列的字节长度取决于字符集的字节长度。The byte length of aVARCHARcolumn is dependant on the byte length of the character set.使用原地
ALTER TABLE减小VARCHAR大小是不支持的。减小VARCHAR大小需要表复制 (ALGORITHM=COPY)。DecreasingVARCHARsize using in-placeALTER TABLEis not supported. DecreasingVARCHARsize requires a table copy (ALGORITHM=COPY).设置列默认值Setting a column default value
ALTER TABLE
tbl_nameALTER COLUMNcolSET DEFAULTliteral, ALGORITHM=INSTANT;仅修改表元数据。默认列值存储在 数据字典 中。Only modifies table metadata. Default column values are stored in the data dictionary.
删除列的默认值Dropping a column default value
ALTER TABLE
tblALTER COLUMNcolDROP DEFAULT, ALGORITHM=INSTANT;更改自增值Changing the auto-increment value
ALTER TABLE
tableAUTO_INCREMENT=next_value, ALGORITHM=INPLACE, LOCK=NONE;修改存储在内存中的值,而不是数据文件。Modifies a value stored in memory, not the data file.
在使用复制或分片的分布式系统中,有时你会将表的自增计数器重置为特定值。下一行插入表时,其自增列将使用指定的值。你也可能在数据仓库环境中使用这种技术,在这种环境中,你会定期清空所有表并重新加载它们,并将自增序列从 1 重新开始。In a distributed system using replication or sharding, you sometimes reset the auto-increment counter for a table to a specific value. The next row inserted into the table uses the specified value for its auto-increment column. You might also use this technique in a data warehousing environment where you periodically empty all the tables and reload them, and restart the auto-increment sequence from 1.
将列设置为
NULLMaking a columnNULLALTER TABLE tbl_name MODIFY COLUMN
column_namedata_typeNULL, ALGORITHM=INPLACE, LOCK=NONE;在原位重建表。数据会被大幅重组,因此这是一个开销较大的操作。Rebuilds the table in place. Data is reorganized substantially, making it an expensive operation.
将列设置为
NOT NULLMaking a columnNOT NULLALTER TABLE
tbl_nameMODIFY COLUMNcolumn_namedata_typeNOT NULL, ALGORITHM=INPLACE, LOCK=NONE;在原地重建表。
STRICT_ALL_TABLES或STRICT_TRANS_TABLESSQL_MODE是操作成功所必需的。如果列包含 NULL 值,操作将失败。服务器禁止对可能导致参照完整性丧失的外键列进行更改。请参阅 第 15.1.9 节,“ALTER TABLE 语句”。数据将被大幅重组,因此这是一个开销较大的操作。Rebuilds the table in place.STRICT_ALL_TABLESorSTRICT_TRANS_TABLESSQL_MODEis required for the operation to succeed. The operation fails if the column contains NULL values. The server prohibits changes to foreign key columns that have the potential to cause loss of referential integrity. See Section 15.1.9, “ALTER TABLE Statement”. Data is reorganized substantially, making it an expensive operation.修改
ENUM或SET列的定义Modifying the definition of anENUMorSETcolumnCREATE TABLE t1 (c1 ENUM('a', 'b', 'c')); ALTER TABLE t1 MODIFY COLUMN c1 ENUM('a', 'b', 'c', 'd'), ALGORITHM=INSTANT;通过在有效成员值列表的末尾添加新的枚举或集合成员来修改
ENUM或SET列的定义时,只要数据类型的存储大小不改变,就可以立即或就地执行。例如,向具有 8 个成员的SET列添加一个成员,将每个值所需的存储从 1 字节更改为 2 字节;这需要进行表复制。在列表中间添加成员会导致现有成员重新编号,这也需要进行表复制。Modifying the definition of anENUMorSETcolumn by adding new enumeration or set members to the end of the list of valid member values may be performed instantly or in place, as long as the storage size of the data type does not change. For example, adding a member to aSETcolumn that has 8 members changes the required storage per value from 1 byte to 2 bytes; this requires a table copy. Adding members in the middle of the list causes renumbering of existing members, which requires a table copy.
下表提供了关于生成列操作的在线 DDL 支持概览。详情请参见 语法和使用说明。The following table provides an overview of online DDL support for generated column operations. For details, see Syntax and Usage Notes.
表 17.18 生成列操作的在线 DDL 支持Table 17.18 Online DDL Support for Generated Column Operations
| 操作Operation | 即时Instant | 就地In Place | 重建表Rebuilds Table | 允许并发 DMLPermits Concurrent DML | 仅修改元数据Only Modifies Metadata |
|---|---|---|---|---|---|
添加一个 STORED 列Adding a STORED column |
否No | 否No | 是Yes | 否No | 否No |
修改 STORED 列顺序Modifying STORED column order |
否No | 否No | 是Yes | 否No | 否No |
删除一个 STORED 列Dropping a STORED column |
否No | 是Yes | 是Yes | 是Yes | 否No |
添加一个 VIRTUAL 列Adding a VIRTUAL column |
是Yes | 是Yes | 否No | 是Yes | 是Yes |
修改 VIRTUAL 列顺序Modifying VIRTUAL column order |
否No | 否No | 是Yes | 否No | 否No |
删除一个 VIRTUAL 列Dropping a VIRTUAL column |
是Yes | 是Yes | 否No | 是Yes | 是Yes |
语法和使用说明Syntax and Usage Notes
添加一个
STORED列Adding aSTOREDcolumnALTER TABLE t1 ADD COLUMN (c2 INT GENERATED ALWAYS AS (c1 + 1) STORED), ALGORITHM=COPY;
ADD COLUMN对于存储列而言不是就地操作(不使用临时表完成),因为表达式必须由服务器评估。ADD COLUMNis not an in-place operation for stored columns (done without using a temporary table) because the expression must be evaluated by the server.修改
STORED列顺序ModifyingSTOREDcolumn orderALTER TABLE t1 MODIFY COLUMN c2 INT GENERATED ALWAYS AS (c1 + 1) STORED FIRST, ALGORITHM=COPY;
在原位重建表。Rebuilds the table in place.
删除一个
STORED列Dropping aSTOREDcolumnALTER TABLE t1 DROP COLUMN c2, ALGORITHM=INPLACE, LOCK=NONE;
在原位重建表。Rebuilds the table in place.
添加一个
VIRTUAL列Adding aVIRTUALcolumnALTER TABLE t1 ADD COLUMN (c2 INT GENERATED ALWAYS AS (c1 + 1) VIRTUAL), ALGORITHM=INSTANT;
对于非分区表,添加虚拟列可以立即或就地执行。Adding a virtual column can be performed instantly or in place for non-partitioned tables.
对于分区表,添加
VIRTUAL不是就地操作。Adding aVIRTUALis not an in-place operation for partitioned tables.修改
VIRTUAL列顺序ModifyingVIRTUALcolumn orderALTER TABLE t1 MODIFY COLUMN c2 INT GENERATED ALWAYS AS (c1 + 1) VIRTUAL FIRST, ALGORITHM=COPY;
删除一个
VIRTUAL列Dropping aVIRTUALcolumnALTER TABLE t1 DROP COLUMN c2, ALGORITHM=INSTANT;
对于非分区表,删除
VIRTUAL列可以立即或就地执行。Dropping aVIRTUALcolumn can be performed instantly or in place for non-partitioned tables.
下表提供了在线 DDL 对外键操作支持的概览。星号表示附加信息、例外或依赖关系。详情请参见 语法和使用说明。The following table provides an overview of online DDL support for foreign key operations. An asterisk indicates additional information, an exception, or a dependency. For details, see Syntax and Usage Notes.
表 17.19 外键操作的在线 DDL 支持Table 17.19 Online DDL Support for Foreign Key Operations
| 操作Operation | 即时Instant | 就地In Place | 重建表Rebuilds Table | 允许并发 DMLPermits Concurrent DML | 仅修改元数据Only Modifies Metadata |
|---|---|---|---|---|---|
| 添加外键约束Adding a foreign key constraint | 否No | 是*Yes* | 否No | 是Yes | 是Yes |
| 删除外键约束Dropping a foreign key constraint | 否No | 是Yes | 否No | 是Yes | 是Yes |
语法和使用说明Syntax and Usage Notes
添加外键约束Adding a foreign key constraint
当
foreign_key_checks被禁用时,支持INPLACE算法。否则,仅支持COPY算法。TheINPLACEalgorithm is supported whenforeign_key_checksis disabled. Otherwise, only theCOPYalgorithm is supported.ALTER TABLE
tbl1ADD CONSTRAINTfk_nameFOREIGN KEYindex(col1) REFERENCEStbl2(col2)referential_actions;删除外键约束Dropping a foreign key constraint
ALTER TABLE
tblDROP FOREIGN KEYfk_name;删除外键可以在打开或关闭
foreign_key_checks选项的情况下在线执行。Dropping a foreign key can be performed online with theforeign_key_checksoption enabled or disabled.如果您不知道特定表上的外键约束名称,请执行以下语句,并在每个外键的
CONSTRAINT子句中查找约束名称:If you do not know the names of the foreign key constraints on a particular table, issue the following statement and find the constraint name in theCONSTRAINTclause for each foreign key:SHOW CREATE TABLE
table\G或者,查询信息模式
TABLE_CONSTRAINTS表,并使用CONSTRAINT_NAME和CONSTRAINT_TYPE列来识别外键名称。Or, query the Information SchemaTABLE_CONSTRAINTStable and use theCONSTRAINT_NAMEandCONSTRAINT_TYPEcolumns to identify the foreign key names.您还可以在单条语句中删除外键及其关联的索引:You can also drop a foreign key and its associated index in a single statement:
ALTER TABLE
tableDROP FOREIGN KEYconstraint, DROP INDEXindex;
如果要修改的表中已经存在外键(也就是说,它是一个子表,包含FOREIGN KEY ... REFERENCE子句),即便是那些不直接涉及外键列的在线 DDL 操作,也会受到额外的限制:If foreign keys are
already present in the table being altered (that is, it is a
child table containing
a FOREIGN KEY ... REFERENCE clause),
additional restrictions apply to online DDL operations, even
those not directly involving the foreign key columns:
对子表进行的
ALTER TABLE操作可能需要等待另一个事务提交,如果对父表的更改通过使用CASCADE或SET NULL参数的ON UPDATE或ON DELETE子句在子表中引起相关更改。AnALTER TABLEon the child table could wait for another transaction to commit, if a change to the parent table causes associated changes in the child table through anON UPDATEorON DELETEclause using theCASCADEorSET NULLparameters.In the same way, if a table is the parent table in a foreign key relationship, even though it does not contain any
FOREIGN KEYclauses, it could wait for theALTER TABLEto complete if anINSERT,UPDATE,或DELETEstatement causes anON UPDATE或者ON DELETEaction in the child table.In the same way, if a table is the parent table in a foreign key relationship, even though it does not contain anyFOREIGN KEYclauses, it could wait for theALTER TABLEto complete if anINSERT,UPDATE, orDELETEstatement causes anON UPDATEorON DELETEaction in the child table.
下表提供了关于表操作的在线 DDL 支持概述。星号表示附加信息、例外或依赖关系。详情请参见 语法和使用说明。The following table provides an overview of online DDL support for table operations. An asterisk indicates additional information, an exception, or a dependency. For details, see Syntax and Usage Notes.
表 17.20 表操作的在线 DDL 支持Table 17.20 Online DDL Support for Table Operations
| 操作Operation | 即时Instant | 就地In Place | 重建表Rebuilds Table | 允许并发 DMLPermits Concurrent DML | 仅修改元数据Only Modifies Metadata |
|---|---|---|---|---|---|
更改 ROW_FORMATChanging the ROW_FORMAT |
否No | 是Yes | 是Yes | 是Yes | 否No |
更改 KEY_BLOCK_SIZEChanging the KEY_BLOCK_SIZE |
否No | 是Yes | 是Yes | 是Yes | 否No |
| 设置持久表统计信息Setting persistent table statistics | 否No | 是Yes | 否No | 是Yes | 是Yes |
| 指定字符集Specifying a character set | 否No | 是Yes | 是*Yes* | 是Yes | 否No |
| 转换字符集Converting a character set | 否No | 是Yes | 是*Yes* | 否No | 否No |
| 优化表Optimizing a table | 否No | 是*Yes* | 是Yes | 是Yes | 否No |
使用 FORCE 选项重建Rebuilding with the FORCE option |
否No | 是*Yes* | 是Yes | 是Yes | 否No |
| 执行空重建Performing a null rebuild | 否No | 是*Yes* | 是Yes | 是Yes | 否No |
| 重命名表Renaming a table | 是Yes | 是Yes | 否No | 是Yes | 是Yes |
语法和使用说明Syntax and Usage Notes
更改
ROW_FORMATChanging theROW_FORMATALTER TABLE
tbl_nameROW_FORMAT =row_format, ALGORITHM=INPLACE, LOCK=NONE;数据会被大量重组,使其成为一个开销较大的操作。Data is reorganized substantially, making it an expensive operation.
有关
ROW_FORMAT选项的更多信息,请参见 表选项。For additional information about theROW_FORMAToption, see Table Options.更改
KEY_BLOCK_SIZEChanging theKEY_BLOCK_SIZEALTER TABLE
tbl_nameKEY_BLOCK_SIZE =value, ALGORITHM=INPLACE, LOCK=NONE;数据会被大量重组,使其成为一个开销较大的操作。Data is reorganized substantially, making it an expensive operation.
有关
KEY_BLOCK_SIZE选项的更多信息,请参见 表选项。For additional information about theKEY_BLOCK_SIZEoption, see Table Options.设置持久表统计信息选项Setting persistent table statistics options
ALTER TABLE
tbl_nameSTATS_PERSISTENT=0, STATS_SAMPLE_PAGES=20, STATS_AUTO_RECALC=1, ALGORITHM=INPLACE, LOCK=NONE;仅修改表元数据。Only modifies table metadata.
持久统计信息包括
STATS_PERSISTENT、STATS_AUTO_RECALC和STATS_SAMPLE_PAGES。有关更多信息,请参见 第 17.8.10.1 节,“配置持久优化器统计参数”。Persistent statistics includeSTATS_PERSISTENT,STATS_AUTO_RECALC, andSTATS_SAMPLE_PAGES. For more information, see Section 17.8.10.1, “Configuring Persistent Optimizer Statistics Parameters”.指定字符集Specifying a character set
ALTER TABLE
tbl_nameCHARACTER SET =charset_name, ALGORITHM=INPLACE, LOCK=NONE;如果新的字符编码不同,则重建表。Rebuilds the table if the new character encoding is different.
转换字符集Converting a character set
ALTER TABLE
tbl_nameCONVERT TO CHARACTER SETcharset_name, ALGORITHM=INPLACE, LOCK=NONE;如果新的字符编码不同,则重建表。Rebuilds the table if the new character encoding is different.
优化表Optimizing a table
OPTIMIZE TABLE
tbl_name;对于具有
FULLTEXT索引的表,不支持就地操作。该操作使用INPLACE算法,但不允许使用ALGORITHM和LOCK语法。In-place operation is not supported for tables withFULLTEXTindexes. The operation uses theINPLACEalgorithm, butALGORITHMandLOCKsyntax is not permitted.使用
FORCE选项重建表Rebuilding a table with theFORCEoptionALTER TABLE
tbl_nameFORCE, ALGORITHM=INPLACE, LOCK=NONE;使用
ALGORITHM=INPLACE自 MySQL 5.6.17 起.ALGORITHM=INPLACE不支持具有FULLTEXT索引的表。UsesALGORITHM=INPLACEas of MySQL 5.6.17.ALGORITHM=INPLACEis not supported for tables withFULLTEXTindexes.执行“空”重建Performing a "null" rebuild
ALTER TABLE
tbl_nameENGINE=InnoDB, ALGORITHM=INPLACE, LOCK=NONE;从 MySQL 5.6.17 开始使用
ALGORITHM=INPLACE。对于具有FULLTEXT索引的表,不支持ALGORITHM=INPLACE。UsesALGORITHM=INPLACEas of MySQL 5.6.17.ALGORITHM=INPLACEis not supported for tables withFULLTEXTindexes.重命名表Renaming a table
ALTER TABLE
old_tbl_nameRENAME TOnew_tbl_name, ALGORITHM=INSTANT;重命名表可以即时进行或就地进行。MySQL 会重命名与表
tbl_name对应的文件,而无需制作副本。(你也可以使用RENAME TABLE语句来重命名表。参见 第 15.1.36 节,“RENAME TABLE 语句”。)专门授予重命名表的权限不会自动迁移到新名称。必须手动更改。Renaming a table can be performed instantly or in place. MySQL renames files that correspond to the tabletbl_namewithout making a copy. (You can also use theRENAME TABLEstatement to rename tables. See Section 15.1.36, “RENAME TABLE Statement”.) Privileges granted specifically for the renamed table are not migrated to the new name. They must be changed manually.
下表提供了在线 DDL 对表空间操作的支持概览。有关详细信息,请参见 语法和使用说明。The following table provides an overview of online DDL support for tablespace operations. For details, see Syntax and Usage Notes.
表 17.21 表空间操作的在线 DDL 支持Table 17.21 Online DDL Support for Tablespace Operations
| 操作Operation | 即时Instant | 就地In Place | 重建表Rebuilds Table | 允许并发 DMLPermits Concurrent DML | 仅修改元数据Only Modifies Metadata |
|---|---|---|---|---|---|
| 重命名普通表空间Renaming a general tablespace | 否No | 是Yes | 否No | 是Yes | 是Yes |
| 启用或禁用普通表空间加密Enabling or disabling general tablespace encryption | 否No | 是Yes | 否No | 是Yes | 否No |
| 启用或禁用每表文件表空间加密Enabling or disabling file-per-table tablespace encryption | 否No | 否No | 是Yes | 否No | 否No |
语法和使用说明Syntax and Usage Notes
重命名普通表空间Renaming a general tablespace
ALTER TABLESPACE
tablespace_nameRENAME TOnew_tablespace_name;ALTER TABLESPACE ... RENAME TO使用INPLACE算法,但不支持ALGORITHM子句。ALTER TABLESPACE ... RENAME TOuses theINPLACEalgorithm but does not support theALGORITHMclause.启用或禁用普通表空间加密Enabling or disabling general tablespace encryption
ALTER TABLESPACE
tablespace_nameENCRYPTION='Y';ALTER TABLESPACE ... ENCRYPTION使用INPLACE算法,但不支持ALGORITHM子句。ALTER TABLESPACE ... ENCRYPTIONuses theINPLACEalgorithm but does not support theALGORITHMclause.有关相关信息,请参阅 第 17.13 节,“InnoDB 静态数据加密”。For related information, see Section 17.13, “InnoDB Data-at-Rest Encryption”.
启用或禁用每表文件表空间加密Enabling or disabling file-per-table tablespace encryption
ALTER TABLE
tbl_nameENCRYPTION='Y', ALGORITHM=COPY;有关相关信息,请参阅 第 17.13 节,“InnoDB 静态数据加密”。For related information, see Section 17.13, “InnoDB Data-at-Rest Encryption”.
除了某些 ALTER
TABLE 分区子句之外,分区 InnoDB 表的在线 DDL 操作遵循适用于普通 InnoDB 表的相同规则。With the exception of some ALTER
TABLE partitioning clauses, online DDL operations for
partitioned InnoDB tables follow the same
rules that apply to regular InnoDB tables.
某些 ALTER TABLE 分区子句不会像常规非分区 InnoDB 表那样通过相同的内部在线 DDL API。因此,ALTER
TABLE 分区子句的在线支持程度有所不同。Some ALTER TABLE partitioning
clauses do not go through the same internal online DDL API as
regular non-partitioned InnoDB tables. As a
result, online support for ALTER
TABLE partitioning clauses varies.
下表显示了每个 ALTER TABLE 分区语句的在线状态。无论使用哪种在线 DDL API,MySQL 都会尽可能减少数据复制和锁定。The following table shows the online status for each
ALTER TABLE partitioning statement.
Regardless of the online DDL API that is used, MySQL attempts to
minimize data copying and locking where possible.
ALTER TABLE 分区选项使用 ALGORITHM=COPY 或仅允许 “ALGORITHM=DEFAULT,
LOCK=DEFAULT” 时,使用 COPY 算法对表进行重新分区。换句话说,将使用新的分区方案创建一个新的分区表。新创建的表包括由 ALTER TABLE 语句应用的任何更改,并且表数据将复制到新的表结构中。ALTER TABLE partitioning options
that use ALGORITHM=COPY or that only permit
“ALGORITHM=DEFAULT,
LOCK=DEFAULT”, repartition the table using the
COPY algorithm. In other words, a new
partitioned table is created with the new partitioning scheme.
The newly created table includes any changes applied by the
ALTER TABLE statement, and table
data is copied into the new table structure.
表 17.22 分区操作的在线 DDL 支持Table 17.22 Online DDL Support for Partitioning Operations
| 分区子句Partitioning Clause | 即时Instant | 就地In Place | 允许 DMLPermits DML | 注释Notes |
|---|---|---|---|---|
PARTITION BYPARTITION BY |
否No | 否No | 否No | 允许 ALGORITHM=COPY,LOCK={DEFAULT|SHARED|EXCLUSIVE}Permits ALGORITHM=COPY,
LOCK={DEFAULT|SHARED|EXCLUSIVE} |
ADD PARTITIONADD PARTITION |
否No | 是*Yes* | 是*Yes* | ALGORITHM=INPLACE,
LOCK={DEFAULT|NONE|SHARED|EXCLUSISVE} 支持 RANGE 和 LIST 分区,ALGORITHM=INPLACE,
LOCK={DEFAULT|SHARED|EXCLUSISVE} 支持 HASH 和 KEY 分区,而 ALGORITHM=COPY,
LOCK={SHARED|EXCLUSIVE} 支持所有分区类型。对于按 RANGE 或 LIST 分区的表,不会复制现有数据。在使用 ALGORITHM=COPY 时,对按 HASH 或 LIST 分区的表允许并发查询,因为 MySQL 在持有共享锁的同时复制数据。ALGORITHM=INPLACE,
LOCK={DEFAULT|NONE|SHARED|EXCLUSISVE} is
supported for RANGE and
LIST partitions,
ALGORITHM=INPLACE,
LOCK={DEFAULT|SHARED|EXCLUSISVE} for
HASH and KEY
partitions, and ALGORITHM=COPY,
LOCK={SHARED|EXCLUSIVE} for all partition types.
Does not copy existing data for tables partitioned by
RANGE or LIST.
Concurrent queries are permitted with
ALGORITHM=COPY for tables partitioned
by HASH or LIST, as
MySQL copies the data while holding a shared lock. |
DROP PARTITION |
否No | 是*Yes* | 是*Yes* |
|
DISCARD PARTITIONDISCARD PARTITION |
否No | 否No | 否No | 仅允许 ALGORITHM=DEFAULT,LOCK=DEFAULTOnly permits ALGORITHM=DEFAULT,
LOCK=DEFAULT |
IMPORT PARTITIONIMPORT PARTITION |
否No | 否No | 否No | 仅允许 ALGORITHM=DEFAULT,LOCK=DEFAULTOnly permits ALGORITHM=DEFAULT,
LOCK=DEFAULT |
TRUNCATE
PARTITIONTRUNCATE
PARTITION |
否No | 是Yes | 是Yes | 不复制现有数据。它只是删除行;不会修改表本身或其任何分区的定义。Does not copy existing data. It merely deletes rows; it does not alter the definition of the table itself, or of any of its partitions. |
COALESCE
PARTITIONCOALESCE
PARTITION |
否No | 是*Yes* | 否No | ALGORITHM=INPLACE, LOCK={DEFAULT|SHARED|EXCLUSIVE}。ALGORITHM=INPLACE, LOCK={DEFAULT|SHARED|EXCLUSIVE} is
supported. |
REORGANIZE
PARTITIONREORGANIZE
PARTITION |
否No | 是*Yes* | 否No | ALGORITHM=INPLACE, LOCK={DEFAULT|SHARED|EXCLUSIVE}。ALGORITHM=INPLACE, LOCK={DEFAULT|SHARED|EXCLUSIVE} is
supported. |
EXCHANGE
PARTITIONEXCHANGE
PARTITION |
否No | 是Yes | 是Yes | |
ANALYZE PARTITIONANALYZE PARTITION |
否No | 是Yes | 是Yes | |
CHECK PARTITIONCHECK PARTITION |
否No | 是Yes | 是Yes | |
OPTIMIZE
PARTITIONOPTIMIZE
PARTITION |
否No | 否No | 否No | ALGORITHM 和 LOCK 子句将被忽略。重建整个表。参见 第 26.3.4 节,“分区维护”。ALGORITHM and LOCK clauses are
ignored. Rebuilds the entire table. See
Section 26.3.4, “Maintenance of Partitions”. |
REBUILD PARTITIONREBUILD PARTITION |
否No | 是*Yes* | 否No | ALGORITHM=INPLACE, LOCK={DEFAULT|SHARED|EXCLUSIVE}。ALGORITHM=INPLACE, LOCK={DEFAULT|SHARED|EXCLUSIVE} is
supported. |
REPAIR PARTITIONREPAIR PARTITION |
否No | 是Yes | 是Yes | |
REMOVE
PARTITIONINGREMOVE
PARTITIONING |
否No | 否No | 否No | 允许 ALGORITHM=COPY,LOCK={DEFAULT|SHARED|EXCLUSIVE}Permits ALGORITHM=COPY,
LOCK={DEFAULT|SHARED|EXCLUSIVE} |
分区表上的非分区在线 ALTER TABLE 操作遵循适用于普通表的相同规则。然而,ALTER
TABLE 会对每个表分区执行在线操作,这会由于在多个分区上执行操作而增加系统资源的需求。Non-partitioning online ALTER
TABLE operations on partitioned tables follow the same
rules that apply to regular tables. However,
ALTER TABLE performs online
operations on each table partition, which causes increased
demand on system resources due to operations being performed on
multiple partitions.
有关 ALTER
TABLE 分区子句的更多信息,请参阅 分区选项,以及 第 15.1.9.1 节,“ALTER TABLE 分区操作”。有关分区的一般信息,请参阅 第 26 章,分区。For additional information about ALTER
TABLE partitioning clauses, see
Partitioning Options, and
Section 15.1.9.1, “ALTER TABLE Partition Operations”. For
information about partitioning in general, see
Chapter 26, Partitioning.