MySQL的DDL

本文深入探讨MySQL的DDL,包括COPY、INPLACE、INSTANT算法以及pt-online-schema-change和gh-ost工具的使用。介绍了DDL的执行时间、负载、额外空间占用和主从同步延时等问题,并提出了应对策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述
点击此处阅读原文:MySQL的DDL

前言

DDL 一向是业务的痛点,尤其是对大型表的 DDL 操作,具有操作时间久,对性能影响大,可能影响业务正常使用等问题。

本文详细解释 MySQL DDL 的原理,以及尽可能减少 DDL 对业务的影响的办法。

MySQL DDL 的方法

MySQL 的 DDL 有很多种算法。

MySQL 本身自带三种算法,分别是:COPY、INPLACE、INSTANT。

  • COPY 算法为最古老的算法,在 MySQL 5.5 及以下为默认算法。
  • 从 MySQL 5.6 开始,引入了 INPLACE 算法并且默认使用。INPLACE 算法还包含两种类型:rebuild-table 和 not-rebuild-table。MySQL 使用 INPLACE 算法时,会自动判断,能使用 not-rebuild-table 的情况下会尽量使用,不能的时候才会使用 rebuild-table。当 DDL 涉及到主键和全文索引相关的操作时,无法使用 not-rebuild-table,必须使用 rebuild-table。其他情况下都会使用 not-rebuild-table。
  • 从 MySQL 8.0.12 开始,引入了 INSTANT 算法并且默认使用。目前 INSTANT 算法只支持增加列等少量 DDL 类型的操作,其他类型仍然会默认使用 INPLACE。

有一些第三方工具也可以实现 DDL 操作,最常见的是 percona 的 pt-online-schema-change 工具(简称为 pt-osc),和 GitHub 的 gh-ost 工具,均支持 MySQL 5.5 以上的版本。

各类工具的对比
方法 COPY INPLACE
not-rebuild-table
INPLACE
rebuild-table
INSTANT pt-osc gh-ost
DDL 过程中读取数据 允许 允许 允许 允许 允许 允许
DDL 过程中写入数据 不允许 允许 允许 允许 允许 允许
需要 MDL 需要 需要 需要 需要 需要 需要
需要额外空间
执行时间 非常长 非常长 非常短
IO 负载 非常小 非常大
导致主从同步延时 非常大 非常小
其他 支持临时暂停

推荐工具:

MySQL 版本 操作类型 主从同步延时 推荐工具
MySQL 5.5
MySQL 5.6
gh-ost
MysQL 5.7 修改索引等不涉及修改数据的操作 INPLACE(默认)
MySQL 5.7 修改数据的操作(例如增加列) 在意 gh-ost
MySQL 5.7 修改数据的操作&
### MySQL DDL Commands and Usage #### Understanding MySQL Data Definition Language (DDL) Data Definition Language (DDL) is a subset of SQL statements used to define the structure of database objects such as tables, indexes, views, etc. In MySQL 5.7, several enhancements have been made to support online operations that do not require table locks or data copying during schema changes. For instance, adding an index can be performed without locking the entire table by using specific algorithms: ```sql ALTER TABLE `table_name` ADD INDEX index_name (`column`), ALGORITHM=INPLACE; ``` This command adds an index named `index_name` on the specified column while ensuring minimal impact on concurrent transactions through the use of the `ALGORITHM=INPLACE` option[^1]. However, certain modifications may still necessitate full table scans and copies when altering columns with large character lengths like VARCHAR(256). Such alterations will result in temporary table creation along with exclusive access restrictions until completion due to limitations within MySQL's storage engine architecture[^3]: If changing fields larger than Varchar(256) based on byte count for UTF8 encoding which uses three bytes per character, then these actions would indeed cause complete table lockups alongside necessary data duplication processes. #### Managing User Privileges via DDL Statements Privilege management also falls under DDL territory where administrators might need to grant or revoke permissions over databases/tables from users. An example illustrating how one could check whether particular privileges were successfully revoked appears below: To verify if all privileges on a given database have been removed properly after executing a REVOKE statement, ```sql SHOW GRANTS FOR 'admin1'@'localhost'; ``` The output indicates only USAGE privilege remains granted meaning no other explicit rights exist anymore at this point regarding any object inside said server instance[^2]. #### Summary of Commonly Used DDL Operations Common tasks involving DDL include creating new structures, modifying existing ones, dropping unwanted elements, renaming entities, adjusting partitioning schemes among others depending upon requirements faced daily across various applications utilizing relational databases managed by systems similar to those provided here today about MySQL specifically versioned around release number five dot seven.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值