些SQL语句执行效率低下,通过explain我们可以得知SQL语句的具体执行情况,索引使用等,还可以结合show命令查看执行状态。
如果觉得explain的信息不够详细,可以同通过profiling命令得到更准确的SQL执行消耗系统资源的信息。
profiling默认是关闭的。可以通过以下语句查看 select @@profiling;
打开功能: mysql>set profiling=1; 执行需要测试的sql 语句:
开启后执行了sql语句 就可以通过show profiles;命令查看执行时间
SQL
mysql> show profiles;
+----------+------------+---------------------------+
| Query_ID | Duration | Query |
+----------+------------+---------------------------+
| 1 | 0.00417000 | select * from user |
| 2 | 0.00214700 | select count(*) from user |
+----------+------------+---------------------------+
2 rows in set, 1 warning (0.00 sec)
mysql> show profiles\G; 可以得到被执行的SQL语句的时间和ID
mysql>show profile for query 1; 得到对应SQL语句执行的详细信息
Bash
mysql> show profile for query 1;
+----------------------+----------+
| Status | Duration |
+----------------------+----------+
| starting | 0.001587 |
| checking permissions | 0.000351 |
| Opening tables | 0.000015 |
| init | 0.000021 |
| System lock | 0.000264 |
| optimizing | 0.000014 |
| statistics | 0.000014 |
| preparing | 0.000012 |
| executing | 0.000003 |
| Sending data | 0.001485 |
| end | 0.000018 |
| query end | 0.000120 |
| closing tables | 0.000026 |
| freeing items | 0.000157 |
| cleaning up | 0.000083 |
+----------------------+----------+
15 rows in set, 1 warning (0.00 sec)