不用MariaDB/Percona也能查看DDL的进度
Posted MySQLBeginer
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了不用MariaDB/Percona也能查看DDL的进度相关的知识,希望对你有一定的参考价值。
导读
如何查看DDL的进度?
使用MariaDB/Percona版本的一个便利之处就是可以及时查看DDL的进度,进而预估DDL耗时。
其实,在官方版本里也是可以查看DDL进度的,认真看手册的同学就能发现手册中有提到过:
You can monitor ALTER TABLE progress for InnoDB tables using Performance Schema.
应该怎么做呢,我们来大概说下。
简言之,需要启用performance_schema,并设置2个地方:
[yejr@imysql]> UPDATE setup_instruments SET ENABLED = 'YES' WHERE NAME LIKE 'stage/innodb/alter%'; [yejr@imysql]> UPDATE setup_consumers SET ENABLED = 'YES' WHERE NAME LIKE '%stages%';
现在,跑个DDL看看:
[yejr@imysql]> SELECT COUNT(*) FROM t1; +----------+ | count(*) | +----------+ | 799994 | +----------+ [yejr@imysql]> ALTER TABLE t1 ADD c4 DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP;
在另一个SESSION中反复执行下面的SQL查看进度:
[yejr@imysql]> SELECT EVENT_NAME, WORK_COMPLETED, WORK_ESTIMATED FROM events_stages_current; +------------------------------------------------------+----------------+----------------+ | EVENT_NAME | WORK_COMPLETED | WORK_ESTIMATED | +------------------------------------------------------+----------------+----------------+ | stage/sql/Sending data | NULL | NULL | | stage/innodb/alter table (read PK and internal sort) | 3464 | 31227 | +------------------------------------------------------+----------------+----------------+ [yejr@imysql]> SELECT EVENT_NAME, WORK_COMPLETED, WORK_ESTIMATED FROM events_stages_current; +------------------------------------------------------+----------------+----------------+ | EVENT_NAME | WORK_COMPLETED | WORK_ESTIMATED | +------------------------------------------------------+----------------+----------------+ | stage/sql/Sending data | NULL | NULL | | stage/innodb/alter table (read PK and internal sort) | 11760 | 31227 | +------------------------------------------------------+----------------+----------------+ [yejr@imysql]> SELECT EVENT_NAME, WORK_COMPLETED, WORK_ESTIMATED FROM events_stages_current; +---------------------------------------+----------------+----------------+ | EVENT_NAME | WORK_COMPLETED | WORK_ESTIMATED | +---------------------------------------+----------------+----------------+ | stage/sql/Sending data | NULL | NULL | | stage/innodb/alter table (merge sort) | 12888 | 31227 | +---------------------------------------+----------------+----------------+ [yejr@imysql]> SELECT EVENT_NAME, WORK_COMPLETED, WORK_ESTIMATED FROM events_stages_current; +-----------------------------------+----------------+----------------+ | EVENT_NAME | WORK_COMPLETED | WORK_ESTIMATED | +-----------------------------------+----------------+----------------+ | stage/sql/Sending data | NULL | NULL | | stage/innodb/alter table (insert) | 22432 | 31227 | +-----------------------------------+----------------+----------------+ [yejr@imysql]> SELECT EVENT_NAME, WORK_COMPLETED, WORK_ESTIMATED FROM events_stages_current; +----------------------------------+----------------+----------------+ | EVENT_NAME | WORK_COMPLETED | WORK_ESTIMATED | +----------------------------------+----------------+----------------+ | stage/sql/Sending data | NULL | NULL | | stage/innodb/alter table (flush) | 34076 | 34980 | +----------------------------------+----------------+----------------+ [yejr@imysql]> SELECT EVENT_NAME, WORK_COMPLETED, WORK_ESTIMATED FROM events_stages_current; +------------------------+----------------+----------------+ | EVENT_NAME | WORK_COMPLETED | WORK_ESTIMATED | +------------------------+----------------+----------------+ | stage/sql/Sending data | NULL | NULL | +------------------------+----------------+----------------+
最后,也可以查看 events_stages_history 里记录的完整过程:
[yejr@imysql]> SELECT EVENT_NAME, WORK_COMPLETED, WORK_ESTIMATED FROM events_stages_history; +----------------------------------------------------+----------------+----------------+ | EVENT_NAME | WORK_COMPLETED | WORK_ESTIMATED | +----------------------------------------------------+----------------+----------------+ | stage/sql/System lock | NULL | NULL | | stage/sql/optimizing | NULL | NULL | | stage/sql/statistics | NULL | NULL | | stage/sql/preparing | NULL | NULL | | stage/sql/executing | NULL | NULL | | stage/sql/cleaning up | NULL | NULL | | stage/sql/starting | NULL | NULL | | stage/sql/checking permissions | NULL | NULL | | stage/sql/Opening tables | NULL | NULL | | stage/sql/init | NULL | NULL | | stage/sql/cleaning up | NULL | NULL | | stage/innodb/alter table (log apply table) | 35363 | 35363 | | stage/sql/committing alter table to storage engine | NULL | NULL | | stage/innodb/alter table (end) | 35363 | 35363 | | stage/innodb/alter table (log apply table) | 35747 | 35747 | | stage/sql/end | NULL | NULL | | stage/sql/query end | NULL | NULL | | stage/sql/closing tables | NULL | NULL | | stage/sql/freeing items | NULL | NULL | | stage/sql/logging slow query | NULL | NULL | +----------------------------------------------------+----------------+----------------+
从上面的结果我们也能看到,一个DDL执行过程包括下面几个主要阶段:
stage/innodb/alter table (read PK and internal sort),读取主键(聚集索引),计算需要处理的data page数;
stage/innodb/alter table (merge sort),处理ALTER TABLE影响的索引,每个索引跑一次(含主键索引);
stage/innodb/alter table (insert),同上;
stage/innodb/alter table (log apply index),将执行DDL期间新增的DML操作应用到index上;
stage/innodb/alter table (flush),flush阶段;
stage/innodb/alter table (log apply table),将执行DDL期间新增的DML操作应用到table上;
stage/innodb/alter table (end),收尾阶段。
再说下利用P_S查看ALTER TABLE 进度的局限性:
只支持MySQL 5.7+的版本;
只支持InnoDB引擎表;
不支持spatial indexes。
最后,我们可以运行下面的SQL,查看当前所有ALTER TABLE进度及其对应的DDL SQL:
[yejr@imysql]> SELECT ec.THREAD_ID, ec.EVENT_NAME, ec.WORK_COMPLETED, ec.WORK_ESTIMATED, pt.STATE, pt.INFO FROM performance_schema.events_stages_current ec left join performance_schema.threads th on ec.thread_id = th.thread_id left join information_schema.PROCESSLIST pt on th.PROCESSLIST_ID = pt.ID where pt.INFO like
‘ALTER%’\G
*************************** 1. row *************************** THREAD_ID: 105 EVENT_NAME: stage/innodb/alter table (merge sort) WORK_COMPLETED: 14032 WORK_ESTIMATED: 33999 STATE: altering table INFO: ALTER TABLE t1 DROP c4
文中案例的MySQL版本:5.7.16。
参考
14.16.1 Monitoring ALTER TABLE Progress for InnoDB Tables Using Performance Schema , https://dev.mysql.com/doc/refman/5.7/en/monitor-alter-table-performance-schema.html
(群号:579036588)
群内可@各位助教了解更多课程信息
知数堂
叶金荣与吴炳锡联合打造
领跑IT精英培训
行业资深专家强强联合,倾心定制
MySQL实战/MySQL优化/大数据实战 / Python/ SQL优化
数门精品课程
紧随技术发展趋势,定期优化培训教案
融入大量生产案例,贴合企业一线需求
社群陪伴学习,一次报名,可学1年
DBA、开发工程师必修课
上千位学员已华丽转身,薪资翻番,职位提升
改变已悄然发生,你还在等什么?
扫码下载知数堂精品课程试听视频
(MySQL 实战/优化、大数据实战、Python开发,及SQL优化等课程)
密码:hg3h
以上是关于不用MariaDB/Percona也能查看DDL的进度的主要内容,如果未能解决你的问题,请参考以下文章
第184 期Microsoft Forms——不用问卷星也能轻松做问卷收数据
Oracle利用dbms_metadata.get_ddl查看DDL语句