MySQL中如何评估DDL的进度

2024-01-23 15:58:23 浏览数 (1)

这里只讨论原生DDL, 因为pt-os或者ghost的话,会自带进度的显示。

对于原生的DDL,目前有2种方法观测进度

方法1 查询ps库

在MySQL 5.7.6或者更高版本,能够通过 performance_schema 观察alter table的进度。方法如下:

代码语言:sql复制
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%';

select EVENT_NAME,WORK_COMPLETED,WORK_ESTIMATED from performance_schema.events_stages_current;

返回的结果集有3列数据:
    EVENT_NAME 执行到的阶段
    WORK_COMPLETED 已执行的耗时
    WORK_ESTIMATED 预估总的耗时

例子:

代码语言:sql复制
开一个会话,执行DDL:
alter table sbtest3 modify  pad varchar(256) ;

另开一个会话,查看:
performance_schema> select EVENT_NAME,WORK_COMPLETED,WORK_ESTIMATED from performance_schema.events_stages_current;
 ------------------------------------------- ---------------- ---------------- 
| EVENT_NAME                                | WORK_COMPLETED | WORK_ESTIMATED |
 ------------------------------------------- ---------------- ---------------- 
| stage/sql/Waiting for table metadata lock | <null>         | <null>         |
| stage/sql/copy to tmp table               | 21368          | 98712          |
 ------------------------------------------- ---------------- ---------------- 

可以看出,当前进度: 21368/98712 = 21%

方法2 查询sys库

代码语言:sql复制
开一个会话,执行DDL
alter table sbtest9 change pad pad char(90)

另开一个会话,查看:

[(none)]> select * from sys.session where conn_id != connection_id() G
*************************** 1. row ***************************
                thd_id: 20666
            conn_id: 20273
                user: root@localhost
                    db: sbtest
            command: Query
                state: copy to tmp table
                time: 34
    current_statement: alter table sbtest9 change pad pad char(90)
    execution_engine: PRIMARY 
    statement_latency: 34.44 s   --> 可以看到当前的DDL已经执行的时长
            progress: 10.53    --> 可以看到 进度10.53% 
        lock_latency: 13.00 us
        cpu_latency:   0 ps
        rows_examined: 0
            rows_sent: 0
        rows_affected: 0
            tmp_tables: 0
    tmp_disk_tables: 0
            full_scan: NO
        last_statement: NULL
last_statement_latency: NULL
        current_memory: 1.24 MiB
            last_wait: NULL
    last_wait_latency: NULL
                source: NULL
        trx_latency: 34.44 s
            trx_state: ACTIVE
        trx_autocommit: NO
                pid: 26299
        program_name: mysql

0 人点赞