首页 > 数据库技术 > 详细

explain和profiling分析查询SQL时间

时间:2018-04-21 18:28:03      阅读:152      评论:0      收藏:0      [点我收藏+]

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
 
 
 
 
 
 

explain和profiling分析查询SQL时间

原文:https://www.cnblogs.com/musings/p/8902439.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!