explain和profiling分析查询SQL时间

mysql可以通过profiling命令查看到执行查询SQL消耗的时间。

默认情况下,mysql是关闭profiling的,命令:

  1. select @@profiling;  
+-------------------+

|    @@profiling    |

+-------------------+

|                     0   |

+-------------------+

说明:

0:表示profiling功能是关闭;

1:表示打开的。

可以通过命令打开/关闭profiling功能。

打开命令:

  1. set profiling=1;  
关闭命令:
  1. set profiling=0;  
如查询命令:

select * from employee limit 1,10;

可以使用profiling命令查看执行这条SQL消耗的时间:

  1. show profiles;  
查询结果:

+----------------+-----------------+-------------------------------------------------------------+

| Query_ID        | Duration         | Query                                                                     |

+----------------+-----------------+--------------------------------------------------------------+

|             1       | 0.00083225      | select * from employee limit 1,10                              |

+----------------+-----------------+--------------------------------------------------------------+

1 row in set ( 0.00 sec)

使用explain来分析是否命中索引

  1. mysql> explain select * from user where username = 'a';  
  2. +----+-------------+-------+------+---------------+------------+---------+-------+------+-------------+  
  3. | id | select_type | table | type | possible_keys | key        | key_len | ref   | rows | Extra       |  
  4. +----+-------------+-------+------+---------------+------------+---------+-------+------+-------------+  
  5. |  1 | SIMPLE      | user  | ref  | user_index    | user_index | 62      | const |    1 | Using where |  
  6. +----+-------------+-------+------+---------------+------------+---------+-------+------+-------------+  
  7. 1 row in set (0.00 sec)  

可以看出已经命中索引user_index
 
 
 
 
 
 
原文地址:https://www.cnblogs.com/musings/p/8902439.html