explain和profiling分析查询SQL时间
mysql可以通过profiling命令查看到执行查询SQL消耗的时间。
默认情况下,mysql是关闭profiling的,命令:
- select @@profiling;
| @@profiling |
+-------------------+
| 0 |
+-------------------+
说明:
0:表示profiling功能是关闭;
1:表示打开的。
可以通过命令打开/关闭profiling功能。
打开命令:
- set profiling=1;
- set profiling=0;
select * from employee limit 1,10;
可以使用profiling命令查看执行这条SQL消耗的时间:
- show profiles;
+----------------+-----------------+-------------------------------------------------------------+
| Query_ID | Duration | Query |
+----------------+-----------------+--------------------------------------------------------------+
| 1 | 0.00083225 | select * from employee limit 1,10 |
+----------------+-----------------+--------------------------------------------------------------+
1 row in set ( 0.00 sec)
使用explain来分析是否命中索引
- mysql> explain select * from user where username = 'a';
- +----+-------------+-------+------+---------------+------------+---------+-------+------+-------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+------+---------------+------------+---------+-------+------+-------------+
- | 1 | SIMPLE | user | ref | user_index | user_index | 62 | const | 1 | Using where |
- +----+-------------+-------+------+---------------+------------+---------+-------+------+-------------+
- 1 row in set (0.00 sec)
可以看出已经命中索引user_index