Mysql 性能查询 show profile

300 阅读2分钟

这是我参与8月更文挑战的第20天,活动详情查看:8月更文挑战

猫和老鼠.jpg

前言:   本篇文章 是我关于MySQL的第20篇文章,水平一般、能力有限。文章写的比较浅,适合新手来看。

show profile命令

在日常的开发中,我们经常会遇到一些慢查询sql。如果想要分析一下此sql占用的开销,就可以用show profile命令来查看。然后找到问题,并针对性的进行优化。这个命令主要功能是来查看sql各部分所占用的具体时间。

开启show profile

一般来说 show profile命令是默认禁用的,我们需要把它开启。

mysql> set profiling=1;
Query OK, 0 rows affected, 1 warning (0.00 sec)

开启之后,执行的所有语句都会被记录其详细的耗时。

内部逻辑是,在服务器进行一条查询时会解析内部的执行并记录到一张临时表中。并给一个id。

首先执行一条查询sql

mysql> select * from food;
+-------+--------+------+
10003 rows in set (0.02 sec)

查询执行记录

这条查询返回了10003行,用了0.02秒。我们执行show profiles;看一下所有的执行记录.

mysql> show profiles;
+----------+------------+----------------------------------------------------------------------------------+
| Query_ID | Duration   | Query                                                                            |
+----------+------------+----------------------------------------------------------------------------------+
|        1 | 0.00012475 | select * from food                                                               |
|        2 | 0.00017675 | SELECT DATABASE()                                                                |
|        3 | 0.00017900 | SELECT DATABASE()                                                                |
|        4 | 0.00087125 | show databases                                                                   |
|        5 | 0.00093550 | show tables                                                                      |
|       6 | 0.01715250 | select * from food                                                               |
+----------+------------+----------------------------------------------------------------------------------+
6 rows in set, 1 warning (0.00 sec)

最后执行的query_id 为6的就是我刚执行的sql;

查询具体执行时间

我们再根据这条的id来详细查询它.

mysql> show profile for query 6;
+--------------------------------+----------+
| Status                         | Duration |
+--------------------------------+----------+
| starting                       | 0.000082 |
| Executing hook on transaction  | 0.000005 |
| starting                       | 0.000009 |
| checking permissions           | 0.000007 |
| Opening tables                 | 0.000035 |
| init                           | 0.000005 |
| System lock                    | 0.000012 |
| optimizing                     | 0.000005 |
| statistics                     | 0.000016 |
| preparing                      | 0.000019 |
| executing                      | 0.016889 |
| end                            | 0.000021 |
| query end                      | 0.000010 |
| closing tables                 | 0.000012 |
| freeing items                  | 0.000012 |
| cleaning up                    | 0.000015 |
+--------------------------------+----------+
16 rows in set, 1 warning (0.00 sec)

下面是详细的过程解释

  • starting //开始
  • checking permissions //检查权限
  • Opening tables //打开数据表
  • init //初始化
  • System lock //锁机制
  • optimizing //优化器
  • statistics //分析语法树
  • prepareing //预准备
  • executing //引擎执行开始
  • end //引擎执行结束
  • query end //查询结束
  • closing tables //释放数据表
  • freeing items //释放内存
  • cleaning up //彻底清理

参考文档

《高性能mysql》