让MySQL为我们记录执行流程

让MySQL为我们记录执行流程

 

我们可以开启profiling,让MySQL为我们记录SQL语句的执行流程

 

  • 查看profiling参数

    shell > select @@profiling;
    
  • 进行更改

    shell > set profiling = 1;
    
  • 执行语句

    select * from employees limit 100;
    
  • 查看该语句执行流程

    show profile;
    

    分别是

    • 启动
    • 权限检查
    • 打开表
    • 初始化表
    • 锁系统
    • 优化查询
    • 统计
    • 优化
    • 执行
    • 发送数据
    • 结束
    • 查询结束
    • 关闭表
    • 释放
    • 清理

 

  • 请注意上述命令与下面的不同

    show profiles;
    

    显示完成每条指令的所花费的时间。上面show profile是对一条指令每个阶段花费时间的统计,这个统计的所有时间的总和就等于show profiles中对应命令的Duration。

 

补充

(0812)

  • 查询profile设置

    show variables like ‘%profil%’;
    
    +------------------------+-------+
    | Variable_name          | Value |
    +------------------------+-------+
    | have_profiling         | YES   |
    | profiling              | OFF   |
    | profiling_history_size | 15    |
    +------------------------+-------+
    3 rows in set (0.10 sec)
    
  • 基于会话基本开启

    set profiling = 1;
    
  • 查询某条语句的性能消耗情况

show profile for query 1;
# query 1 是 query id
  • 查询cpu,内存和I\O消耗情况

    show profile cpu,memory,block io for query 1;
    
  • 查看查询过程每一步对应的源码文件、函数名、源文件行数

    show profile source for query 1;
    
  • show profiles 在mysql5.7.2中已经被废除。

 

posted on 2020-06-22 23:29  G-Aurora  阅读(189)  评论(0编辑  收藏  举报