1.1、本次测试的环境
mysql> show variables like 'profiling';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| profiling | OFF |
+---------------+-------+
1 row in set (0.01 sec)
mysql> show variables like 'profiling';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| profiling | ON |
+---------------+-------+
1 row in set (0.00 sec)
执行要分析的SQL查询。
mysql> show tables
-> ;
+----------------+
| Tables_in_test |
+----------------+
| my_table |
| tmp |
| tmp_table |
+----------------+
3 rows in set (0.00 sec)
mysql> select * from my_table;
+----+-------+-----+
| id | name | age |
+----+-------+-----+
| 1 | Tom | 25 |
| 2 | Tom | 25 |
| 3 | Jerry | 30 |
| 4 | Alice | 28 |
+----+-------+-----+
4 rows in set (0.00 sec)
执行查询后,你可以通过以下命令查看所有查询的Profile
mysql> show profiles;
+----------+------------+---------------------------------+
| Query_ID | Duration | Query |
+----------+------------+---------------------------------+
| 1 | 0.00116575 | show variables like 'profiling' |
| 2 | 0.00084675 | show tables |
| 3 | 0.00038900 | select * from my_table |
+----------+------------+---------------------------------+
3 rows in set, 1 warning (0.00 sec)
这将列出所有已执行的查询及其查询编号(Query_ID)。
mysql>
选择你想要查看详细Profile的查询编号,然后使用以下命令:
mysql> select * from tmp_table;
Empty set (0.00 sec)
mysql> show profiles;
+----------+------------+---------------------------------+
| Query_ID | Duration | Query |
+----------+------------+---------------------------------+
| 1 | 0.00116575 | show variables like 'profiling' |
| 2 | 0.00084675 | show tables |
| 3 | 0.00038900 | select * from my_table |
| 4 | 0.00034225 | select * from tmp_table |
+----------+------------+---------------------------------+
4 rows in set, 1 warning (0.00 sec)
The SHOW PROFILE and SHOW PROFILES statements display profiling
information that indicates resource usage for statements executed
during the course of the current session.