MySQL 评估 ALTER TABLE 进度(5.7)
一、前言
问题:大表里执行 ALTER TABLE 的时候,经常会比较忐忑,会面临 “跑又跑不完 Kill 也不敢 Kill” 的窘境。
需求:客户在执行 ALTER TABLE 时也会让我们来评估影响的计算时间,我们可以将监控方法提供给客户。
二、实验
来源于:MySQL 5.7 官方文档
启动相关服务参数:
UPDATE performance_schema.setup_instruments SET ENABLED = 'YES' WHERE NAME LIKE 'stage/innodb/alter%'; UPDATE performance_schema.setup_consumers SET ENABLED = 'YES' WHERE NAME LIKE '%stages%';
执行一个 DDL 操作:
ALTER TABLE sbtest1 ADD COLUMN middle_name varchar(200);
查询 ALTER TABLE 不断获取进度:
select stmt.SQL_TEXT as sql_text, concat(WORK_COMPLETED, '/', WORK_ESTIMATED) as progress, (stage.TIMER_END - stmt.TIMER_START) / 1e12 as current_seconds, (stage.TIMER_END - stmt.TIMER_START) / 1e12 * (WORK_ESTIMATED - WORK_COMPLETED) / WORK_COMPLETED as remaining_seconds from performance_schema.events_stages_current stage, performance_schema.events_statements_current stmt where stage.THREAD_ID = stmt.THREAD_ID and stage.NESTING_EVENT_ID = stmt.EVENT_ID;
sql_text | progress | current_seconds | remaining_seconds |
ALTER TABLE sbtest1 drop COLUMN middle_name | 68182/616441 | 2.569207816 | 20.659284092463466 |
sql_text | progress | current_seconds | remaining_seconds |
ALTER TABLE sbtest1 drop COLUMN middle_name | 421800/616441 | 23.386745981 | 10.791891001630681 |
文档维护:文若