使用SQL Profiler 性能分析器

mysql 的 sql 性能分析器主要用途是显示 sql 执行的整个过程中各项资源的使用情况。分析器可以更好的展示出不良 SQL 的性能问题所在。
下面我们举例介绍一下 MySQL SQL Profiler 的使用方法:

首先,选择库

mysql> use test;

 

然后查看 MySQL SQL Profiler 是否开启【0表示off】

mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
|           0 |
+-------------+

 

开启profile功能

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

mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
|           1 |
+-------------+

 

下面通过对比执行两条相同命令他们的执行过程的差异

创建表zhaoyj

插入三条记录

执行下列查询语句(两次)

mysql> select count(*) from zhaoyj2;
+----------+
| count(*) |
+----------+
|        3 |
+----------+

 

mysql> select count(*) from zhaoyj2;
+----------+
| count(*) |
+----------+
|        3 |
+----------+

 

下面看一下profiles的记录

mysql> show profiles;
+----------+------------+-------------------------------------------+
| Query_ID | Duration   | Query                                     |
+----------+------------+-------------------------------------------+
|        1 | 0.00040100 | select count(*) from zhaoyj2              |
|        2 | 0.00007500 | select count(*) from zhaoyj2              |
+----------+------------+-------------------------------------------+

 

看一下具体的执行过程

mysql> show profile for query 1;
+--------------------------------+----------+
| Status                         | Duration |
+--------------------------------+----------+
| starting                       | 0.000021 |
| Waiting for query cache lock   | 0.000005 |
| checking query cache for query | 0.000039 |
| checking permissions           | 0.000009 |
| Opening tables                 | 0.000021 |
| System lock                    | 0.000010 |
| Waiting for query cache lock   | 0.000037 |
| init                           | 0.000013 |
| optimizing                     | 0.000006 |
| statistics                     | 0.000011 |
| preparing                      | 0.000010 |
| executing                      | 0.000007 |
| Sending data                   | 0.000048 |
| end                            | 0.000006 |
| query end                      | 0.000006 |
| closing tables                 | 0.000009 |
| freeing items                  | 0.000007 |
| Waiting for query cache lock   | 0.000004 |
| freeing items                  | 0.000112 |
| Waiting for query cache lock   | 0.000003 |
| freeing items                  | 0.000004 |
| storing result in query cache  | 0.000005 |
| logging slow query             | 0.000003 |
| cleaning up                    | 0.000005 |
+--------------------------------+----------+
24 rows in set (0.00 sec)

 

mysql> show profile for query 2;
+--------------------------------+----------+
| Status                         | Duration |
+--------------------------------+----------+
| starting                       | 0.000024 |
| Waiting for query cache lock   | 0.000005 |
| checking query cache for query | 0.000009 |
| checking privileges on cached  | 0.000006 |
| checking permissions           | 0.000009 |
| sending cached result to clien | 0.000013 |
| logging slow query             | 0.000005 |
| cleaning up                    | 0.000004 |
+--------------------------------+----------+
8 rows in set (0.00 sec)

 

从上面的例子中我们可以清晰的看出 2 次执行 count 语句的差别, SHOW PROFILE FOR QUERY 1 展现的是第一次 count 统计的执行过程,包含了 Opening tables 、 Table lock 等操作 。而 SHOW PROFILE FOR QUERY 2 展示了第二次 count 统计的执行过程 , 第二次 count 直接从查询缓存中返回 count 统计结果,通过对比 2 次统计的总执行时间发现,缓存读的速度接近物理读的 10 倍。通过使用 SQL 性能分析器可以帮助我们对一些比较难以确定性能问题的 SQL 进行诊断,找出问题根源。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值