确定查询各阶段消耗的时间

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

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

原文地址:https://www.cnblogs.com/yg_zhang/p/5935966.html