1.简介
版本:5.5之后
作用:属于系统库,用于收集数据库服务器性能参数
引擎:此库中的表的默认引擎,也叫做PERFORMANCE_SCHEMA
通过如下语句查看:
select table_catalog ,table_schema ,table_name ,engine
from information_schema.tables
where table_schema = 'performance_schema';
开启:5.5版本默认是不开的,需要在配置文件my.cnf中添加
[mysqld]
performance_schema=ON
查看是否开启:
mysql>show variables like 'performance_schema';
5.6版本之后是默认开启的
其中的表的含义,记录什么内容,可以参考:
https://www.cnblogs.com/cchust/p/5022148.html
https://www.cnblogs.com/cchust/p/5057498.html
2.常用
查找执行最多的SQL:
SELECT SCHEMA_NAME,DIGEST_TEXT,COUNT_STAR,SUM_ROWS_SENT,SUM_ROWS_EXAMINED,FIRST_SEEN,LAST_SEEN FROM events_statements_summary_by_digest ORDER BY COUNT_STAR desc LIMIT 1\G
此外其他列的含义如下,可以视情况加入select list
SQL的平均响应时间
AVG_TIMER_WAIT
SQL排序记录数
SUM_SORT_ROWS
SQL扫描记录数
SUM_ROWS_EXAMINED
SQL使用临时表
SUM_CREATED_TMP_TABLES,SUM_CREATED_TMP_DISK_TABLES
SQL返回结果集
SUM_ROWS_SENT
通过上述指标我们可以间接获得某类SQL的逻辑IO(SUM_ROWS_EXAMINED),CPU消耗(SUM_SORT_ROWS),网络带宽(SUM_ROWS_SENT)的对比,但还无法得到某类SQL的物理IO消耗,以及某类SQL访问数据的buffer命中率。
查找响应时间最长的SQL:
SELECT SCHEMA_NAME,DIGEST_TEXT,COUNT_STAR,AVG_TIMER_WAIT,SUM_ROWS_SENT,SUM_ROWS_EXAMINED,FIRST_SEEN,LAST_SEEN FROM events_statements_summary_by_digest ORDER BY AVG_TIMER_WAIT desc LIMIT 1\G
查找逻辑IO最高的表、文件(热数据):
SELECT FILE_NAME,EVENT_NAME,COUNT_READ,SUM_NUMBER_OF_BYTES_READ,COUNT_WRITE,SUM_NUMBER_OF_BYTES_WRITE FROM file_summary_by_instance ORDER BY SUM_NUMBER_OF_BYTES_READ+SUM_NUMBER_OF_BYTES_WRITE DESC LIMIT 2\G
查找使用最多的索引:
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 limit 1;
查找未使用过的索引:
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;
查找最耗时的等待:
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 LIMIT 1;