1.profile
mysql> SELECT @@have_profiling;
+------------------+
| @@have_profiling |
+------------------+
| YES |
+------------------+
1 row in set, 1 warning (0.00 sec)
mysql> SET profiling = 1;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> SHOW profiles;
+----------+------------+-------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+-------------------------------------+
| 1 | 0.00010125 | select count(*) from employee |
| 2 | 0.00015975 | SELECT DATABASE() |
| 3 | 0.00040175 | show databases |
| 4 | 0.00041950 | show tables |
| 5 | 0.07274575 | select count(*) from employee |
| 6 | 0.00017050 | show profile cpu for query query_id |
| 7 | 0.10228900 | select count(id) from employee |
+----------+------------+-------------------------------------+
7 rows in set, 1 warning (0.00 sec)
mysql> SHOW profile for query 2;
+----------------------+----------+
| Status | Duration |
+----------------------+----------+
| starting | 0.000063 |
| checking permissions | 0.000009 |
| Opening tables | 0.000007 |
| init | 0.000016 |
| optimizing | 0.000008 |
| executing | 0.000011 |
| end | 0.000006 |
| query end | 0.000007 |
| closing tables | 0.000006 |
| freeing items | 0.000014 |
| cleaning up | 0.000016 |
+----------------------+----------+
11 rows in set, 1 warning (0.00 sec)
2. Performance Schema
The MySQL Performance Schema is a feature for monitoring MySQL Server
execution at a low level. The Performance Schema has these
characteristics.
这是MySQL官网上对Performance Schema解释的一句话,大意是Performance Schema是一个运行在较低级别里的具有监控MySQL Server执行的功能。
具体可看:https://dev.mysql.com/doc/refman/8.0/en/performance-schema.html
用法:
通过select * from events_waits_current\G; 从表中信息可得到该表中有多少当前线程在运行以及线程运行的其它信息,用来显示每个线程的最新监视事件。
好处是Performance Schema比show profile功能更全,当从SQL语句执行时间等的执行过程分析中无法找到原因进行优化时,用Performance Schema可以直接查看表中有哪些线程在运行,从每个线程的包含信息记录中找到耗时原因。