当前位置: 首页 > news >正文

MySQL语句执行耗时分析

MySQL语句执行耗时分析

  • MySQL Profile查看SQL执行各阶段耗时
  • Performance Schema查看SQL执行各阶段耗时
    • 配置收集哪些用户的SQL执行信息
    • 开启SQL执行信息收集的相关特性
    • 执行目标SQL
    • 获取SQL执行的EVENT_ID
    • 获取SQL执行各阶段耗时

MySQL Profile查看SQL执行各阶段耗时

--开启SQL Profiling
SQL> set profiling=1; --执行目标SQL
SQL> SELECT * FROM db.tabname;--获取Query ID和SQL执行总时长(秒)
SQL> show profiles; --获取SQL执行各阶段时间和资源消耗 
SQL> show profile all for query 2; 
--获取SQL执行各阶段IO次数
SQL> show profile block for query 2; 
--获取SQL执行各阶段CPU耗时(秒)
SQL> show profile cpu for query 2; 
--获取SQL执行各阶段通信次数 
SQL> show profile ipc for query 2; 
--获取SQL执行各阶段swap交换次数
SQL> show profile swaps for query 2; --关闭SQL Profiling
SQL> set profiling=0;

🦈See more in https://dev.mysql.com/doc/refman/8.0/en/show-profile.html

Performance Schema查看SQL执行各阶段耗时

MySQL Profile目前已被列为Deprecated,官方推荐使用Performance Schema替代。不过目前Performance Schema好像还不是很完善,只能查看SQL执行各阶段的耗时,而看不到CPU和IO等资源消耗(截止8.0.32)。

配置收集哪些用户的SQL执行信息

查看搜集哪些用户的SQL执行历史信息:

select * from performance_schema.setup_actors;

限制搜集SQL执行历史信息的用户为本地root用户连接(根据实际需求设置):

update performance_schema.setup_actors 
set enabled='NO', history='NO' 
where host='%' and user='%';insert into performance_schema.setup_actors (host,user,role,enabled,history) 
values('localhost','root','%','YES','YES');select * from performance_schema.setup_actors;

开启SQL执行信息收集的相关特性

确保setup_instruments中的相关特性已开启:

update performance_schema.setup_instruments 
set enabled='YES', TIMED='YES' 
where name like '%statement/%';update performance_schema.setup_instruments 
set enabled='YES', TIMED='YES' 
where name like '%stage/%';

确保setup_consumers中的相关特性已开启:

update performance_schema.setup_consumers 
set enabled='YES' where name like '%events_statements_%';update performance_schema.setup_consumers 
set enabled='YES' where name like '%events_stages_%';

执行目标SQL

SELECT * FROM employees.employees WHERE emp_no = 10001;

获取SQL执行的EVENT_ID

events_statements_history_long中获取执行SQL的EVENT_ID:

select event_id, truncate(timer_wait/1000000000000,6) as duration, sql_text 
from performance_schema.events_statements_history_long 
where sql_text like 'SELECT%';

获取SQL执行各阶段耗时

events_stages_history_long中获取SQL执行各阶段的耗时:

--以nesting_event_id匹配上面得到的event_id
select event_name as stage, truncate(timer_wait/1000000000000,6) as duration 
from performance_schema.events_stages_history_long 
where nesting_event_id=299;

🐬See more in https://dev.mysql.com/doc/refman/8.0/en/performance-schema-query-profiling.html

http://www.lryc.cn/news/60313.html

相关文章:

  • FVM链的Themis Pro(0x,f4) 5日IDO超百万美元,领Filecoin重回高点
  • 【PMP】优秀的项目经理如何做好范围管理?
  • 【Linux】 密码相关。pwconv
  • 揭秘阿里新大招:大模型只是前菜
  • 【U8+】win10/11系统注册用友U8硬加密
  • SQL Server 服务器安装配置和使用
  • Spring常见面试题汇总
  • javaEE汽车用油加油站销售管理系统servlet
  • CSS动画
  • 《花雕学AI》21:ChatGPT能否应对脑筋急转弯?逻辑推理和创造性思维的大考验!
  • 【Vue】学习笔记-列表渲染/数据监视
  • 人工标注或成过去式?SSA语义分割框架、SSA-engine自动类别标注引擎,大幅提升细粒度语义标注效率
  • 远程工具在哪里打开?使用教程
  • HBase高手之路7—HBase之全文检索Phoneix
  • 城镇水务系统碳减排路径|雨水系统
  • 摆花
  • newman结合jenkins实现自动化测试
  • 九种 OOM 常见原因及解决方案(IT枫斗者)
  • 远程代码执行渗透与防御
  • Activiti7原生整合和工作流相关概念详解
  • 核心业务4:标的管理
  • 面向计算机视觉的深度学习:6~10
  • 【LeetCode 图论 一】初探有向图Directed Graph
  • 计算机视觉:图片数据的预处理
  • 探秘C++中的神奇组合:std--pair的魅力之旅
  • Win10搭建我的世界Minecraft服务器「内网穿透远程联机」
  • 基于springboot和ajax的简单项目 02 代码部分实现,xml文件sql语句优化 (中)
  • LNMP架构部署
  • SpringBoot 防护XSS攻击
  • iOS 吸顶效果