更新时间:2023-01-18 GMT+08:00

Online DDL

DDM支持通用的Online DDL操作:增加字段、删除字段、修改字段、设置默认值、修改编码、修改表名等。

Online DDL主要功能为在对应DDL上,提供ALGORITHM、LOCK显示声明的支持,并提供透传至后端数据库节点能力(此功能需满足DDM内核版本大于等于3.1.0版本)。

当DDM实例关联的DN实例为MySQL5.7版本时,Online DDL操作支持以下语法:

ALTER TABLE tbl_name
    [alter_option [, alter_option] ...]

alter_option: {
  | ADD [COLUMN] col_name column_definition
        [FIRST | AFTER col_name]
  | ADD [COLUMN] (col_name column_definition,...)
  | DROP [COLUMN] col_name
  | ALTER [COLUMN] col_name {
        SET DEFAULT {literal | (expr)}
      | DROP DEFAULT
    }
  | CHANGE [COLUMN] old_col_name new_col_name column_definition
        [FIRST | AFTER col_name]
  | [DEFAULT] CHARACTER SET [=] charset_name [COLLATE [=] collation_name]
  | CONVERT TO CHARACTER SET charset_name [COLLATE collation_name]
  | MODIFY [COLUMN] col_name column_definition
        [FIRST | AFTER col_name]
  | RENAME [TO | AS] new_tbl_name

  | ALGORITHM [=] {DEFAULT | INPLACE | COPY}
  | LOCK [=] {DEFAULT | NONE | SHARED | EXCLUSIVE}
}

当DDM实例关联的DN实例为MySQL8.0版本时,Online DDL操作支持以下语法:

ALTER TABLE tbl_name
    [alter_option [, alter_option] ...]

alter_option: {
  | ADD [COLUMN] col_name column_definition
        [FIRST | AFTER col_name]
  | ADD [COLUMN] (col_name column_definition,...)
  | DROP [COLUMN] col_name
  | ALTER [COLUMN] col_name {
        SET DEFAULT {literal | (expr)}
      | DROP DEFAULT
    }
  | CHANGE [COLUMN] old_col_name new_col_name column_definition
        [FIRST | AFTER col_name]
  | [DEFAULT] CHARACTER SET [=] charset_name [COLLATE [=] collation_name]
  | CONVERT TO CHARACTER SET charset_name [COLLATE collation_name]
  | MODIFY [COLUMN] col_name column_definition
        [FIRST | AFTER col_name]
  | RENAME COLUMN old_col_name TO new_col_name
  | RENAME [TO | AS] new_tbl_name

  | ALGORITHM [=] {DEFAULT | INSTANT | INPLACE | COPY}
  | LOCK [=] {DEFAULT | NONE | SHARED | EXCLUSIVE}
}

在使用Online DDL语法时,最终ALGORITHM、LOCK选项生效位置为后端数据库,当后端有多个数据库时,DDM表现出的并发性可能与参数值不一致。

使用示例

增加字段

# 向表t2中增加字段x,类型为int,Online DDL 算法为inplace,锁为NONE
ALTER TABLE t2 ADD COLUMN x INT, ALGORITHM=INPLACE, LOCK=NONE;

修改字段

# 修改表t2的字段x,修改字段类型为varchar(255),Online DDL 算法为copy,锁为shared
ALTER TABLE t2 MODIFY x VARCHAR(255), ALGORITHM=COPY, LOCK=SHARED;

修改编码

# 修改表t2编码为utf8,排序算法为utf8_bin,Online DDL 算法为copy,锁为shared
ALTER TABLE t2 CHARACTER SET utf8 COLLATE utf8_bin, ALGORITHM=COPY, LOCK=SHARED;