苏眠月|MySQL优化:explain、show profile和show processlist( 四 )

可以看到MySQL已经统计了上面执行的两条SQL语句

  • 如果想具体查看SQL语句各个步骤的详细耗时 , 接着执行如下SQL语句
## 查看第二条SQL语句执行耗时的详细信息show profile for query 2执行结果
+----------------------+----------+| Status| Duration |+----------------------+----------+| starting| 0.000164 || checking permissions | 0.000054 || Opening tables| 0.004434 || init| 0.000037 || System lock| 0.000013 || optimizing| 0.000007 || statistics| 0.000013 || preparing| 0.000014 || executing| 0.000004 || Sending data| 0.001350 || end| 0.000013 || query end| 0.000007 || closing tables| 0.000012 || freeing items| 0.000123 || cleaning up| 0.000018 |+----------------------+----------+15 rows in set, 1 warning (0.03 sec)执行结果展示个各个步骤以及持续的时间 。
show profile语法show profile完整的语法如下:
SHOW PROFILE [type [, type] ... ][FOR QUERY n][LIMIT row_count [OFFSET offset]]type: {ALL| BLOCK IO| CONTEXT SWITCHES| CPU| IPC| MEMORY| PAGE FAULTS| SOURCE| SWAPS}各个type对应的信息如下
苏眠月|MySQL优化:explain、show profile和show processlist也就是说除了各个步骤持续的时间 , 还可以看到BLOCK IO、CPU等信息 , 具体用法如下:
show profile block io, cpu for query 2执行结果:
+----------------------+----------+----------+------------+--------------+---------------+| Status| Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |+----------------------+----------+----------+------------+--------------+---------------+| starting| 0.000164 | 0.000000 |0.000000 |NULL |NULL || checking permissions | 0.000054 | 0.000000 |0.000000 |NULL |NULL || Opening tables| 0.004434 | 0.000000 |0.000000 |NULL |NULL || init| 0.000037 | 0.000000 |0.000000 |NULL |NULL || System lock| 0.000013 | 0.000000 |0.000000 |NULL |NULL || optimizing| 0.000007 | 0.000000 |0.000000 |NULL |NULL || statistics| 0.000013 | 0.000000 |0.000000 |NULL |NULL || preparing| 0.000014 | 0.000000 |0.000000 |NULL |NULL || executing| 0.000004 | 0.000000 |0.000000 |NULL |NULL || Sending data| 0.001350 | 0.000000 |0.000000 |NULL |NULL || end| 0.000013 | 0.000000 |0.000000 |NULL |NULL || query end| 0.000007 | 0.000000 |0.000000 |NULL |NULL || closing tables| 0.000012 | 0.000000 |0.000000 |NULL |NULL || freeing items| 0.000123 | 0.000000 |0.000000 |NULL |NULL || cleaning up| 0.000018 | 0.000000 |0.000000 |NULL |NULL |+----------------------+----------+----------+------------+--------------+---------------+15 rows in set, 1 warning (0.00 sec)补充【苏眠月|MySQL优化:explain、show profile和show processlist】需要注意的是 , show profile方式将从5.6.7开始不推荐使用 , 并且在以后的版本中会删除 , 改用Performance Schema
使用show processlistshow processlist命令可以查看当前MySQL实例的连接情况 , 用于观察是否有大量的连接处于非正常状态 。 用法非常简单 , 直接使用就行
show processlist执行结果
mysql> show processlist;+----+------+----------------+------+---------+------+-------+------------------+| Id | User | Host| db| Command | Time | State | Info|+----+------+----------------+------+---------+------+-------+------------------+|7 | root | localhost:2353 | test | Sleep|57 || NULL||8 | root | localhost:3811 | NULL | Query|0 | init| show processlist |+----+------+----------------+------+---------+------+-------+------------------+2 rows in set (0.00 sec)


推荐阅读