Mysql 8.0.12 新增 instant 算法,秒级添加字段
ALTER TABLE `member` ADD `user_from` smallint(1) NOT NULL, ALGORITHM=instant
看了网上很多回答,基本都是过时和错误的。
5.6 以后增加了ONLINE DDL,
实现不锁表增加字段和索引非常简单。
解决办法
ALTER TABLE `member` ADD `user_from` smallint(1) NOT NULL, ALGORITHM=INPLACE, LOCK=NONE
Copy
ALGORITHM表示算法:default默认(根据具体操作类型自动选择),inplace(不影响DML),copy创建临时表(锁表),INSTANT只修改元数据(8.0新增,在修改名字等极少数情况可用)
LOCK表示是否锁表:default默认,none,shared共享锁,exclusive
什么情况下会inplace,什么情况下会copy?
什么情况下会inplace,什么情况下会copy,具体参考:[Online DDL Operations](https://dev.mysql.com/doc/refman/8.0/en/innodb-online-ddl-operations.html "Online DDL Operations")
5.6以前版本解决办法
-
[GitHub’s Online Schema Migrations for MySQL](https://github.com/github/gh-ost) (gh-ost)
-
[pt-online-schema-change](https://www.percona.com/doc/percona-toolkit/2.2/pt-online-schema-change.html "pt-online-schema-change") (pt-osc)
参考
-
[MySQL Online DDL,还是要谨慎](http://database.51cto.com/art/201908/602035.htm)
-
[InnoDB and Online DDL](https://dev.mysql.com/doc/refman/8.0/en/innodb-online-ddl.html)
本条目发布于[2019年9月3日](https://c4ys.com/archives/1943 "16:18")。属于[Database](https://c4ys.com/archives/category/database)分类,被贴了 [mysql](https://c4ys.com/archives/tag/mysql) 标签。