从 MySQL 8.0.27 版本开始,引入了两个新参数:innodb_ddl_buffer_size 和 innodb_ddl_threads
innodb_ddl_buffer_size(默认1MB,最大4GB,可在线修改):Defines the maximum buffer size for DDL operations. The default setting is 1048576 bytes (approximately 1 MB). Applies to online DDL operations that create or rebuild secondary indexes.
innodb_ddl_threads(默认4,最大64,可在线修改):Defines the maximum number of parallel threads for the sort and build phases of index creation. Applies to online DDL operations that create or rebuild secondary indexes.
如果我们资源充足:内存够用,CPU核数够用,磁盘都有足够的容量且速度很快,我们可以通过参数设置来加快我们创建索引或者重建索引的速度。
MySQL 8.0.27缺省设置情况下:
mysql > alter table product add index idx_prod_pid_pcode_c_id (product_id, product_code, customer_id); Query OK, 0 rows affected (8 min 0.5371 sec)
复制
调整innodb_ddl_buffer_size:
mysql > SET innodb_ddl_buffer_size = 1048576000;
mysql > alter table product add index idx_prod_pid_pcode_c_id (product_id, product_code, customer_id); Query OK, 0 rows affected (2 min 47.1231 sec)
复制
执行时间大大减少,加速索引创建过程。
具体详细情况请参考官方文档:
https://dev.mysql.com/doc/refman/8.0/en/online-ddl-memory-management.html
https://dev.mysql.com/doc/refman/8.0/en/online-ddl-parallel-thread-configuration.html