随笔 - 441  文章 - 4  评论 - 84  阅读 - 109万 

根据各阶段消耗的时间,确定如何进行优化。

1.使用profile

set profiling=1;

启动profile,这是一个session的配置。

执行需要优化的查询。

使用命令:

show profiles

查看每一个查询所消耗的总时间信息。

show profile for query N;

查询的每个阶段所消耗的时间 ,N 为查询出来的queryID。

image

 

image

 

image

这里可以看到查询每个阶段消耗的时间。

 

show profile cpu for query 1;

 

image

 

2.使用performmance_schema 衡量。

从5.5开始使用。

启动监控,需执行一下两条语句:

update setup_instruments set enabled='YES',TIMED='YES' WHERE NAME LIKE 'stage%';

update setup_consumers set enabled='YES' WHERE NAME like 'events%';

image

这个对全局数据库有效。

 

执行语句查看:

 

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;

 

image

posted on   自由港  阅读(701)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示