利用performance_schema数据库查看SQL资源统计

纵然是瞬间 提交于 2020-02-15 22:03:48


1.修改数据字典以启用相关事件

mysql> use performance_schema
mysql> show tables like 'setup%';
+---------------------------------------+
| Tables_in_performance_schema (setup%) |
+---------------------------------------+
| setup_actors                          |
| setup_consumers                       |
| setup_instruments                     |
| setup_objects                         |
| setup_timers                          |
+---------------------------------------+
5 rows in set (0.00 sec)

mysql> 
mysql> update performance_schema.setup_consumers set enabled='YES' where name like 'events_stages_%';
Query OK, 0 rows affected (0.01 sec)
Rows matched: 3  Changed: 0  Warnings: 0

mysql> 
mysql> select * from performance_schema.setup_consumers where name like 'events_stages_%';
+----------------------------+---------+
| NAME                       | ENABLED |
+----------------------------+---------+
| events_stages_current      | YES     |
| events_stages_history      | YES     |
| events_stages_history_long | YES     |
+----------------------------+---------+
3 rows in set (0.01 sec)

mysql>  update performance_schema.setup_instruments set enabled='YES',timed='YES' where name like 'stage/sql/%';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 120  Changed: 0  Warnings: 0

mysql> select * from performance_schema.setup_instruments where name like 'stage/sql/%';

2.执行SQL语句

mysql> select * from user where user_id=2222;
+---------+--------------+---------------+---------------------+
| user_id | user_name    | user_email    | created             |
+---------+--------------+---------------+---------------------+
|    2222 | bill2222 | 123456@qq.com | 2020-02-09 12:24:32 |
+---------+--------------+---------------+---------------------+
1 row in set (0.01 sec)

mysql> 

3.查看SQL资源统计

SELECT 
  thread_id,
  event_id,
  event_name,
  timer_wait 
FROM
  performance_schema.events_stages_history 
WHERE thread_id IN 
  (SELECT 
    thread_id 
  FROM
    performance_schema.threads 
  WHERE processlist_id = 2452) 
ORDER BY event_id;
+-----------+----------+--------------------------------+------------+
| thread_id | event_id | event_name                     | timer_wait |
+-----------+----------+--------------------------------+------------+
|      2477 |     1076 | stage/sql/checking permissions |   25424000 |
|      2477 |     1077 | stage/sql/Opening tables       |  216525000 |
|      2477 |     1078 | stage/sql/init                 |  136345000 |
|      2477 |     1079 | stage/sql/System lock          |   79226000 |
|      2477 |     1080 | stage/sql/optimizing           |   43497000 |
|      2477 |     1081 | stage/sql/statistics           |  256410000 |
|      2477 |     1082 | stage/sql/preparing            |  108996000 |
|      2477 |     1083 | stage/sql/Creating tmp table   |   36546000 |
|      2477 |     1084 | stage/sql/Sorting result       |  950685000 |
|      2477 |     1085 | stage/sql/executing            |    5021000 |
+-----------+----------+--------------------------------+------------+
10 rows in set (0.05 sec)

mysql> 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!