2056558.jpg 在数据库运维工作中,经常需要对大表进行表结构变更,或者跑批。长事务执行进度,一直是我们关注的问题。MySQL8.0之前的版本并不支持查看长事务的执行进度。下边我们来一起学习。 MySQL5.7 对某大表添加组合索引:

alter table test_order add index idx_uid_pstatus_fix(user_id,perform_status,fixed);

查看sys.session视图信息:

MySQL> select * from sys.session where conn_id != connection_id() and current_statement like 'alter%' \G
*************************** 1. row ***************************
                thd_id: 11790367
               conn_id: 11790331
                  user: dba@10.9.6.98
                    db: xxx_performance
               command: Query
                 state: alter table (read PK and internal sort)
                  time: 27
     current_statement: alter table test_order  ... (user_id,perform_status,fixed)
     statement_latency: 27.65 s
              progress: NULL
          lock_latency: 991.00 us
         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: 0 bytes
             last_wait: NULL
     last_wait_latency: NULL
                source: NULL
           trx_latency: NULL
             trx_state: NULL
        trx_autocommit: NULL
                   pid: 15520
          program_name: NULL
1 row in set (0.15 sec)

MySQL5.7版本虽然也有statement_latency和progress字段,但是只有statement_latency字段显示数据(执行时间),而progress字段并不返回结果。

MySQL8.0 添加索引:

alter table test_order add index idx_uid_pstatus_fix(user_id,perform_status,fixed);

查看sys.session:

mysql> select * from sys.session where conn_id != connection_id() and current_statement like 'alter%' \G
*************************** 1. row ***************************
                thd_id: 187
               conn_id: 31
                  user: root@10.8.0.8
                    db: test
               command: Query
                 state: alter table (read PK and internal sort)
                  time: 13
     current_statement: alter table test_order add ind ... (user_id,perform_status,fixed)
     statement_latency: 13.65 s
              progress: 17.84
          lock_latency: 459.00 us
         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: 10.08 MiB
             last_wait: wait/io/file/innodb/innodb_temp_file
     last_wait_latency: 625.90 us
                source: os0file.ic:473
           trx_latency: 13.64 s
             trx_state: ACTIVE
        trx_autocommit: NO
                   pid: 15520
          program_name: NULL
1 row in set (0.31 sec)

可以看到: statement_latency为13.65 s,表示此事务已执行13.65 s progress为17.84,表示执行进度为17.84%