前言:
5.1 和 5.5 innodb plugin 支持Fast index create:
Fast index create 如何实现的? 只是对于 secondary index ,不需要copy table data。
执行过程:
1.判断当前表是否有未结束的transaction(commit or rollback)
2.对原表加 shared lock
2.把secondary index 用的column 排序放到memory或temp file,建立B-Tree 索引
3.unlock table
注意每一次create index 或 alter table add index 都会扫描一次clustered index,所以尽量把多个添加索引语句放在一起。
alter table xxx drop index xx_idx1,add index xx_idx2,add index xx_idx3;
Fast index create的限制:
1.新的索引,临时文件写入tempdir。
2.alter table drop index name_idx,add index name_idx;使用的是同一个索引名,使用copy table data。
3.TEMPORARY TABLE
创建index
使用copy table data。
4.ALTER TABLE ... RENAME COLUMN 为了保证innodb的数据字典和mysql的数据字典信息保持一致,使用copy table data
5.The statement ALTER IGNORE TABLE t ADD UNIQUE INDEX
does not delete duplicate rows. This has been reported as MySQL Bug #40344. The IGNORE
keyword is ignored. If any duplicate rows exist, the operation fails with the following error message:
- ERROR 23000: Duplicate entry '347' for key 'pl'
6.optimize table 更新secondary index 不用使用Fast index create。
DDL发展历程:
1.copy table
MySQL最早的DDL操作方式,DDL通过Copy Table方式实现:
-新建temp table
-锁住原表,原表可读不可写
-copy原表的数据到tempbiao
-删除原表,rename temp表
2.inplace
-在原表上直接进行ddl,锁表进行
缺点:并发度低
3.online
ddl过程不长期锁表,并发读写
1.inplace online ddl
2.copy table online ddl
二.从5.6 开始,支持Online DDL(inplace online ddl、copy table online ddl)
1.inplace online ddl
实现过程:
解决问题:
1.online ddl 过程中,add index,因缺乏新的索引,索引字典上新增一个标识:trx_id(代表索引创建过程最大的事务id),小于此trx_id的事务都不使用新索引。
2.优化(加速)Online DDL的性能:
a可通过增加innodb_sort_buffer_size参数,优化Online (Add Index/Column)操作性能;
b创建索引,排序过程,使用内存大小为innodb_sort_buffer_size的3倍;
cRow Log Block大小,等于innodb_sort_buffer_size ;
1.copy table online ddl
实现过程: