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


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 | [email protected] | 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> 

你可能感兴趣的:(MYSQL,SQL优化)