mysql checking permissions 优化_mysql 性能优化

五、SQL 执行分析

5.1、SQL 执行时间分析

通过找到执行时间长的 SQL 语句,可以直观的发现数据层的效率问题。

①、通过 show processlist 来查看系统的执行情况

29132484b143794f6b76088ed82f56d8.gif

②、通过 profiling 来进行查看

这个命令是查看 SQL 的执行时间,能很直观的看出快慢。

A 查看 profiling 是否开启

0 代表还是关闭着分析功能mysql> select @@profiling;

+-------------+

| @@profiling |

+-------------+

|           0 |

+-------------+

B 打开工具mysql> set profiling=1;

Query OK, 0 rows affected (0.00 sec)

mysql> select @@profiling;

+-------------+

| @@profiling |

+-------------+

|           1 |

+-------------+

1 row in set (0.00 sec)

C 查看 SQL 的执行时间mysql> show profiles;

+----------+------------+--------------------+

| Query_ID | Duration   | Query              |

+----------+------------+--------------------+

|        1 | 0.00032000 | select @@profiling |

+----------+------------+--------------------+

D 查看 SQL 执行耗时详细信息show profile for query Query_IDmysql> show profile for query 1;

+----------------------+----------+

| Status               | Duration |

+----------------------+----------+

| starting             | 0.000073 |

| checking permissions | 0.000031 |   ---检查是否在缓存中

| Opening tables       | 0.000207 |   ---打开表

| init                 | 0.000067 |   ---初始化

| System lock          | 0.000040 |   ---锁系统

| optimizing           | 0.000005 |   ---优化查询

| statistics           | 0.000021 |

| preparing            | 0.000015 |   ---准备

| executing            | 0.000003 |   ---执行

| Sending data         | 0.000993 |

| end                  | 0.000006 |

| query end            | 0.000007 |

| closing tables       | 0.000011 |

| freeing items        | 0.000169 |

| cleaning up          | 0.000089 |

+----------------------+----------+

以上具体的信息都是从 INFORMATION_SCHEMA.PROFILING 这张表中取得的。这张表记录了所有的各个步骤的执行时间及相关信息。语法:select * from INFORMATION_SCHEMA.PROFILING where query_id = Query_ID;

③、慢查询日志

MySQL 的慢查询日志,顾名思义就是把执行时间超过设定值(默认为10s)的 SQL 记录到日志中。这项功能需要手动开启,但是开启后会造成一定的性能损耗。

A 查看慢日志是否开启

默认情况下slow_query_log的值为OFF,表示慢查询日志是禁用的,可以通过设置slow_query_log的值来开启。mysql> show variables  like "%slow_query_log%"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值