zoukankan      html  css  js  c++  java
  • MySQL online ddl

     
    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://huanghualiang.blog.51cto.com/6782683/1596174

    前言:

    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

    实现过程:

    spacer.gif

    解决问题:

    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

    实现过程:

    spacer.gif

    本文出自 “My DBA life” 博客,请务必保留此出处http://huanghualiang.blog.51cto.com/6782683/1596174

  • 相关阅读:
    IntelliJ IDEA 常用快捷键汇总
    Git常用命令
    org.h2.jdbc.jdbcsqlexception: database is already closed (to disable automatic closing at vm shutdown, add ";db_close_on_exit=false" to the db url) [90121-197]
    AbstractErrorController
    JSR-303
    MultipartFile
    day4
    day3
    day 2
    day1
  • 原文地址:https://www.cnblogs.com/zengkefu/p/5678213.html
Copyright © 2011-2022 走看看