1 检查MySQL中激活的actor(即监控的主机),
mysql> SELECT *
-> FROM performance_schema.setup_actors;
+------+------+------+---------+---------+
| HOST | USER | ROLE | ENABLED | HISTORY |
+------+------+------+---------+---------+
| % | % | % | YES | YES |
+------+------+------+---------+---------+
1 row in set (0.00 sec)
2 打开对sql语句的监视,可以针对某一类sql语句单数打开监控
mysql> UPDATE performance_schema.setup_instruments
-> SET ENABLED = 'YES', TIMED = 'YES'
-> WHERE NAME LIKE '%statement/%';
Query OK, 0 rows affected (0.12 sec)
Rows matched: 213 Changed: 0 Warnings: 0
UPDATE performance_schema.setup_instruments
SET ENABLED = 'YES', TIMED = 'YES'
WHERE NAME LIKE '%stage/%';
3 查看是否打开
select name, enabled from performance_schema.setup_instruments where name like 'statement%' or name like 'stage%';
4 打开statement和stage consumer
update setup_consumers set ENABLED='YES' where name='events_stages_history_long';
update setup_consumers set ENABLED='YES' where name='events_stages_history';
update setup_consumers set ENABLED='YES' where name='events_stages_current';
5 检查所需consumer是否被打开
select * from performance_schema.setup_consumers where name like '%stage%';
+----------------------------+---------+
| NAME | ENABLED |
+----------------------------+---------+
| events_stages_current | YES |
| events_stages_history | YES |
| events_stages_history_long | YES |
+----------------------------+---------+
3 rows in set (0.00 sec)
6 运行sql查询
select * from a limit 100 ;
7 从performance_schema.events_statements_history_long查询sql语句的event id
SELECT EVENT_ID, TRUNCATE(TIMER_WAIT/1000000000000,6) as Duration, SQL_TEXT
-> FROM performance_schema.events_statements_history_long;
EVENT_ID | Duration | SQL_TEXT |
---|---|---|
1156 | 0.0020 | truncate table performance_schema.events_statements_history_long |
1 | 0.0001 | select @@version_comment limit 1 |
13 | 0.0002 | SELECT DATABASE() |
25 | 0.0000 | NULL |
28 | 0.0010 | show databases |
48 | 0.0010 | show tables |
74 | 0.0001 | NULL |
79 | 0.0000 | NULL |
84 | 0.0000 | NULL |
89 | 0.0000 | NULL |
94 | 0.0000 | NULL |
99 | 0.0025 | select * from a limit 10000 |
12 rows in set (0.00 sec)
8 查询这条语句每个阶段的执行时间
mysql> SELECT event_name AS Stage, TRUNCATE(TIMER_WAIT / 1000000000000, 6) AS Duration
-> FROM performance_schema.events_stages_history_long
-> WHERE NESTING_EVENT_ID = 99;
Stage | Duration |
---|---|
stage/sql/starting | 0.0000 |
stage/sql/Executing hook on transaction begin. | 0.0000 |
stage/sql/starting | 0.0000 |
stage/sql/checking permissions | 0.0000 |
stage/sql/Opening tables | 0.0000 |
stage/sql/init | 0.0000 |
stage/sql/System lock | 0.0000 |
stage/sql/optimizing | 0.0000 |
stage/sql/statistics | 0.0000 |
stage/sql/preparing | 0.0000 |
stage/sql/executing | 0.0022 |
stage/sql/end | 0.0000 |
stage/sql/query end | 0.0000 |
stage/sql/waiting for handler commit | 0.0000 |
stage/sql/closing tables | 0.0000 |
stage/sql/freeing items | 0.0000 |
stage/sql/cleaning up | 0.0000 |
17 rows in set (0.00 sec)
可以看到每个sql语句每个阶段的执行时间