MySQL使用show profile调优

show profile调优

有时候使用explain执行计划发现其实是命中了索引的,但是还是很慢,此时可以使用profile功能,show profile是mysql提供的可以用来分析当前会话中语句执行的资源消耗情况,如CPU、IO、IPC、SWAP以及发生的PAGE FAULTS等,可以用于SQL调优的测量

配置

默认情况下是关闭的

sql 复制代码
mysql> show variables like '%profiling%';
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| have_profiling         | YES   |
| profiling              | OFF   |
| profiling_history_size | 15    |
+------------------------+-------+

have_profiling  表示当前mysql版本支持show profile功能
profiling  表示当前功能是否开启
profiling_history_size  表示可以保留的历史数量(sql条数)

开启show profile

sql 复制代码
set profiling = 1;

查看

查看历史sql记录

sql 复制代码
-- 由于上边的配置是15,所以最多显示最近15条记录
show profiles;

根据query_id来分析该条sql的性能问题

sql 复制代码
show profile cpu,block io for query 18;
+----------------------+----------+----------+------------+--------------+---------------+
| Status               | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
+----------------------+----------+----------+------------+--------------+---------------+
| starting             | 0.000084 | 0.000067 |   0.000013 |            0 |             0 |
| checking permissions | 0.000014 | 0.000008 |   0.000005 |            0 |             0 |
| Opening tables       | 0.000039 | 0.000036 |   0.000003 |            0 |             0 |
| init                 | 0.000017 | 0.000015 |   0.000002 |            0 |             0 |
| System lock          | 0.000009 | 0.000008 |   0.000001 |            0 |             0 |
| optimizing           | 0.000004 | 0.000003 |   0.000001 |            0 |             0 |
| statistics           | 0.000011 | 0.000010 |   0.000001 |            0 |             0 |
| preparing            | 0.000009 | 0.000008 |   0.000001 |            0 |             0 |
| executing            | 0.000003 | 0.000002 |   0.000001 |            0 |             0 |
| Sending data         | 0.000039 | 0.000037 |   0.000001 |            0 |             0 |
| end                  | 0.000004 | 0.000003 |   0.000002 |            0 |             0 |
| query end            | 0.000007 | 0.000006 |   0.000001 |            0 |             0 |
| closing tables       | 0.000008 | 0.000007 |   0.000001 |            0 |             0 |
| freeing items        | 0.000016 | 0.000007 |   0.000009 |            0 |             0 |
| cleaning up          | 0.000015 | 0.000013 |   0.000001 |            0 |             0 |
+----------------------+----------+----------+------------+--------------+---------------+

该查询展示出了这条sql完整的执行生命周期

可以查询的类型有

  • all 显示所有开销
  • block io 显示块io开销
  • cpu 显示cpu开销
  • context switches 上下文切换开销
  • ipc 发送和接收相关开销
  • memory 内存开销
  • page faults 页面错误相关开销
  • source 显示Source_function、Source_file、Source_line相关开销
  • swaps 交换次数相关开销

PROFILING表

除了使用show profiles;来查询之外,还可以使用information_schema.PROFILING表来进行查询,且可以进行排序等操作

sql 复制代码
select state,sum(duration) as total_r,
round(100*sum(duration)/ (select sum(duration) from information_schema.PROFILING where query_id = 71),2) as pct_r,
count(*) as calls
from information_schema.PROFILING 
where query_id = 71
group by state
order by total_r desc

如何判断哪些是出现问题的?

如果在执行过程中存在以下情况,则需要进行优化

  • converting HEAP to MyISAM 查询结果太大,内存不够用了,开始使用磁盘
  • create tmp table 创建临时表
  • Copying to tmp table on disk 将内存中的临时表复制到磁盘
  • locked

[zhhll.icu/2021/数据库/关系...](https://link.juejin.cn?target=https%3A%2F%2Fzhhll.icu%2F2021%2F%25E6%2595%25B0%25E6%258D%25AE%25E5%25BA%2593%2F%25E5%2585%25B3%25E7%25B3%25BB%25E5%259E%258B%25E6%2595%25B0%25E6%258D%25AE%25E5%25BA%2593%2FMySQL%2F%25E8%25BF%259B%25E9%2598%25B6%2F10.show "https://zhhll.icu/2021/%E6%95%B0%E6%8D%AE%E5%BA%93/%E5%85%B3%E7%B3%BB%E5%9E%8B%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/%E8%BF%9B%E9%98%B6/10.show") profile调优/\]([zhhll.icu/2021/数据库/关系...](https://link.juejin.cn?target=https%3A%2F%2Fzhhll.icu%2F2021%2F%25E6%2595%25B0%25E6%258D%25AE%25E5%25BA%2593%2F%25E5%2585%25B3%25E7%25B3%25BB%25E5%259E%258B%25E6%2595%25B0%25E6%258D%25AE%25E5%25BA%2593%2FMySQL%2F%25E8%25BF%259B%25E9%2598%25B6%2F10.show "https://zhhll.icu/2021/%E6%95%B0%E6%8D%AE%E5%BA%93/%E5%85%B3%E7%B3%BB%E5%9E%8B%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/%E8%BF%9B%E9%98%B6/10.show") profile调优/)

本文由mdnice多平台发布

相关推荐
cr7xin7 分钟前
缓存查询逻辑及问题解决
数据库·redis·后端·缓存·go
何中应12 分钟前
Oracle数据库安装(Windows)
java·数据库·后端·oracle
遇见你的雩风33 分钟前
【MySQL】--- 视图
数据库·mysql
Thepatterraining1 小时前
大厂架构师揭秘:MySQL缓冲池为什么不用mmap?LRU-K算法详解
数据库·mysql
LB21121 小时前
Redis黑马点评 分布式锁
数据库·redis·分布式
无敌最俊朗@1 小时前
SQlite:电影院售票系统中的主键(单列,复合)约束应用
java·开发语言·数据库
全栈工程师修炼指南1 小时前
DBA | Oracle 数据备份迁移之数据泵 expdp/impdp 工具实战指南
数据库·oracle·dba
迷了璐的航1 小时前
mybatis解决查询中使用group by时出现sql_mode=only_full_group_by
数据库·sql·mybatis
金仓拾光集1 小时前
「安全升级 + 零代码平替」金仓数据库如何实现MongoDB社交动态发布系统的无缝迁移?
数据库·安全·mongodb·kingbase·kingbasees·数据库平替用金仓·金仓数据库
金仓拾光集1 小时前
告别“凭感觉”告警,金仓数据库替换MongoDB让运维更精准
运维·数据库·mongodb·kingbase·数据库平替用金仓·金仓数据库·kingbasees·