1. Instant DDL优点
The online DDL feature provides support for instant and in-place table alterations and concurrent DML. Benefits of this feature include:
- Improved responsiveness and availability in busy production environments, where making a table unavailable for minutes or hours is not practical.
在一个繁忙的系统上提高了响应性和可用性 - For in-place operations, the ability to adjust the balance between performance and concurrency during DDL operations using the
LOCK
clause. See The LOCK clause. - Less disk space usage and I/O overhead than the table-copy method
节约了磁盘IO
2. MySQL在8.0.12版本支持了Instant DDL功能
只需要修改数据字典的元数据,不需要修改存储层数据就可以快速完成DDL
ALTER TABLE *tbl_name* ADD COLUMN *column_name* *column_definition*, ALGORITHM=INSTANT;
支持的算法:
1.DEFAULT:MySQL自己选择锁定资源最少的方式
2.INSTANT:只需要更新数据字典中的元数据, 很快完成
3.INPLACE:在已存在的表空间文件内变更数据,除了一些特殊情况外表空间文件id 不会变化 此变更由InnoDB引擎独立完成, 不需要使用Redo log等, 可以节省开销
4.COPY:把存在数据拷贝到一个新的表空间文件中,所以IO 比较多,时间比较长 此变更会重建聚簇索引, 执行DDL的时候会创建临时表
快速DDL支持类型
-
Instant add column
- 当一条alter语句中同时存在不支持instant的ddl时,则无法使用
- 只能顺序加列,也就是说只能在最后添加列,这有点不友好,在8.0.29后可以任意地方加列
- 不支持压缩表、不支持包含全文索引的表
- 不支持临时表,临时表只能使用copy的方式执行DDL
- 不支持那些在数据词典表空间中创建的表
-
修改索引类型
-
修改ENUM/SET类型的定义
- 存储的大小不变时
- 向后追加成员
-
增加或删除类型为virtual的generated column
-
RENAME TABLE操作
3. MySQL 8.0.28版本增强Instant DDL对列重命名的支持
4. MySQL 8.0.29版本可以在任何地方添加列,而且是Instant,这个是个好特性
ALGORITHM=INSTANT Operations that support ALGORITHM=INSTANT
only modify metadata in the data dictionary. No exclusive metadata locks(表级元数据锁) are taken on the table during preparation and execution phases of the operation, and table data is unaffected, making the operations instantaneous. If not specified explicitly, ALGORITHM=INSTANT
is used by default by DDL operations that support it.
Prior to MySQL 8.0.29, an instantly added column could only be added as the last column of the table.
From MySQL 8.0.29, an instantly added column can be added to any position in the table.
Instantly added or dropped columns create a new row version. Up to 64 row versions are permitted. A new TOTAL_ROW_VERSIONS
column was added to the INFORMATION_SCHEMA.INNODB_TABLES
table to track the number of row versions.
参考文档: