加入收藏 | 设为首页 | 会员中心 | 我要投稿 核心网 (https://www.hxwgxz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

MySQL EXPLAIN结果集分析 - 附带大量案例

发布时间:2019-09-18 00:07:43 所属栏目:编程 来源:佚名
导读:EXPLAIN:查看SQL语句的执行计划 EXPLAIN命令可以帮助我们深入了解MySQL基于开销的优化器,还可以获得很多可能被优化器考虑到的访问策略的细节,以及当运行SQL语句时哪种策略预计会被优化器采用,在优化慢查询时非常有用。 执行explain之后结果集包含如下

ref可以用于使用=或<=>操作符的带索引的列

  1. mysql> explain select * from test ,test2 where test.bnet_id=test2.aid; 
  2. +----+-------------+-------+------+---------------+---------+---------+-------------------+-------+-----------------------+ 
  3. | id | select_type | table | type | possible_keys | key     | key_len | ref               | rows  | Extra                 | 
  4. +----+-------------+-------+------+---------------+---------+---------+-------------------+-------+-----------------------+ 
  5. |  1 | SIMPLE      | test  | ALL  | NULL          | NULL    | NULL    | NULL              | 68505 | Using where           | 
  6. |  1 | SIMPLE      | test2 | ref  | idx_aid       | idx_aid | 5       | test.test.bnet_id | 34266 | Using index condition | 
  7. +----+-------------+-------+------+---------------+---------+---------+-------------------+-------+-----------------------+ 

test表bnet_id不是索引,test2表aid为索引列

ref_or_null: 类似ref,但是添加了可以专门搜索null值的行

  1. mysql> explain select * from test where bnet_id=1 or bnet_id is null; 
  2. +----+-------------+-------+-------------+---------------+----------+---------+-------+------+-----------------------+ 
  3. | id | select_type | table | type        | possible_keys | key      | key_len | ref   | rows | Extra                 | 
  4. +----+-------------+-------+-------------+---------------+----------+---------+-------+------+-----------------------+ 
  5. |  1 | SIMPLE      | test  | ref_or_null | idx_bnet      | idx_bnet | 9       | const |    2 | Using index condition | 
  6. +----+-------------+-------+-------------+---------------+----------+---------+-------+------+-----------------------+ 

前提为bnet_id列为索引,且bnet_id列有null值

index_merge: 该访问类型使用了索引合并优化方法,key列包含了使用的索引的清单,key_len包含了使用的索引的最长的关键元素

  1. mysql> explain select * from test where id  = 1 or bnet_id = 1; 
  2. +----+-------------+-------+-------------+------------------+------------------+---------+------+------+--------------------------------------------+ 
  3. | id | select_type | table | type        | possible_keys    | key              | key_len | ref  | rows | Extra                                      | 
  4. +----+-------------+-------+-------------+------------------+------------------+---------+------+------+--------------------------------------------+ 
  5. |  1 | SIMPLE      | test  | index_merge | PRIMARY,idx_bnet | PRIMARY,idx_bnet | 8,9     | NULL |    2 | Using union(PRIMARY,idx_bnet); Using where | 
  6. +----+-------------+-------+-------------+------------------+------------------+---------+------+------+--------------------------------------------+ 

前提条件为id列和bnet_id列都有单列索引。如果出现index_merge,并且这类SQL后期使用较频繁,可以考虑把单列索引换为组合索引,这样效率更高

range: 只检索给定范围的行,使用一个索引来选择行。key列显示使用了哪个索引。key_len包含所使用索引的最长关键元素。在该类型中ref列为NULL

(编辑:核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读