MYSQL 查询sql语句各个阶段的消耗时间

一.profiles

适用于mysql5.5以前的版本,用来查询语句在各个阶段的消耗

打开设置

mysql> set profiling =1;

任意使用一条查询语句

mysql> select count(*) from lg_student_production;

每一条语句都会生成一条queryId

mysql> show profiles;

+----------+------------+--------------------------------------------+
| Query_ID | Duration   | Query                                      |
+----------+------------+--------------------------------------------+
|        1 | 0.00330400 | show databases                             |
|        2 | 0.00013700 | SELECT DATABASE()                          |
|        3 | 0.00036700 | show tables                                |
|        4 | 0.00492625 | select count(*) from lg_student_production |
+----------+------------+--------------------------------------------+

然后我们查看对应Query_ID的语句执行情况
mysql> show profile for query 4;
+----------------------+----------+
| Status               | Duration |
+----------------------+----------+
| starting             | 0.000082 |
| checking permissions | 0.000017 |
| Opening tables       | 0.004718 |
| init                 | 0.000031 |
| System lock          | 0.000013 |
| optimizing           | 0.000006 |
| executing            | 0.000010 |
| end                  | 0.000004 |
| query end            | 0.000005 |
| closing tables       | 0.000010 |
| freeing items        | 0.000020 |
| cleaning up          | 0.000013 |
+----------------------+----------+
12 rows in set, 1 warning (0.00 sec)

mysql> show  profile cpu for query 4;
+----------------------+----------+----------+------------+
| Status               | Duration | CPU_user | CPU_system |
+----------------------+----------+----------+------------+
| starting             | 0.000082 | 0.000052 |   0.000023 |
| checking permissions | 0.000017 | 0.000012 |   0.000005 |
| Opening tables       | 0.004718 | 0.000136 |   0.003360 |
| init                 | 0.000031 | 0.000017 |   0.000008 |
| System lock          | 0.000013 | 0.000008 |   0.000003 |
| optimizing           | 0.000006 | 0.000005 |   0.000002 |
| executing            | 0.000010 | 0.000006 |   0.000003 |
| end                  | 0.000004 | 0.000003 |   0.000001 |
| query end            | 0.000005 | 0.000003 |   0.000001 |
| closing tables       | 0.000010 | 0.000007 |   0.000004 |
| freeing items        | 0.000020 | 0.000014 |   0.000006 |
| cleaning up          | 0.000013 | 0.000008 |   0.000003 |
+----------------------+----------+----------+------------+
12 rows in set, 1 warning (0.00 sec)

你可以注意到底下会有一个warning,让我们来查看一下

mysql> show warnings;
+---------+------+-------------------------------------------------------------------------------------------------------------+
| Level   | Code | Message                                                                                                     |
+---------+------+-------------------------------------------------------------------------------------------------------------+
| Warning | 1287 | 'SHOW PROFILE' is deprecated and will be removed in a future release. Please use Performance Schema instead |
+---------+------+-------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
 

因为在mysql5.5之后官方推荐了另一种查询方式Performance Schema。

mysql5.6之后的版本建议启动。

二. Performance Schema

首先需要打开数据库

mysql> use performance_schema;
Database changed


mysql> update setup_instruments set enabled='Yes',timed='yes' where name like 'stage%';
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column

如果出现这个错误不要紧,我们需要设置一下安全模式

mysql> show variables like 'SQL_SAFE_UPDATES';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| sql_safe_updates | ON    |
+------------------+-------+
1 row in set (0.00 sec)

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

mysql> show variables like 'SQL_SAFE_UPDATES';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| sql_safe_updates | OFF   |
+------------------+-------+
1 row in set (0.01 sec)
这样就好了

在执行一遍,成功

mysql> update setup_instruments set enabled='Yes',timed='yes' where name like 'stage%';

接下来启动历史记录表

mysql> update setup_consumers set enabled='Yes' where name like 'events%';


我们在执行一遍sql查询语句

mysql> select count(*) from undergraduate.lg_student_production;
+----------+
| count(*) |
+----------+
|      775 |
+----------+
1 row in set (0.00 sec)

mysql> SELECT a.THREAD_ID,SQL_TEXT,c.EVENT_NAME,(c.TIMER_END - c.TIMER_START)/1000000000 AS'DURATION (ms)'
    -> FROM events_statements_history_long a
    -> JOIN threads b ON a.`THREAD_ID`=b.`THREAD_ID`
    -> JOIN events_stages_history_long c ON c.`THREAD_ID`=b.`THREAD_ID`
    -> AND c.`EVENT_ID` BETWEEN a.EVENT_ID AND a.END_EVENT_ID
    -> WHERE b.`PROCESSLIST_ID`=CONNECTION_ID()
    -> AND a.EVENT_NAME='statement/sql/select'
    -> ORDER BY a.THREAD_ID,c.EVENT_ID;

MySQL性能架构


 

 

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值