在工作中,经常会遇到要优化语句的场景,MySQL的Query Profiler就是一个很好的工具,使用方法也很简单,介绍如下:
mysql> set profiling=1; /*开启profiling*/
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like '%profiling%';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| have_profiling | YES |
| profiling | ON |
| profiling_history_size | 15 |
+------------------------+-------+
3 rows in set (0.01 sec)
mysql> select * from user;
mysql> show profiles;
+----------+------------+--------------------+
| Query_ID | Duration | Query |
+----------+------------+--------------------+
| 1 | 0.00834675 | select * from user |
+----------+------------+--------------------+
1 row in set (0.00 sec)
mysql> set profiling=0; /*关闭profiling*/
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like '%profiling%';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| have_profiling | YES |
| profiling | OFF |
| profiling_history_size | 15 |
+------------------------+-------+
3 rows in set (0.00 sec)
这就是MySQL profiling的一个简单的使用方法。