首先,检查performance_schema是否支持(performance_schema被视为存储引擎。如果该引擎可用,则应该在INFORMATION_SCHEMA.ENGINES表或SHOW ENGINES语句的输出中看到它的SUPPORT值为YES):
qogir_env@localhost : performance_schema 02:41:41> SELECT * FROM INFORMATION_SCHEMA.ENGINES WHERE ENGINE ='PERFORMANCE_SCHEMA';
+--------------------+---------+--------------------+--------------+------+------------+
| ENGINE | SUPPORT | COMMENT | TRANSACTIONS | XA | SAVEPOINTS |
+--------------------+---------+--------------------+--------------+------+------------+
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
+--------------------+---------+--------------------+--------------+------+------------+
1 row in set (0.00 sec)
qogir_env@localhost : performance_schema 02:41:54> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| CSV | YES | CSV storage engine | NO | NO | NO |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)
启用performance_schema
performance_schema在5.7.x版本中默认启用(5.6.x版本默认关闭),要显式启用或关闭时,使用参数perform_schema=ON|OFF设置,并在my.cnf中进行配置
[mysqld]
performance_schema = ON
检查启用是否成功,mysqld启动之后,通过如下语句查看是否启用成功(值为ON表示performance_schema已初始化成功且可以使用了。如果值为OFF表示在启用performance_schema时发生某些错误。可以查看错误日志进行排查)
qogir_env@localhost : performance_schema 03:13:10> SHOW VARIABLES LIKE 'performance_schema';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| performance_schema | ON |
+--------------------+-------+
1 row in set (0.00 sec)
performance_schema引擎相关的数据存储在performance_schema数据库下的相关表中。可以通过从INFORMATION_SCHEMA.tables表和直接在performance_schema库下使用show tables语句来查看有哪些performance_schema引擎表,以便可以使用show create table语句来查看表结构等信息:
qogir_env@localhost : performance_schema 03:13:22> SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES \
WHERE TABLE_SCHEMA ='performance_schema' and engine='performance_schema';
+------------------------------------------------------+
| TABLE_NAME |
+------------------------------------------------------+
| accounts |
| cond_instances |
......
| users |
| variables_by_thread |
+------------------------------------------------------+
87 rows in set (0.00 sec)
qogir_env@localhost : performance_schema 03:20:43> use performance_schema
Database changed
qogir_env@localhost : performance_schema 03:21:06> show tables from performance_schema;
+------------------------------------------------------+
| Tables_in_performance_schema |
+------------------------------------------------------+
| accounts |
| cond_instances |
......
| users |
| variables_by_thread |
+------------------------------------------------------+
87 rows in set (0.00 sec)
qogir_env@localhost : performance_schema 03:21:09> show create table performance_schema.accounts;
| Table | Create Table
| accounts | CREATE TABLE `accounts` (
`USER` char(32) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`HOST` char(60) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`CURRENT_CONNECTIONS` bigint(20) NOT NULL,
`TOTAL_CONNECTIONS` bigint(20) NOT NULL
) ENGINE=PERFORMANCE_SCHEMA DEFAULT CHARSET=utf8 |
1 row in set (0.00 sec)
performance_schema库下的表可以按照监视不同的对象进行分组,如下
# 监视语句的相关事件的表(监视器的事件数据还可以根据事件维度和是否经过聚合来分为当前事件表events_statements_current、历史事件表events_statements_history和events_statements_history_long、聚合后的摘要表*summary*,其中,summary表还可以根据帐号,主机,程序,线程,用户和全局再进行细分)
qogir_env@localhost : performance_schema 03:51:36> show tables like 'events_statement%';
+----------------------------------------------------+
| Tables_in_performance_schema (%statement%) |
+----------------------------------------------------+
| events_statements_current |
| 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 |
+----------------------------------------------------+
11 rows in set (0.00 sec)
# 监视等待事件的表
qogir_env@localhost : performance_schema 03:53:51> show tables like 'events_wait%';
+-----------------------------------------------+
| Tables_in_performance_schema (%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 |
+-----------------------------------------------+
12 rows in set (0.01 sec)
# 监视alter table的阶段事件的表
qogir_env@localhost : performance_schema 03:55:07> show tables like 'events_stage%';
+------------------------------------------------+
| Tables_in_performance_schema (%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)
# 监视事务相关的事件的表
qogir_env@localhost : performance_schema 03:55:30> show tables like 'events_transaction%';
+------------------------------------------------------+
| Tables_in_performance_schema (%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)
# 监视文件系统层调用的表
qogir_env@localhost : performance_schema 03:58:27> 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.01 sec)
# 监视内存使用的表
qogir_env@localhost : performance_schema 03:58:38> 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.01 sec)
数据库刚刚初始化并启动时,并非所有instruments和consumers都启用了,所以默认不会收集所有的事件,可能你需要检测的事件并没有打开,需要进行设置,可以使用如下两个语句打开对应的instruments和consumers(行计数可能会因MySQL版本而异):
qogir_env@localhost : performance_schema 03:34:40> UPDATE setup_instruments SET ENABLED = 'YES', TIMED = 'YES' where name like 'wait%';;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 323 Changed: 0 Warnings: 0
qogir_env@localhost : performance_schema 04:23:40> UPDATE setup_consumers SET ENABLED = 'YES' where name like '%wait%';
Query OK, 3 rows affected (0.04 sec)
Rows matched: 3 Changed: 3 Warnings: 0
要查看server当前正在做什么,可以通过查询events_waits_current表来得知,该表中每个线程只包含一行数据,用于显示每个线程的最新监视事件(正在做的事情):
qogir_env@localhost : performance_schema 04:23:52> SELECT * FROM events_waits_current limit 1\G
*************************** 1. row ***************************
THREAD_ID: 4
EVENT_ID: 60
END_EVENT_ID: 60
EVENT_NAME: wait/synch/mutex/innodb/log_sys_mutex
SOURCE: log0log.cc:1572
TIMER_START: 1582395491787124480
TIMER_END: 1582395491787190144
TIMER_WAIT: 65664
SPINS: NULL
OBJECT_SCHEMA: NULL
OBJECT_NAME: NULL
INDEX_NAME: NULL
OBJECT_TYPE: NULL
OBJECT_INSTANCE_BEGIN: 955681576
NESTING_EVENT_ID: NULL
NESTING_EVENT_TYPE: NULL
OPERATION: lock
NUMBER_OF_BYTES: NULL
FLAGS: NULL
1 row in set (0.02 sec)
# 该事件信息表示线程ID为4的线程正在等待innodb存储引擎的log_sys_mutex锁,这是innodb存储引擎的一个互斥锁,等待时间为65664皮秒(*_ID列表示事件来自哪个线程、事件编号是多少;EVENT_NAME表示检测到的具体的内容;SOURCE表示这个检测代码在哪个源文件中以及行号;计时器字段TIMER_START、TIMER_END、TIMER_WAIT分别表示该事件的开始时间、结束时间、以及总的花费时间,如果该事件正在运行而没有结束,那么TIMER_END和TIMER_WAIT的值显示为NULL。注:计时器统计的值是近似值,并不是完全精确)
_current表中每个线程只保留一条记录,且一旦线程完成工作,该表中不会再记录该线程的事件信息,_history表中记录每个线程已经执行完成的事件信息,但每个线程的只事件信息只记录10条,再多就会被覆盖掉,*_history_long表中记录所有线程的事件信息,但总记录数量是10000行,超过会被覆盖掉,咱们查看一下这个表:
qogir_env@localhost : performance_schema 06:14:08> SELECT THREAD_ID,EVENT_ID,EVENT_NAME,TIMER_WAIT FROM events_waits_history ORDER BY THREAD_ID limit 21;
+-----------+----------+------------------------------------------+------------+
| THREAD_ID | EVENT_ID | EVENT_NAME | TIMER_WAIT |
+-----------+----------+------------------------------------------+------------+
| 4 | 341 | wait/synch/mutex/innodb/fil_system_mutex | 84816 |
| 4 | 342 | wait/synch/mutex/innodb/fil_system_mutex | 32832 |
| 4 | 343 | wait/io/file/innodb/innodb_log_file | 544126864 |
| 4 | 344 | wait/synch/mutex/innodb/fil_system_mutex | 55632 |
| 4 | 345 | wait/synch/mutex/innodb/log_sys_mutex | 32832 |
| 4 | 346 | wait/synch/mutex/innodb/fil_system_mutex | 69312 |
| 4 | 347 | wait/synch/mutex/innodb/fil_system_mutex | 23712 |
| 4 | 348 | wait/io/file/innodb/innodb_log_file | 693076224 |
| 4 | 349 | wait/synch/mutex/innodb/fil_system_mutex | 65664 |
| 4 | 350 | wait/synch/mutex/innodb/log_sys_mutex | 25536 |
| 13 | 2260 | wait/synch/mutex/innodb/buf_pool_mutex | 111264 |
| 13 | 2259 | wait/synch/mutex/innodb/fil_system_mutex | 8708688 |
| 13 | 2258 | wait/synch/mutex/innodb/buf_dblwr_mutex | 43776 |
| 13 | 2267 | wait/synch/mutex/innodb/flush_list_mutex | 28272 |
| 13 | 2266 | wait/synch/mutex/innodb/buf_pool_mutex | 60192 |
| 13 | 2265 | wait/synch/mutex/innodb/fil_system_mutex | 22800 |
| 13 | 2264 | wait/synch/mutex/innodb/flush_list_mutex | 29184 |
| 13 | 2263 | wait/synch/mutex/innodb/buf_pool_mutex | 23712 |
| 13 | 2262 | wait/synch/mutex/innodb/fil_system_mutex | 23712 |
| 13 | 2261 | wait/synch/mutex/innodb/flush_list_mutex | 122208 |
| 15 | 291 | wait/synch/mutex/innodb/buf_dblwr_mutex | 37392 |
+-----------+----------+------------------------------------------+------------+
21 rows in set (0.00 sec)
summary表提供所有事件的汇总信息。该组中的表以不同的方式汇总事件数据(如:按用户,按主机,按线程等等)。例如:要查看哪些instruments占用最多的时间,可以通过对events_waits_summary_global_by_event_name表的COUNT_STAR或SUM_TIMER_WAIT列进行查询(这两列是对事件的记录数执行COUNT(*)、事件记录的TIMER_WAIT列执行SUM(TIMER_WAIT)统计而来),如下:
qogir_env@localhost : performance_schema 06:17:23> SELECT EVENT_NAME,COUNT_STAR FROM events_waits_summary_global_by_event_name \
ORDER BY COUNT_STAR DESC LIMIT 10;
| EVENT_NAME | COUNT_STAR |
+---------------------------------------------------+------------+
| wait/synch/mutex/mysys/THR_LOCK_malloc | 6419 |
| wait/io/file/sql/FRM | 452 |
| wait/synch/mutex/sql/LOCK_plugin | 337 |
| wait/synch/mutex/mysys/THR_LOCK_open | 187 |
| wait/synch/mutex/mysys/LOCK_alarm | 147 |
| wait/synch/mutex/sql/THD::LOCK_thd_data | 115 |
| wait/io/file/myisam/kfile | 102 |
| wait/synch/mutex/sql/LOCK_global_system_variables | 89 |
| wait/synch/mutex/mysys/THR_LOCK::mutex | 89 |
| wait/synch/mutex/sql/LOCK_open | 88 |
+---------------------------------------------------+------------+
qogir_env@localhost : performance_schema 06:19:20> SELECT EVENT_NAME,SUM_TIMER_WAIT FROM events_waits_summary_global_by_event_name\
ORDER BY SUM_TIMER_WAIT DESC LIMIT 10;
+----------------------------------------+----------------+
| EVENT_NAME | SUM_TIMER_WAIT |
+----------------------------------------+----------------+
| wait/io/file/sql/MYSQL_LOG | 1599816582 |
| wait/synch/mutex/mysys/THR_LOCK_malloc | 1530083250 |
| wait/io/file/sql/binlog_index | 1385291934 |
| wait/io/file/sql/FRM | 1292823243 |
| wait/io/file/myisam/kfile | 411193611 |
| wait/io/file/myisam/dfile | 322401645 |
| wait/synch/mutex/mysys/LOCK_alarm | 145126935 |
| wait/io/file/sql/casetest | 104324715 |
| wait/synch/mutex/sql/LOCK_plugin | 86027823 |
| wait/io/file/sql/pid | 72591750 |
+----------------------------------------+----------------+
# 这些结果表明,THR_LOCK_malloc互斥事件是最热的。注:THR_LOCK_malloc互斥事件仅在DEBUG版本中存在,GA版本不存在
instance表记录了哪些类型的对象会被检测。这些对象在被server使用时,在该表中将会产生一条事件记录,例如,file_instances表列出了文件I/O操作及其关联文件名:
qogir_env@localhost : performance_schema 06:27:26> SELECT * FROM file_instances limit 20;
+------------------------------------------------------+--------------------------------------+------------+
| FILE_NAME | EVENT_NAME | OPEN_COUNT |
+------------------------------------------------------+--------------------------------------+------------+
| /home/mysql/program/share/english/errmsg.sys | wait/io/file/sql/ERRMSG | 0 |
| /home/mysql/program/share/charsets/Index.xml | wait/io/file/mysys/charset | 0 |
| /data/mysqldata1/innodb_ts/ibdata1 | wait/io/file/innodb/innodb_data_file | 3 |
| /data/mysqldata1/innodb_log/ib_logfile0 | wait/io/file/innodb/innodb_log_file | 2 |
| /data/mysqldata1/innodb_log/ib_logfile1 | wait/io/file/innodb/innodb_log_file | 2 |
| /data/mysqldata1/undo/undo001 | wait/io/file/innodb/innodb_data_file | 3 |
| /data/mysqldata1/undo/undo002 | wait/io/file/innodb/innodb_data_file | 3 |
| /data/mysqldata1/undo/undo003 | wait/io/file/innodb/innodb_data_file | 3 |
| /data/mysqldata1/undo/undo004 | wait/io/file/innodb/innodb_data_file | 3 |
| /data/mysqldata1/mydata/multi_master/test.ibd | wait/io/file/innodb/innodb_data_file | 1 |
| /data/mysqldata1/mydata/mysql/engine_cost.ibd | wait/io/file/innodb/innodb_data_file | 3 |
| /data/mysqldata1/mydata/mysql/gtid_executed.ibd | wait/io/file/innodb/innodb_data_file | 3 |
| /data/mysqldata1/mydata/mysql/help_category.ibd | wait/io/file/innodb/innodb_data_file | 3 |
| /data/mysqldata1/mydata/mysql/help_keyword.ibd | wait/io/file/innodb/innodb_data_file | 3 |
| /data/mysqldata1/mydata/mysql/help_relation.ibd | wait/io/file/innodb/innodb_data_file | 3 |
| /data/mysqldata1/mydata/mysql/help_topic.ibd | wait/io/file/innodb/innodb_data_file | 3 |
| /data/mysqldata1/mydata/mysql/innodb_index_stats.ibd | wait/io/file/innodb/innodb_data_file | 3 |
| /data/mysqldata1/mydata/mysql/innodb_table_stats.ibd | wait/io/file/innodb/innodb_data_file | 3 |
| /data/mysqldata1/mydata/mysql/plugin.ibd | wait/io/file/innodb/innodb_data_file | 3 |
| /data/mysqldata1/mydata/mysql/server_cost.ibd | wait/io/file/innodb/innodb_data_file | 3 |
+------------------------------------------------------+--------------------------------------+------------+
20 rows in set (0.00 sec)
setup_*是配置表,用于配置需要监视的事件类型、监视器的时间单位以及监视数据是否需要记录在相关的表中以便查询使用等,如下
# 要查看哪些可用事件计时器,可以查询setup_timers表:
qogir_env@localhost : performance_schema 06:27:28> SELECT * FROM setup_timers;
+-------------+-------------+
| NAME | TIMER_NAME |
+-------------+-------------+
| idle | MICROSECOND |
| wait | CYCLE |
| stage | NANOSECOND |
| statement | NANOSECOND |
| transaction | NANOSECOND |
+-------------+-------------+
5 rows in set (0.00 sec)
# 要查看支持哪些事件收集instruments,以及哪些instruments被关闭或者被启动,可以查看setup_instruments表(如果要启用对应的监视器,则可以使用update语句更新ENABLED列为YES,要关闭则更新为NO。如果要启用对应监视器的时间统计功能,则可以使用update语句更新TIMED列为YES,要关闭则更新为NO)
qogir_env@localhost : performance_schema 06:35:46> SELECT * FROM setup_instruments;
+---------------------------------------------------------+---------+-------+
| NAME | ENABLED | TIMED |
+---------------------------------------------------------+---------+-------+
| wait/synch/mutex/sql/TC_LOG_MMAP::LOCK_tc | YES | YES |
| wait/synch/mutex/sql/LOCK_des_key_file | YES | YES |
| wait/synch/mutex/sql/MYSQL_BIN_LOG::LOCK_commit | YES | YES |
| wait/synch/mutex/sql/MYSQL_BIN_LOG::LOCK_commit_queue | YES | YES |
| wait/synch/mutex/sql/MYSQL_BIN_LOG::LOCK_done | YES | YES |
| wait/synch/mutex/sql/MYSQL_BIN_LOG::LOCK_flush_queue | YES | YES |
| wait/synch/mutex/sql/MYSQL_BIN_LOG::LOCK_index | YES | YES |
| wait/synch/mutex/sql/MYSQL_BIN_LOG::LOCK_log | YES | YES |
| wait/synch/mutex/sql/MYSQL_BIN_LOG::LOCK_binlog_end_pos | YES | YES |
| wait/synch/mutex/sql/MYSQL_BIN_LOG::LOCK_sync | YES | YES |
......
| memory/sql/udf_mem | NO | NO |
| memory/sql/Relay_log_info::mts_coor | NO | NO |
| wait/lock/metadata/sql/mdl | YES | YES |
+--------------------------------------------------------------------------------+---------+-------+
1028 rows in set (0.00 sec)
# performance_schema使用instruments收集到的事件来更新performance_schema数据库中的相关表,这些表充当instruments收集到的事件信息的“消费者”(consumers),setup_consumers表列出了支持的consumers,哪些被关闭了,哪些启用了(注:summary汇总表是否记录数据由配置记录global_instrumentation控制),要启用对应的consumers,则可以使用update语句更新ENABLED列为YES,要关闭则更新为NO
qogir_env@localhost : performance_schema 06:36:39> select * from setup_consumers;
+----------------------------------+---------+
| NAME | ENABLED |
+----------------------------------+---------+
| events_stages_current | YES |
| events_stages_history | YES |
| events_stages_history_long | YES |
| events_statements_current | YES |
| events_statements_history | YES |
| events_statements_history_long | NO |
| events_transactions_current | NO |
| events_transactions_history | NO |
| events_transactions_history_long | NO |
| events_waits_current | YES |
| events_waits_history | YES |
| events_waits_history_long | YES |
| global_instrumentation | YES |
| thread_instrumentation | YES |
| statements_digest | YES |
+----------------------------------+---------+
15 rows in set (0.00 sec)
有一些杂项表不属于前面提到的任何分组。例如,performance_timers列出了可用的事件计时器及其特性
qogir_env@localhost : performance_schema 06:37:14> select * from performance_timers;
+-------------+-----------------+------------------+----------------+
| TIMER_NAME | TIMER_FREQUENCY | TIMER_RESOLUTION | TIMER_OVERHEAD |
+-------------+-----------------+------------------+----------------+
| CYCLE | 3292298507 | 1 | 21 |
| NANOSECOND | 1000000000 | 1 | 378 |
| MICROSECOND | 1000000 | 1 | 378 |
| MILLISECOND | 1035 | 1 | 378 |
| TICK | 106 | 1 | 345 |
+-------------+-----------------+------------------+----------------+
5 rows in set (0.00 sec)
对于配置表的启用与禁用,如果通过直接修改setup_*这些表,在数据库重启之后,配置信息将丢失,如果需要长期保持这些配置,可以通过一些启动选项进行配置,如
performance_schema_consumer_events_stages_current=FALSE
performance_schema_consumer_events_stages_history=FALSE
performance_schema_consumer_events_stages_history_long=FALSE
performance_schema_consumer_events_statements_current=TRUE
performance_schema_consumer_events_statements_history=TRUE
performance_schema_consumer_events_statements_history_long=FALSE
performance_schema_consumer_events_transactions_current=FALSE
performance_schema_consumer_events_transactions_history=FALSE
performance_schema_consumer_events_transactions_history_long=FALSE
performance_schema_consumer_events_waits_current=FALSE
performance_schema_consumer_events_waits_history=FALSE
performance_schema_consumer_events_waits_history_long=FALSE
performance_schema_consumer_global_instrumentation=TRUE
performance_schema_consumer_statements_digest=TRUE
performance_schema_consumer_thread_instrumentation=TRUE
performance_schema_instrument[=name] #name指定在instruments表中的name名称,可以使用通配符%和_,详见链接:https://dev.mysql.com/doc/refman/5.7/en/performance-schema-startup-configuration.html,或者详见后续章节