explain和profiling分析查詢SQL時間
mysql可以通過profiling命令查看到執行查詢SQL消耗的時間。
默認情況下,mysql是關閉profiling的,命令:
[sql] view plain copy- select @@profiling;
| @@profiling |
+-------------------+
| 0 |
+-------------------+
說明:
0:表示profiling功能是關閉;
1:表示打開的。
可以通過命令打開/關閉profiling功能。
打開命令:
[sql] view plain copy- set profiling=1;
- set profiling=0;
select * from employee limit 1,10;
可以使用profiling命令查看執行這條SQL消耗的時間:
[sql] view plain copy- show profiles;
+----------------+-----------------+-------------------------------------------------------------+
| Query_ID | Duration | Query |
+----------------+-----------------+--------------------------------------------------------------+
| 1 | 0.00083225 | select * from employee limit 1,10 |
+----------------+-----------------+--------------------------------------------------------------+
1 row in set ( 0.00 sec)
使用explain來分析是否命中索引
[sql] view plain copy- 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
explain和profiling分析查詢SQL時間