MySQL--Online DDL

##=============================================##
在MySQL 5.6.7版本前,DDL操作主要有copy和inplace两种方式,两种方式全程都需要锁表禁止写操作,允许部分时间段的读操作。
copy方式:
(1)新建带索引的临时表
(2)锁原表,禁止DML,允许查询
(3)将原表数据拷贝到临时表(无排序,一行一行拷贝)
(4)进行rename,升级字典锁,禁止读写
(5)完成创建索引操作

inplace方式

(1)新建索引的数据字典
(2)锁表,禁止DML,允许查询
(3)读取聚集索引,构造新的索引项,排序并插入新索引
(4)等待打开当前表的所有只读事务提交
(5)创建索引结束

inplace方式仅支持添加和删除索引两种方式,由于inplace方式不需要拷贝数据因此操作较快。

##=============================================##
在MySQL 5.6.7版本后,引入了row_log来记录DDL期间写操作所产生的日志,因此除DDL操作开始和结束的两小段时间需要对表持EXCLUSIVE-MDL锁禁止读写外,其余DDL操作阶段允许其他回话对表进行读写,被称为Online DDL。
 
Online DDL操作包含copy和inplace两种方式,对于inplace方式,可细分为rebuild方式和no-rebuild方式:
1、rebuild方式指需要重新组织记录的操作如添加删除列或交换列顺序等操作,
2、no-rebuild方式指不会导致记录格式发生变化的操作如删除和添加索引。
 
ONLINE DDL可分为三个阶段操作:
Prepare阶段:
1.创建新的临时frm文件
2.持有EXCLUSIVE-MDL锁,禁止读写
3.根据alter类型,确定执行方式(copy,online-rebuild,online-norebuild)
4.更新数据字典的内存对象
5.分配row_log对象记录增量
6.生成新的临时ibd文件
ddl执行阶段:
1.降级EXCLUSIVE-MDL锁,允许读写
2.扫描old_table的聚集索引每一条记录rec
3.遍历新表的聚集索引和二级索引,逐一处理
4.根据rec构造对应的索引项
6.将构造索引项插入sort_buffer块
6.将sort_buffer块插入新的索引
7.处理ddl执行过程中产生的增量(仅rebuild类型需要)
commit阶段
1.升级到EXCLUSIVE-MDL锁,禁止读写
2.重做最后row_log中最后一部分增量
3.更新innodb的数据字典表
4.提交事务(刷事务的redo日志)
5.修改统计信息
6.rename临时idb文件,frm文件
7.变更完成  
##=============================================##
MySQL 5.6 后版本常见的DLL操作
添加/删除索引: 采用Online no-rebuild方式;
修改列名/default值/自增列值:由于只需要修改元数据,因此操作可以很快完成;
添加/删除列/交换列顺序/修改列NULL-NOTNULL属性/修改ROW-FORMAT/添加修改主键: 采用online rebuild方式,由于需要记录格式改变,需要重建表;
修改列类型/Optimize table/转换字符集:采用Offline Copy方式,需要锁表,不允许其他会话读写。
##=============================================##
MySQL 5.6 联机创建唯一索引的BUG
MySQL 在处理Row Log的时候存在BUG,会导致创建的唯一索引中可能存在不唯一KEY值的情况。
 
 
##=============================================##
如果Online DDL期间产生大量Row Log,那么会导致在回放Row log期间长时间锁表么?
答案是不会,Row Log按照Block来存放,回放Row Log时按照Block来处理,一个Block回放完后处理下一个Block,只有到达最后一个Block时才会锁表,保证最后一个Block完成后新数据和老数据保持一致,因此Online DDL期间产生大量Row Log不会导致表被长时间锁定。
每个Block的大小由参数innodb_sort_buffer_size控制。
 
参数innodb_online_alter_log_max_size用于限制在online ddl操作期间缓存dml操作的日志文件大小,在online ddl操作时允许对表的读写,并发生的dml操作日志缓存到操作日志中,在online ddl操作的最后阶段再锁表,然后应用这些操作日志,最终保证数据一致性。
Specifies an upper limit on the size of the temporary log files used during online DDL operations for InnoDB tables. There is one such log file for each index being created or table being altered. This log file stores data inserted, updated, or deleted in the table during the DDL operation. The temporary log file is extended when needed by the value of innodb_sort_buffer_size, up to the maximum specified by innodb_online_alter_log_max_size. If any temporary log file exceeds the upper size limit, the ALTER TABLE operation fails and all uncommitted concurrent DML operations are rolled back. Thus, a large value for this option allows more DML to happen during an online DDL operation, but also causes a longer period at the end of the DDL operation when the table is locked to apply the data from the log.
 
链接:http://dev.mysql.com/doc/refman/5.7/en/innodb-parameters.html#sysvar_innodb_online_alter_log_max_size
 

##=============================================##

猜你喜欢

转载自www.cnblogs.com/gaogao67/p/10390631.html