| 此工具默认是禁止,修改 set profiling=1;设置成功后,服务器上执行的所有语句,都会测量其耗费的时间和其它一些执行状态变更的数据。注意:此工具被标记为过期,将来回逐步弃用。
https://dev.mysql.com/doc/refman/8.0/en/performance-schema.html
The MySQL Performance Schema is a feature for monitoring MySQL Server execution at a low level。我理解为:低耗能的性能监控工具。
在mysql的5.7版本中,性能模式是默认开启的。如果想要显式的关闭的话需要修改配置文件,不能直接进行修改,会报错Variable ‘performance_schema’ is a read only variable
– 查看performance_schema的属性
mysql> SHOW VARIABLES LIKE ‘performance_schema’;
–在配置文件中修改performance_schema的属性值,on表示开启,off表示关闭
[mysqld]
performance_schema=ON
想要搞明白后续的内容,需要理解两个基本概念:
performance_schema库下的表可以按照监视不同的维度进行分组
语句事件记录表,这些表记录了语句事件信息。
–阶段事件记录表,记录语句执行的阶段事件的表
show tables like ‘%stage%’;
–事务事件记录表,记录事务相关的事件的表
show tables like ‘%transaction%’;
–监控文件系统层调用的表
show tables like ‘%file%’;
–监视内存使用的表
show tables like ‘%memory%’;
–动态对performance_schema进行配置的配置表
show tables like ‘%setup%’;
数据库刚刚初始化并启动时,并非所有的instruments(事件采集项,在采集项的配置表中每一项都有一个开关,YES 或 NO)和consumers(消费者,和事件采集项类似,也有一张表;YES就表示对应的表保存性能数据
,为NO表示对应的表不保存性能数据)都启用了,所以默认不会收集所有的事件,可能需要检测的事件并没有打开,需要进行设置,可以使用如下语句打开对应的instrument和consumers
--1、哪类的SQL执行最多?
SELECT DIGEST_TEXT,COUNT_STAR,FIRST_SEEN,LAST_SEEN FROM events_statements_summary_by_digest ORDER BY COUNT_STAR DESC
--2、哪类SQL的平均响应时间最多?
SELECT DIGEST_TEXT,AVG_TIMER_WAIT FROM events_statements_summary_by_digest ORDER BY COUNT_STAR DESC
--3、哪类SQL排序记录数最多?
SELECT DIGEST_TEXT,SUM_SORT_ROWS FROM events_statements_summary_by_digest ORDER BY COUNT_STAR DESC
--4、哪类SQL扫描记录数最多?
SELECT DIGEST_TEXT,SUM_ROWS_EXAMINED FROM events_statements_summary_by_digest ORDER BY COUNT_STAR DESC
--5、哪类SQL使用临时表最多?
SELECT DIGEST_TEXT,SUM_CREATED_TMP_TABLES,SUM_CREATED_TMP_DISK_TABLES FROM events_statements_summary_by_digest ORDER BY COUNT_STAR DESC
--6、哪类SQL返回结果集最多?
SELECT DIGEST_TEXT,SUM_ROWS_SENT FROM events_statements_summary_by_digest ORDER BY COUNT_STAR DESC
--7、哪个表物理IO最多?
SELECT file_name,event_name,SUM_NUMBER_OF_BYTES_READ,SUM_NUMBER_OF_BYTES_WRITE FROM file_summary_by_instance ORDER BY SUM_NUMBER_OF_BYTES_READ + SUM_NUMBER_OF_BYTES_WRITE DESC
--8、哪个表逻辑IO最多?
SELECT object_name,COUNT_READ,COUNT_WRITE,COUNT_FETCH,SUM_TIMER_WAIT FROM table_io_waits_summary_by_table ORDER BY sum_timer_wait DESC
--9、哪个索引访问最多?
SELECT OBJECT_NAME,INDEX_NAME,COUNT_FETCH,COUNT_INSERT,COUNT_UPDATE,COUNT_DELETE FROM table_io_waits_summary_by_index_usage ORDER BY SUM_TIMER_WAIT DESC
--10、哪个索引从来没有用过?
SELECT OBJECT_SCHEMA,OBJECT_NAME,INDEX_NAME FROM table_io_waits_summary_by_index_usage WHERE INDEX_NAME IS NOT NULL AND COUNT_STAR = 0 AND OBJECT_SCHEMA <> 'mysql' ORDER BY OBJECT_SCHEMA,OBJECT_NAME;
--11、哪个等待事件消耗时间最多?
SELECT EVENT_NAME,COUNT_STAR,SUM_TIMER_WAIT,AVG_TIMER_WAIT FROM events_waits_summary_global_by_event_name WHERE event_name != 'idle' ORDER BY SUM_TIMER_WAIT DESC
-- TIMER_START, TIMER_END, TIMER_WAIT 事件的时间信息。这些值的单位是皮秒(万亿分之一秒)
--12 开启记录
UPDATE setup_consumers SET ENABLED = 'YES' where name = 'events_statements_history_long';
--12-1、剖析某条SQL的执行情况,包括statement信息,stege信息,wait信息
SELECT EVENT_ID,sql_text FROM events_statements_history WHERE sql_text LIKE '%tickets%';
--12-2、查看每个阶段的时间消耗
SELECT event_id,EVENT_NAME,SOURCE,TIMER_END - TIMER_START FROM events_stages_history_long WHERE NESTING_EVENT_ID = 1663;
--12-3、查看每个阶段的锁等待情况
SELECT event_id,event_name,source,timer_wait,object_name,index_name,operation,nesting_event_id FROM events_waits_history_long WHERE nesting_event_id = 1663;
--13
-- 查看耗时超过5秒的sql
SELECT EVENT_ID, TRUNCATE(TIMER_WAIT/1000000000000,6) as Duration, SQL_TEXT
FROM performance_schema.events_statements_history_long
WHERE timer_wait > 5000000000000;
-- 备注 ,如果发现上表是空,记得开启生产和消费 日志
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/%';
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_%';
| 观察是否有大量线程处于不正常的状态