根据网上的DDL 树状图,自己copy了一份:http://assets.processon.com/chart_image/5c331a29e4b0fa03ce8eb139.png?_=1547952296662

对不同版本的add index的处理方式,这篇文章写得不错:https://www.jb51.net/article/75217.htm

-- 相关实践

表结构:

CREATE TABLE `tt` (
`id` int(11) DEFAULT NULL,
`age` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
row in set (0.01 sec)

行数:

data_migration@192.168.7.21:3306 test>select count(*) from tt;
+----------+
| count(*) |
+----------+
| 12582912 |
+----------+

data_migration@192.168.7.21:3306 test>alter table tt add index idx_id(id);
Query OK, 0 rows affected (27.49 sec)
Records: 0  Duplicates: 0  Warnings: 0

增加id索引 临时表:只增加了#sql-1e65_68.frm,未增加idb 数据量变化:448M -> 676M

data_migration@192.168.7.21:3306 test>alter table tt add name1 varchar(12);
Query OK, 0 rows affected (44.72 sec)
Records: 0  Duplicates: 0  Warnings: 0

新增字段 临时表:增加#sql-1e65_68.frm, #sql-ib231-4246726206.ibd,完成时增加#sql-ib255-4246726207.ibd没有tt.ibd,最后生成tt.idb 数据量变化:临时表一直增加到和新表同样的数据量后,最后完成新表替换,新表 676M->740M

-rw-r-----  1 mysql mysql 740M 1月   8 11:30 #sql-ib231-4246726206.ibd
-rw-r-----  1 mysql mysql 8.4K 1月   8 11:06 tt.frm
-rw-r-----  1 mysql mysql 676M 1月   8 11:08 tt.ibd
[root@dbtest-21 test]# ll -ah
-rw-r-----  1 mysql mysql 8.5K 1月   8 11:30 #sql-1e65_68.frm
-rw-r-----  1 mysql mysql 740M 1月   8 11:30 #sql-ib231-4246726206.ibd
-rw-r-----  1 mysql mysql 8.4K 1月   8 11:06 tt.frm
-rw-r-----  1 mysql mysql 676M 1月   8 11:08 tt.ibd
[root@dbtest-21 test]# ll -ah
-rw-r-----  1 mysql mysql 8.5K 1月   8 11:30 #sql-1e65_68.frm
-rw-r-----  1 mysql mysql 740M 1月   8 11:30 #sql-ib231-4246726206.ibd
-rw-r-----  1 mysql mysql 676M 1月   8 11:08 #sql-ib255-4246726207.ibd
-rw-r-----  1 mysql mysql 8.4K 1月   8 11:06 tt.frm
[root@dbtest-21 test]# ll -ah
-rw-r-----  1 mysql mysql 8.5K 1月   8 11:30 tt.frm
-rw-r-----  1 mysql mysql 740M 1月   8 11:30 tt.ibd

data_migration@192.168.7.21:3306 test>alter table tt modify age varchar(20);
Query OK, 12582912 rows affected (2 min 36.80 sec)
Records: 12582912  Duplicates: 0  Warnings: 0

修改字段 临时表:增加#sql-1e65_68.frm, #sql-ib231-4246726206.ibd,完成时增加#sql-ib255-4246726207.ibd没有tt.ibd,最后生成tt.idb 数据量变化:临时表一直增加到和新表同样的数据量后,最后完成新表替换 时间增加 TIPS: 1.新增空字段,磁盘会额外占用新加字段的空间。 2.修改增加的新的空字段类型,可以瞬间完成。

--

algorithm = copy/inplace
copy会一直创建临时表,inplace在不同场景(rebuild、no-rebuild)下都会创建、不创建临时表 copy :copy to tmp table,期间不允许DML inplace :altering table 允许并发DML,使用了参数innodb_online_alter_log_max_size缓存新的写