--查看配置表如下:
mysql> SELECT table_name
FROM information_schema.tables t
WHERE t.table_schema='performance_schema'
AND t.table_name LIKE 'setup%' ;
+-------------------+
| table_name |
+-------------------+
| setup_actors | --配置监控的范围
| setup_consumers | --配置监控的类型
| setup_instruments | --配置等待事件的类型
| setup_objects | --配置监控的对象
| setup_timers | --配置时间计量单位(皮秒),8.0版本会被废除
+-------------------+
5 rows in set (0.00 sec)
--1、查看监控的范围,默认监控所有的host,所有的user
mysql> SELECT * FROM performance_schema.setup_actors;
+------+------+------+---------+---------+
| HOST | USER | ROLE | ENABLED | HISTORY |
+------+------+------+---------+---------+
| % | % | % | YES | YES |
+------+------+------+---------+---------+
1 row in set (0.00 sec)
The setup_actors table has these columns:
HOST:The host name. This should be a literal name, or '%' to mean “any host.”
USER:The user name. This should be a literal name, or '%' to mean “any user.”
ROLE:Unused.
ENABLED:Whether to enable instrumentation for foreground threads matched by the row. The value is YES or NO.
HISTORY:Whether to log historical events for foreground threads matched by the row. The value is YES or NO.
--2、查看当前监控的类型(写入到哪些监控的统计表里)和是否开启监控,默认开启如下
mysql> SELECT * FROM performance_schema.setup_consumers;
+----------------------------------+---------+
| NAME | ENABLED |
+----------------------------------+---------+
| events_stages_current | NO |
| events_stages_history | NO |
| events_stages_history_long | NO |
| 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 | NO |
| events_waits_history | NO |
| events_waits_history_long | NO |
| global_instrumentation | YES |
| thread_instrumentation | YES |
| statements_digest | YES |
+----------------------------------+---------+
15 rows in set (0.00 sec)
--3、查看等待事件的类型和是否开启了这种类型的记录
--1022种类型,这里按等待类型查看数量
mysql> SELECT SUBSTRING_INDEX(NAME, '/', 1),COUNT(1)
-> FROM performance_schema.setup_instruments
-> GROUP BY 1
-> ORDER BY 2 DESC;
+-------------------------------+----------+
| SUBSTRING_INDEX(NAME, '/', 1) | COUNT(1) |
+-------------------------------+----------+
| memory | 377 |
| wait | 321 | --等待事件
| statement | 193 | --语句执行的信息
| stage | 129 | --语句执行阶段的时间统计
| idle | 1 | --空闲
| transaction | 1 |
+-------------------------------+----------+
6 rows in set (0.00 sec)
--4、查看监控的对象, 默认对mysql、performance_schema、information_schema三个库的用户对象都不做监控
mysql> SELECT * FROM performance_schema.setup_objects;
+-------------+--------------------+-------------+---------+-------+
| OBJECT_TYPE | OBJECT_SCHEMA | OBJECT_NAME | ENABLED | TIMED |
+-------------+--------------------+-------------+---------+-------+
| EVENT | mysql | % | NO | NO |
| EVENT | performance_schema | % | NO | NO |
| EVENT | information_schema | % | NO | NO |
| EVENT | % | % | YES | YES |
| FUNCTION | mysql | % | NO | NO |
| FUNCTION | performance_schema | % | NO | NO |
| FUNCTION | information_schema | % | NO | NO |
| FUNCTION | % | % | YES | YES |
| PROCEDURE | mysql | % | NO | NO |
| PROCEDURE | performance_schema | % | NO | NO |
| PROCEDURE | information_schema | % | NO | NO |
| PROCEDURE | % | % | YES | YES |
| TABLE | mysql | % | NO | NO |
| TABLE | performance_schema | % | NO | NO |
| TABLE | information_schema | % | NO | NO |
| TABLE | % | % | YES | YES |
| TRIGGER | mysql | % | NO | NO |
| TRIGGER | performance_schema | % | NO | NO |
| TRIGGER | information_schema | % | NO | NO |
| TRIGGER | % | % | YES | YES |
+-------------+--------------------+-------------+---------+-------+
20 rows in set (0.00 sec)
--查看时间单位计量,都是皮秒(1秒=1000000000000皮秒),8.0版本废除的表
mysql> SELECT * FROM performance_schema.setup_timers;
+-------------+-------------+
| NAME | TIMER_NAME |
+-------------+-------------+
| idle | MICROSECOND |
| wait | CYCLE |
| stage | NANOSECOND |
| statement | NANOSECOND |
| transaction | NANOSECOND |
+-------------+-------------+
5 rows in set, 1 warning (0.00 sec)
记录执行语句的等待事件表 , 官方文档:https://dev.mysql.com/doc/refman/5.7/en/performance-schema-statement-tables.html
--查看可记录执行语句的等待事件的表,如下:
mysql> SELECT table_name
FROM information_schema.tables t
WHERE t.table_schema='performance_schema'
AND t.table_name LIKE 'events_statements%' ;
+----------------------------------------------------+
| table_name |
+----------------------------------------------------+
| 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 |
+----------------------------------------------------+
10 rows in set (0.00 sec)
--查看是否开启了配置信息,写入到表中
mysql> SELECT *
FROM performance_schema.setup_consumers
WHERE NAME LIKE '%statements%';
+--------------------------------+---------+
| NAME | ENABLED |
+--------------------------------+---------+
| events_statements_current | YES |
| events_statements_history | YES |
| events_statements_history_long | NO |
| statements_digest | YES |
+--------------------------------+---------+
4 rows in set (0.00 sec)
mysql> SELECT *
FROM performance_schema.setup_instruments
WHERE NAME LIKE 'statement/%';
+---------------------------------------+---------+-------+
| NAME | ENABLED | TIMED |
+---------------------------------------+---------+-------+
| statement/sql/select | YES | YES |
| statement/sql/create_table | YES | YES |
| ......... |
| statement/abstract/new_packet | YES | YES |
| statement/abstract/relay_log | YES | YES |
+---------------------------------------+---------+-------+
193 rows in set (0.01 sec)
--可以在my.cnf配置,永久的开启配置信息来记录等待事件执行语句到表中
[mysqld]
performance-schema-instrument='statement/%=ON'
performance-schema-consumer-events-statements-current=ON
performance-schema-consumer-events-statements-history=ON
performance-schema-consumer-events-statements-history-long=ON
performance-schema-consumer-statements-digest=ON
--查看history历史表记录的数据量,history记录每个线程的最近10条,history_long记录所有线程的最近10000条
mysql> show variables like 'performance_schema_events_statements_history%';
+--------------------------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------------------------+-------+
| performance_schema_events_statements_history_long_size | 10000 |
| performance_schema_events_statements_history_size | 10 |
+--------------------------------------------------------+-------+
2 rows in set (0.01 sec)
--查看历史执行时间最长的前5条sql记录,如下:
mysql> SELECT thread_id
,event_id
,event_name
,(timer_end-timer_start)/1000000000000 exec_time_sec
,lock_time/1000000000000 lock_time_sce
,SUBSTR(sql_text,1,50)
FROM performance_schema.events_statements_history
ORDER BY 4 DESC
LIMIT 5;
+-----------+----------+---------------------------+---------------+---------------+----------------------------------------------------+
| thread_id | event_id | event_name | exec_time_sec | lock_time_sce | SUBSTR(sql_text,1,50) |
+-----------+----------+---------------------------+---------------+---------------+----------------------------------------------------+
| 29 | 15305 | statement/sql/select | 0.0041 | 0.0037 | EXPLAIN select thread_id,event_id , |
| 29 | 15237 | statement/sql/show_status | 0.0018 | 0.0001 | SHOW STATUS |
| 29 | 15189 | statement/sql/show_status | 0.0016 | 0.0001 | SHOW STATUS |
| 32 | 341 | statement/sql/select | 0.0012 | 0.0001 | SELECT * FROM performance_schema.setup_inst |
| 32 | 397 | statement/sql/select | 0.0012 | 0.0002 | SELECT thread_id ,event_id ,event_na |
+-----------+----------+---------------------------+---------------+---------------+----------------------------------------------------+
5 rows in set (0.00 sec)
记录执行语句的等待事件表 , 官方文档:https://dev.mysql.com/doc/refman/5.7/en/performance-schema-stage-tables.html
--查看可记录语句每步执行时间的等待事件的表,如下:
mysql> SELECT table_name
FROM information_schema.tables t
WHERE t.table_schema='performance_schema'
AND t.table_name LIKE 'events_stages%';
+------------------------------------------------+
| table_name |
+------------------------------------------------+
| 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)
--查看是否开启了配置信息,写入到表中
mysql> SELECT *
FROM performance_schema.setup_consumers
WHERE NAME LIKE '%stages%';
+----------------------------+---------+
| NAME | ENABLED |
+----------------------------+---------+
| events_stages_current | NO |
| events_stages_history | NO |
| events_stages_history_long | NO |
+----------------------------+---------+
3 rows in set (0.00 sec)
mysql> SELECT *
FROM performance_schema.setup_instruments
WHERE NAME LIKE 'stage%';
+----------------------------------------------------------------------------+---------+-------+
| NAME | ENABLED | TIMED |
+----------------------------------------------------------------------------+---------+-------+
| stage/sql/After create | NO | NO |
| stage/sql/allocating local table | NO | NO |
| stage/sql/preparing for alter table | NO | NO |
| stage/sql/altering table | NO | NO |
| stage/sql/committing alter table to storage engine | NO | NO |
| ......
| stage/innodb/buffer pool load | YES | YES |
+----------------------------------------------------------------------------+---------+-------+
129 rows in set (0.00 sec)
--可以在my.cnf配置,永久的开启配置信息来记录等待事件执行语句到表中
[mysqld]
performance-schema-instrument='stage/%=ON'
performance-schema-consumer-events-stages-current=ON
performance-schema-consumer-events-stages-history=ON
performance-schema-consumer-events-stages-history-long=ON
--查看history历史表记录的数据量,history记录每个线程的最近10条,history_long记录所有线程的最近10000条
mysql> show variables like 'performance_schema_events_stages_history%';
+----------------------------------------------------+-------+
| Variable_name | Value |
+----------------------------------------------------+-------+
| performance_schema_events_stages_history_long_size | 10000 |
| performance_schema_events_stages_history_size | 10 |
+----------------------------------------------------+-------+
2 rows in set (0.00 sec)
--查看历史执行时间最长的前5条每步具体时间,如下:
mysql> SELECT thread_id
,event_id
,event_name
,(timer_end-timer_start)/1000000000000 exec_time_sec
,timer_wait/1000000000000 timer_wait_sce
FROM performance_schema.events_stages_history
ORDER BY 4 DESC
LIMIT 5;
+-----------+----------+-------------------------------+---------------+----------------+
| thread_id | event_id | event_name | exec_time_sec | timer_wait_sce |
+-----------+----------+-------------------------------+---------------+----------------+
| 24 | 1 | stage/innodb/buffer pool load | 0.6036 | 0.6036 |
| 1 | 200 | stage/sql/System lock | 0.0081 | 0.0081 |
| 1 | 205 | stage/sql/Opening tables | 0.0029 | 0.0029 |
| 1 | 199 | stage/sql/Opening tables | 0.0005 | 0.0005 |
| 1 | 208 | stage/sql/System lock | 0.0003 | 0.0003 |
+-----------+----------+-------------------------------+---------------+----------------+
5 rows in set (0.00 sec)
记录执行语句的等待事件表 , 官方文档:https://dev.mysql.com/doc/refman/5.7/en/performance-schema-wait-tables.html
--查看可记录语句每步执行时间的等待事件的表,如下:
mysql> SELECT table_name
FROM information_schema.tables t
WHERE t.table_schema='performance_schema'
AND t.table_name LIKE 'events_waits%';
+-----------------------------------------------+
| table_name |
+-----------------------------------------------+
| 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.17 sec)
--查看是否开启了配置信息,写入到表中
mysql> SELECT *
FROM performance_schema.setup_consumers
WHERE NAME LIKE '%waits%';
+---------------------------+---------+
| NAME | ENABLED |
+---------------------------+---------+
| events_waits_current | NO |
| events_waits_history | NO |
| events_waits_history_long | NO |
+---------------------------+---------+
3 rows in set (0.04 sec)
mysql> SELECT *
FROM performance_schema.setup_instruments
WHERE NAME LIKE 'wait/%';
+--------------------------------------------------------------------+---------+-------+
| NAME | ENABLED | TIMED |
+--------------------------------------------------------------------+---------+-------+
| wait/synch/mutex/sql/TC_LOG_MMAP::LOCK_tc | NO | NO |
| wait/synch/mutex/sql/LOCK_des_key_file | NO | NO |
| wait/synch/mutex/sql/MYSQL_BIN_LOG::LOCK_commit | NO | NO |
| wait/synch/mutex/sql/MYSQL_BIN_LOG::LOCK_commit_queue | NO | NO |
| wait/synch/mutex/sql/MYSQL_BIN_LOG::LOCK_done | NO | NO |
......
| wait/lock/metadata/sql/mdl | NO | NO |
+--------------------------------------------------------------------+---------+-------+
321 rows in set (0.00 sec)
--可以在my.cnf配置,永久的开启配置信息来记录等待事件执行语句到表中
[mysqld]
performance-schema-instrument='stage/%=ON'
performance-schema-consumer-events-stages-current=ON
performance-schema-consumer-events-stages-history=ON
performance-schema-consumer-events-stages-history-long=ON
--查看history历史表记录的数据量,history记录每个线程的最近10条,history_long记录所有线程的最近10000条
mysql> show variables like 'performance_schema_events_waits_history%';
+---------------------------------------------------+-------+
| Variable_name | Value |
+---------------------------------------------------+-------+
| performance_schema_events_waits_history_long_size | 10000 |
| performance_schema_events_waits_history_size | 10 |
+---------------------------------------------------+-------+
2 rows in set (0.07 sec)
--查看历史执行时间最长的前5条每步具体时间,如下:
mysql> SELECT thread_id
,event_id
,event_name
,(timer_end-timer_start)/1000000000000 exec_time_sec
,timer_wait/1000000000000 timer_wait_sce
FROM performance_schema.events_waits_history
ORDER BY 4 DESC
LIMIT 5;
+-----------+----------+--------------------------------------------+---------------+----------------+
| thread_id | event_id | event_name | exec_time_sec | timer_wait_sce |
+-----------+----------+--------------------------------------------+---------------+----------------+
| 12 | 3 | wait/io/file/innodb/innodb_log_file | 0.0169 | 0.0169 |
| 6 | 113 | wait/io/file/innodb/innodb_data_file | 0.0169 | 0.0169 |
| 12 | 8 | wait/io/file/innodb/innodb_log_file | 0.0034 | 0.0034 |
| 30 | 1242 | wait/io/socket/sql/client_connection | 0.0001 | 0.0001 |
| 1 | 17170 | wait/synch/mutex/sql/LOCK_connection_count | 0.0000 | 0.0000 |
+-----------+----------+--------------------------------------------+---------------+----------------+
5 rows in set (0.00 sec)
--查看sql相应的具体等待事件
--执行一条测试的sql,分析此条sql的等待事件
mysql> SELECT /*+test_waits*/COUNT(1) FROM information_schema.columns;
+----------+
| COUNT(1) |
+----------+
| 3193 |
+----------+
1 row in set, 1 warning (0.08 sec)
--查看上述sql的每个阶段的时间和事件
--说明:events_statements*表的event_id字段和events_stages*表的nesting_event_id字段关联
mysql> SELECT statement.sql_text,
stages.event_id stage_event_id,
stages.event_name stage_event_name ,
stages.timer_wait/1000000000000 stage_timer_wait_sec --秒
FROM performance_schema.events_statements_history_long statement
JOIN performance_schema.events_stages_history_long stages
ON statement.event_id=stages.nesting_event_id
WHERE statement.sql_text LIKE 'SELECT /*+test_waits*/COUNT(1) FROM information_schema.columns'
ORDER BY stages.timer_wait DESC
LIMIT 10;
+----------------------------------------------------------------+----------------+--------------------------------+----------------------+
| sql_text | stage_event_id | stage_event_name | stage_timer_wait_sec |
+----------------------------------------------------------------+----------------+--------------------------------+----------------------+
| SELECT /*+test_waits*/COUNT(1) FROM information_schema.columns | 59168 | stage/sql/checking permissions | 0.0064 |
| SELECT /*+test_waits*/COUNT(1) FROM information_schema.columns | 69109 | stage/sql/Sending data | 0.0052 |
| SELECT /*+test_waits*/COUNT(1) FROM information_schema.columns | 56443 | stage/sql/checking permissions | 0.0032 |
| SELECT /*+test_waits*/COUNT(1) FROM information_schema.columns | 57885 | stage/sql/Opening tables | 0.0015 |
| SELECT /*+test_waits*/COUNT(1) FROM information_schema.columns | 56371 | stage/sql/starting | 0.0012 |
| SELECT /*+test_waits*/COUNT(1) FROM information_schema.columns | 65769 | stage/sql/Opening tables | 0.0012 |
| SELECT /*+test_waits*/COUNT(1) FROM information_schema.columns | 65969 | stage/sql/Opening tables | 0.0006 |
| SELECT /*+test_waits*/COUNT(1) FROM information_schema.columns | 58929 | stage/sql/checking permissions | 0.0006 |
| SELECT /*+test_waits*/COUNT(1) FROM information_schema.columns | 68261 | stage/sql/Opening tables | 0.0005 |
| SELECT /*+test_waits*/COUNT(1) FROM information_schema.columns | 66097 | stage/sql/Opening tables | 0.0004 |
+----------------------------------------------------------------+----------------+--------------------------------+----------------------+
10 rows in set (0.08 sec)
--查看上述第二行的阶段(红色)的具体等待事件,如下:
--说明:events_stages*表的event_id字段和events_waits*表的nesting_event_id字段关联
mysql> SELECT thread_id
,event_id
,event_name
,(timer_end-timer_start) exec_time --皮秒
,timer_wait timer_wait --皮秒
FROM performance_schema.events_waits_history_long t
WHERE t.nesting_event_id = 69109
ORDER BY timer_wait DESC;
+-----------+----------+-------------------------------------------+-----------+------------+
| thread_id | event_id | event_name | exec_time | timer_wait |
+-----------+----------+-------------------------------------------+-----------+------------+
| 29 | 69112 | wait/synch/sxlock/innodb/hash_table_locks | 626410 | 626410 |
| 29 | 69115 | wait/synch/sxlock/innodb/hash_table_locks | 194250 | 194250 |
| 29 | 69114 | wait/synch/sxlock/innodb/hash_table_locks | 156880 | 156880 |
| 29 | 69121 | wait/synch/sxlock/innodb/hash_table_locks | 150960 | 150960 |
| 29 | 69119 | wait/synch/sxlock/innodb/hash_table_locks | 145040 | 145040 |
| 29 | 69116 | wait/synch/sxlock/innodb/hash_table_locks | 125430 | 125430 |
| 29 | 69123 | wait/synch/sxlock/innodb/hash_table_locks | 122100 | 122100 |
| 29 | 69117 | wait/synch/sxlock/innodb/hash_table_locks | 121360 | 121360 |
| 29 | 69113 | wait/synch/sxlock/innodb/hash_table_locks | 119880 | 119880 |
| 29 | 69122 | wait/synch/sxlock/innodb/hash_table_locks | 119510 | 119510 |
| 29 | 69120 | wait/synch/sxlock/innodb/hash_table_locks | 115810 | 115810 |
| 29 | 69118 | wait/synch/sxlock/innodb/hash_table_locks | 110630 | 110630 |
| 29 | 69133 | wait/synch/sxlock/innodb/hash_table_locks | 106930 | 106930 |
| 29 | 69140 | wait/synch/sxlock/innodb/hash_table_locks | 106190 | 106190 |
| 29 | 69126 | wait/synch/sxlock/innodb/hash_table_locks | 105450 | 105450 |
| 29 | 69147 | wait/synch/sxlock/innodb/hash_table_locks | 96570 | 96570 |
| 29 | 69110 | wait/synch/mutex/innodb/trx_mutex | 90280 | 90280 |
| 29 | 69124 | wait/synch/sxlock/innodb/hash_table_locks | 68080 | 68080 |
| 29 | 69127 | wait/synch/sxlock/innodb/hash_table_locks | 66600 | 66600 |
| 29 | 69144 | wait/synch/sxlock/innodb/hash_table_locks | 65860 | 65860 |
| 29 | 69143 | wait/synch/sxlock/innodb/hash_table_locks | 64750 | 64750 |
| 29 | 69141 | wait/synch/sxlock/innodb/hash_table_locks | 64010 | 64010 |
| 29 | 69132 | wait/synch/sxlock/innodb/hash_table_locks | 63270 | 63270 |
| 29 | 69142 | wait/synch/sxlock/innodb/hash_table_locks | 61050 | 61050 |
| 29 | 69137 | wait/synch/sxlock/innodb/hash_table_locks | 60680 | 60680 |
| 29 | 69145 | wait/synch/sxlock/innodb/hash_table_locks | 60310 | 60310 |
| 29 | 69146 | wait/synch/sxlock/innodb/hash_table_locks | 59940 | 59940 |
| 29 | 69128 | wait/synch/sxlock/innodb/hash_table_locks | 59570 | 59570 |
| 29 | 69134 | wait/synch/sxlock/innodb/hash_table_locks | 58830 | 58830 |
| 29 | 69131 | wait/synch/sxlock/innodb/hash_table_locks | 58830 | 58830 |
| 29 | 69125 | wait/synch/sxlock/innodb/hash_table_locks | 58460 | 58460 |
| 29 | 69129 | wait/synch/sxlock/innodb/hash_table_locks | 58090 | 58090 |
| 29 | 69136 | wait/synch/sxlock/innodb/hash_table_locks | 57350 | 57350 |
| 29 | 69135 | wait/synch/sxlock/innodb/hash_table_locks | 56240 | 56240 |
| 29 | 69139 | wait/synch/sxlock/innodb/hash_table_locks | 55500 | 55500 |
| 29 | 69138 | wait/synch/sxlock/innodb/hash_table_locks | 55130 | 55130 |
| 29 | 69130 | wait/synch/sxlock/innodb/hash_table_locks | 52910 | 52910 |
| 29 | 69111 | wait/synch/mutex/innodb/trx_mutex | 36630 | 36630 |
+-----------+----------+-------------------------------------------+-----------+------------+
38 rows in set (0.01 sec)