前言:
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
does not delete duplicate rows. This has been reported as MySQL Bug #40344. The t
ADD UNIQUE INDEXIGNORE
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
實現過程: