当前位置:网站首页>mysql优化之show profile的使用及分析
mysql优化之show profile的使用及分析
2022-07-26 06:35:00 【ganganlee】
show profile 是mysql提供的分析sql的工具之一,它的作用是记录最近执行的15条sql语句,并记录sql执行时详细的耗时、CPU、内存消耗,可以帮助我们快速定位到sql语句的问题并优化,show profile默认是关闭的,并且开启后只存活于当前会话,也就说每次使用前都需要开启。
开启状态
1、查看是否开启(on:开启,off:关闭)
show variables like 'profiling'

2、开启show profile
# 这个开启状态是session级别,意思就是你如果没有使用过一会会自动关闭
set profiling=on;

开始分析
先随便执行一些sql语句,此处略过。。。
1、查看profile列表
show profiles;

如果执行show profiles 出现
Empty set,要不然就是没有开启show profile;要不然就是没有执行sql语句
2、运行sql诊断
# 此处的Query_ID就是show profiles 查询到的Query_ID
show profile cpu,block io for query Query_ID
主要看Duration的执行时间
3、show profile的常用查询参数。
- ALL:显示所有的开销信息。
- BLOCK IO:显示块IO开销。
- CONTEXT SWITCHES:上下文切换开销。
- CPU:显示CPU开销信息。
- IPC:显示发送和接收开销信息。
- MEMORY:显示内存开销信息。
- PAGE FAULTS:显示页面错误开销信息。
- SOURCE:显示和Source_function,Source_file,Source_line相关的开销信息。
- SWAPS:显示交换次数开销信息。
4、诊断结果优化
- 如果诊断结果中出现了
ruguoconverting HEAP to MyISAM。表示查询结果太大,内存不够,数据往磁盘上搬了。 - 如果诊断结果中出现了
Creating tmp table。表示创建临时表。先拷贝数据到临时表,用完后再删除临时表。 - 如果诊断结果中出现了
Copying to tmp table on disk。把内存中临时表复制到磁盘上,危险!!! - 如果诊断结果中出现了
locked。表示执行时表被锁了。
如果在show profile诊断结果中出现了以上4条结果中的任何一条,则说明sql语句需要优化。
边栏推荐
- JVM class loading and GC garbage collection mechanism
- PG Vacuum 杂谈之 auto vacuum
- C language file operation
- 定义方法时为什么使用static关键字
- What are the main reasons for server crash
- Conda 虚拟环境envs目录为空
- Differences and relations between varchar and nvarchar in database
- English sentence pattern reference exclusive Edition - attributive clause
- [day02_0419] C language multiple choice questions
- 力扣——3. 无重复字符的最长子串
猜你喜欢
随机推荐
将一个正整数分解质因数,要求分解成尽可能小的多个的因数。
Slice and array of go
『HarmonyOS』工程的创建与虚拟机的使用
The "darkest hour" has not come yet. Cherish every bullet 2020-03-22
堆排序(heap-sort)
BPG笔记(四)
PG vacuum auto vacuum
机械制造企业如何借助ERP系统,做好生产管理?
If I want to listen to Jay Chou with you, I want you to listen to my whole youth
Heap sort
Design principle of infrared circuit of single chip microcomputer
Quick sort
力扣——4. 寻找两个正序数组的中位数
Sorting problem: bubble sort, select sort, insert sort
Code runner for vs code, with more than 40million downloads! Support more than 50 languages
nuxt 配置主题切换
Interpretation of TPS motion (cvpr2022) video generation paper
Go的map字典及约束
What is KVM? What is KVM virtual machine?
TCP protocol -- message format, connection establishment, reliable transmission, congestion control



![[day04_0421] C language multiple choice questions](/img/18/42924b5994b385a3cf132742141d88.png)





