XXXXX
C/C++
XXXXX
C#/.net
XXXXX
js
java
java
开发导航 开发导航 www.endv.cn
天云

Mysql Performance_schema简介, 表和常用性能查询

简介
performance_schema是运行在较低级别的用于监控MySQL Server运行过程中的资源消耗、资源等待等情况的一个功能特性,也是一个存储引擎。该特性具有以下特点。

提供了一种在数据库运行时实时检查Server内部执行情况的方法
可监控任何事情以及对应的时间消耗,利用这些信息来判断Server中的相关资源消耗
只被记录在本地Server的performance_schema中,其表中数据发生变化时不会被写入binlog中,也不会通过复制机制被复制到其他Server中
对于这些表可使用SELECT语句查询,也可以使用SQL语句更新performance_schema数据库中的表记录,但不建议更新,会影响后续的数据收集
表中数据不会持久化存储在磁盘中,而是保存在内存中,一旦服务器重启,这些数据就会丢失
不会导致server的行为发生变化(查询,优化等)
总体上开销有限也不会影响性能
对某事件监测失败,不影响server正常运行
当针对一个数据,同时被performance_schema收集和查询,则收集优先与查询
事件监测点可进行配置
使用
查看是否当前数据库是否支持
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)

Support为yes则表示支持该特性

查看该特性是否使能
mysql> show variables like 'performance_schema';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| performance_schema | ON |
+--------------------+-------+
1 row in set (0.02 sec)
如果为off则表示未使能。该参数为只读参数,则可在mysql.cnf中进行配置后重启服务

查看有哪些表使用了该引擎
mysql> use performance_schema;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+------------------------------------------------------+
| Tables_in_performance_schema |
+------------------------------------------------------+
| accounts |
| binary_log_transaction_compression_stats |
| cond_instances |
| data_lock_waits |
| data_locks
使用该引擎的这些表的分类
通过查询,在mysql 8.0版本中,共有110个表。这些表根据监测维度不同,我们将其分为:

1)语句事件记录表

mysql> show tables like 'events_statement%';
+----------------------------------------------------+
| Tables_in_performance_schema (events_statement%) |
+----------------------------------------------------+
| events_statements_current | //当前语句事件表
| events_statements_histogram_by_digest | //历史语句事件表
| events_statements_histogram_global |
| events_statements_history |
| events_statements_history_long | //长语句历史事件表
| events_statements_summary_by_account_by_event_name | //聚合后的摘要表
| events_statements_summary_by_digest |
| events_statements_summary_by_host_by_event_name |
| events_statements_summary_by_program |
| events_statements_summary_by_thread_by_event_name |
| events_statements_summary_by_user_by_event_name |
| events_statements_summary_global_by_event_name |
+----------------------------------------------------+
12 rows in set (0.00 sec)
2)等待时间记录表

mysql> show tables like 'events_wait%';
+-----------------------------------------------+
| Tables_in_performance_schema (events_wait%) |
+-----------------------------------------------+
| events_waits_current |
| events_waits_history |
| events_waits_history_long |
| events_waits_summary_by_account_by_event_name |
| events_waits_summary_by_host_by_event_name |
| events_waits_summary_by_instance |
| events_waits_summary_by_thread_by_event_name |
| events_waits_summary_by_user_by_event_name |
| events_waits_summary_global_by_event_name |
+-----------------------------------------------+
9 rows in set (0.00 sec)
3)阶段事件记录表

mysql> show tables like 'events_stage%';
+------------------------------------------------+
| Tables_in_performance_schema (events_stage%) |
+------------------------------------------------+
| events_stages_current |
| events_stages_history |
| events_stages_history_long |
| events_stages_summary_by_account_by_event_name |
| events_stages_summary_by_host_by_event_name |
| events_stages_summary_by_thread_by_event_name |
| events_stages_summary_by_user_by_event_name |
| events_stages_summary_global_by_event_name |
+------------------------------------------------+
8 rows in set (0.00 sec)
4)事务事件记录表

mysql> show tables like 'events_transaction%';
+------------------------------------------------------+
| Tables_in_performance_schema (events_transaction%) |
+------------------------------------------------------+
| events_transactions_current |
| events_transactions_history |
| events_transactions_history_long |
| events_transactions_summary_by_account_by_event_name |
| events_transactions_summary_by_host_by_event_name |
| events_transactions_summary_by_thread_by_event_name |
| events_transactions_summary_by_user_by_event_name |
| events_transactions_summary_global_by_event_name |
+------------------------------------------------------+
8 rows in set (0.00 sec)
5)监控文件系统层调用的表

mysql> show tables like '%file%';
+---------------------------------------+
| Tables_in_performance_schema (%file%) |
+---------------------------------------+
| file_instances |
| file_summary_by_event_name |
| file_summary_by_instance |
+---------------------------------------+
3 rows in set (0.00 sec)
6)监视内存使用的表

mysql> show tables like '%memory%';
+-----------------------------------------+
| Tables_in_performance_schema (%memory%) |
+-----------------------------------------+
| memory_summary_by_account_by_event_name |
| memory_summary_by_host_by_event_name |
| memory_summary_by_thread_by_event_name |
| memory_summary_by_user_by_event_name |
| memory_summary_global_by_event_name |
+-----------------------------------------+
5 rows in set (0.00 sec)
7)动态对performance_schema进行配置的配置表

mysql> show tables like '%setup%';
+----------------------------------------+
| Tables_in_performance_schema (%setup%) |
+----------------------------------------+
| setup_actors |
| setup_consumers |
| setup_instruments |
| setup_objects |
| setup_threads |
+----------------------------------------+
5 rows in set (0.01 sec)
两个基本概念
● instruments:生产者,用于采集MySQL中各种操作产生的事件信息,对应配置表中的配置项,我们可以称之为事件采集配置项。以下提及的生产者均统称为instruments。

● consumers:消费者,对应的消费者表用于存储来自instruments采集的数据,对应配置表中的配置项,我们可以称之为消费存储配置项。以下提及的消费者均统称为consumers。

查询性能表进行进行分析

​ 基本了解了表的相关信息之后,可以通过这些表进行实际的查询操作来进行实际的分析。

1、哪类的SQL执行最多?

mysql> SELECT DIGEST_TEXT,COUNT_STAR,FIRST_SEEN,LAST_SEEN FROM events_statements_summary_by_digest ORDER BY COUNT_STAR DESC
-> ;
+----------------------------------------------------------------------------------------------------------------------------------------+------------+----------------------------+----------------------------+
| DIGEST_TEXT | COUNT_STAR | FIRST_SEEN | LAST_SEEN |
+----------------------------------------------------------------------------------------------------------------------------------------+------------+----------------------------+----------------------------+
| SHOW VARIABLES LIKE ? | 4 | 2022-01-04 18:14:24.153156 | 2022-01-04 21:25:12.396097 |
| SELECT NAME , TYPE , `thread_id` , `processlist_id` FROM `performance_schema` . `threads` WHERE TYPE = ? | 3 | 2022-01-04 18:06:55.067124 | 2022-01-04 18:21:09.809550 |
2、哪类SQL的平均响应时间最多?

mysql> SELECT DIGEST_TEXT,AVG_TIMER_WAIT FROM events_statements_summary_by_digest ORDER BY COUNT_STAR DESC ;
+----------------------------------------------------------------------------------------------------------------------------------------+----------------+
| DIGEST_TEXT | AVG_TIMER_WAIT |
+----------------------------------------------------------------------------------------------------------------------------------------+----------------+
| SHOW VARIABLES LIKE ? | 5867400000 |
| SELECT NAME , TYPE , `thread_id` , `processlist_id` FROM `performance_schema` . `threads` WHERE TYPE = ? | 42246300000 |
3、哪类SQL排序记录数最多?

mysql> SELECT DIGEST_TEXT,SUM_SORT_ROWS FROM events_statements_summary_by_digest ORDER BY COUNT_STAR DESC;
+----------------------------------------------------------------------------------------------------------------------------------------+---------------+
| DIGEST_TEXT | SUM_SORT_ROWS |
+----------------------------------------------------------------------------------------------------------------------------------------+---------------+
| SHOW VARIABLES LIKE ? | 0 |
4、哪类SQL扫描记录数最多?

mysql> SELECT DIGEST_TEXT,SUM_ROWS_EXAMINED FROM events_statements_summary_by_digest ORDER BY COUNT_STAR DESC;
+----------------------------------------------------------------------------------------------------------------------------------------+-------------------+
| DIGEST_TEXT | SUM_ROWS_EXAMINED |
+----------------------------------------------------------------------------------------------------------------------------------------+-------------------+
| SHOW VARIABLES LIKE ? | 51 |
5、哪类SQL使用临时表最多?

mysql> SELECT DIGEST_TEXT,SUM_CREATED_TMP_TABLES,SUM_CREATED_TMP_DISK_TABLES FROM events_statements_summary_by_digest ORDER BY COUNT_STAR DESC;
+----------------------------------------------------------------------------------------------------------------------------------------+------------------------+-----------------------------+
| DIGEST_TEXT | SUM_CREATED_TMP_TABLES | SUM_CREATED_TMP_DISK_TABLES |
+----------------------------------------------------------------------------------------------------------------------------------------+------------------------+-----------------------------+
| SHOW VARIABLES LIKE ? | 4 | 0 |
| SELECT NAME , TYPE , `thread_id` , `processlist_id` FROM `performance_schema` . `threads` WHERE TYPE = ? | 0 | 0 |
6、哪类SQL返回结果集最多?

mysql> SELECT DIGEST_TEXT,SUM_ROWS_SENT FROM events_statements_summary_by_digest ORDER BY COUNT_STAR DESC;
+-------------------------------------------------------------------------------------------------------------------------------------------------------+---------------+
| DIGEST_TEXT | SUM_ROWS_SENT |
+-------------------------------------------------------------------------------------------------------------------------------------------------------+---------------+
| SHOW VARIABLES LIKE ? | 51 |
| SELECT NAME , TYPE , `thread_id` , `processlist_id` FROM `performance_schema` . `threads` WHERE TYPE = ? | 38 |
7、哪个表物理IO最多?

mysql> 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;
+------------------------------------------+---------------------------------------+--------------------------+---------------------------+
| file_name | event_name | SUM_NUMBER_OF_BYTES_READ | SUM_NUMBER_OF_BYTES_WRITE |
+------------------------------------------+---------------------------------------+--------------------------+---------------------------+
| /var/lib/mysql/ibtmp1 | wait/io/file/innodb/innodb_data_file | 0 | 14729216 |
| /var/lib/mysql/#ib_16384_1.dblwr | wait/io/file/innodb/innodb_dblwr_file | 8585216 | 0 |
8、哪个表逻辑IO最多?

mysql> 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;
+------------------------------------------------------+------------+-------------+-------------+----------------+
| object_name | COUNT_READ | COUNT_WRITE | COUNT_FETCH | SUM_TIMER_WAIT |
+------------------------------------------------------+------------+-------------+-------------+----------------+
| books | 1 | 1 | 1 | 14683357736 |
| dd_properties | 0 | 0 | 0 | 0 |
9、哪个索引访问最多?

mysql> 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;
+------------------------------------------------------+----------------------------------+-------------+--------------+--------------+--------------+
| OBJECT_NAME | INDEX_NAME | COUNT_FETCH | COUNT_INSERT | COUNT_UPDATE | COUNT_DELETE |
+------------------------------------------------------+----------------------------------+-------------+--------------+--------------+--------------+
| books | NULL | 1 | 1 | 0 | 0 |
10、哪个索引从来没有用过?

mysql> 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;
+--------------------+------------------------------------------------------+----------------------------------+
| OBJECT_SCHEMA | OBJECT_NAME | INDEX_NAME |
+--------------------+------------------------------------------------------+----------------------------------+
| cara_testing_db | books | PRIMARY |
| performance_schema | accounts | ACCOUNT |
11、哪个等待事件消耗时间最多?

mysql> 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 ;
+---------------------------------------------------------------------+------------+----------------+----------------+
| EVENT_NAME | COUNT_STAR | SUM_TIMER_WAIT | AVG_TIMER_WAIT |
+---------------------------------------------------------------------+------------+----------------+----------------+
| wait/io/file/innodb/innodb_data_file | 1103 | 686179063712 | 622102240 |
| wait/io/file/innodb/innodb_dblwr_file | 30 | 118918612832 | 3963953732 |
| wait/io/file/innodb/innodb_log_file | 109 | 66884431760 | 613618552 |
12、剖析某条SQL的执行情况,包括statement信息,stege信息,wait信息

mysql> SELECT EVENT_ID,sql_text FROM events_statements_history WHERE sql_text LIKE '%count(*)%';
+----------+----------------------------+
| EVENT_ID | sql_text |
+----------+----------------------------+
| 155 | select count(*) from books |
+----------+----------------------------+
1 row in set (0.00 sec)
13、查看每个阶段的时间消耗

mysql> SELECT event_id,EVENT_NAME,SOURCE,TIMER_END - TIMER_START FROM events_stages_history_long WHERE NESTING_EVENT_ID = 1553
-> ;
Empty set (0.01 sec)
14、查看每个阶段的锁等待情况

mysql> SELECT event_id,event_name,source,timer_wait,object_name,index_name,operation,nesting_event_id FROM events_waits_history_long;

———————————————— 

 

posted @ 2023-10-23 17:25  Endv  阅读(665)  评论(0编辑  收藏  举报