得到消息MySQL8.0對Online Column Operations做了優化,部分Online DDL采用instant算法,使得變更時候不需要rebuild整個表,隻需要在表的metadata中記錄新增列額基本資訊即可。在 alter 語句後增加 ALGORITHM=INSTANT 即代表使用 instant 算法,如果未明确指定,則支援 instant 算法的操作會預設使用。如果 ALGORITHM=INSTANT 指定但不支援,則操作立即失敗并顯示錯誤。
檢視官方文檔,得到如下資訊:

接下來,我們對其中一些操作進行測試對比。
測試環境
MySQL5.7.32
MySQL8.0.25
測試表資料量1600萬
增加列
MySQL5.7
MySQL> alter table performance_order add testcol varchar(20);
Query OK, 0 rows affected (1 min 15.47 sec)
MySQL8.0
MySQL> alter table performance_order add testcol varchar(20);
Query OK, 0 rows affected (0.02 sec)
增加列,MySQL8.0性能提升很多。
修改列名
MySQL5.7
MySQL> alter table performance_order CHANGE testcol testcol2 varchar(20);
Query OK, 0 rows affected (0.00 sec)
MySQL8.0
MySQL> alter table performance_order CHANGE testcol testcol2 varchar(20);
Query OK, 0 rows affected (0.01 sec)
修改列名,兩個版執行本速度在一個級别。
設定列預設值
MySQL5.7
MySQL> alter table performance_order alter testcol2 set default '北京';
Query OK, 0 rows affected (0.00 sec)
MySQL8.0
MySQL> alter table performance_order alter testcol2 set default '北京';
Query OK, 0 rows affected (0.01 sec)
設定列預設值,兩個版執行本速度在一個級别。
删除列預設值
MySQL5.7
MySQL> alter table performance_order alter testcol2 drop default;
Query OK, 0 rows affected (0.00 sec)
MySQL8.0
MySQL> alter table performance_order alter testcol2 drop default;
Query OK, 0 rows affected (0.00 sec)
删除列預設值,兩個版執行本速度在一個級别。
删除列
MySQL5.7
MySQL> alter table performance_order drop column testcol2;
Query OK, 0 rows affected (1 min 19.59 sec)
MySQL> alter table performance_order drop column testcol2;
Query OK, 0 rows affected (2 min 35.51 sec)