Mysql explain-type使用详解

下面是测试用例,楼主的测试使用的MySQL版本是5.6.27-log。

show  create table user_man;

CREATE TABLE `user_man` (
  `manid` bigint(20) NOT NULL AUTO_INCREMENT,
  `manname` varchar(32) DEFAULT NULL,
  `manage` int(6) DEFAULT NULL,
  `upd_time` timestamp NOT NULL DEFAULT '2000-01-01 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
  `blacklist` bigint(20) DEFAULT '-1' COMMENT '-1,1',
  PRIMARY KEY (`manid`),
  KEY `IDX_MN` (`manname`) USING BTREE,
  KEY `IDX_MID` (`manid`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8;


show  create table user_woman;
CREATE TABLE `user_woman` (
  `womanid` int(11) NOT NULL AUTO_INCREMENT,
  `womanname` varchar(20) DEFAULT NULL,
  `womanage` int(6) DEFAULT NULL,
  PRIMARY KEY (`womanid`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;




#执行计划中type定义说明:




#(1)执行计划type为ALL:遍历全表来查找
explain  SELECT * from user_man ;
explain select * from user_man where manage in (9,10);#非索引范围查找也是走全表


#(2)执行计划type为index:只遍历索引树
explain SELECT manid from user_man;


#(3)执行计划type为range:索引扫描范围
explain select *from user_man where manid  < 10;
explain select * from user_man where manid in (8,10,9) order by manage;


#(4)执行计划type为ref:非唯一性扫描索引
explain select *from user_man where manname = 'zzk';


#(5) 执行计划type为eq_ref:唯一性索引扫描
explain select * from user_man ,user_woman where user_man.manid = user_woman.womanid;
explain select * from user_man where manid in (select womanid from user_woman);


#(6)执行计划type为const、system:当MySQL对查询某部分进行优化,并转换为一个常量时,使用这些类型访问。如将主键置于where列表中,MySQL就能将该查询转换为一个常量
explain select *from user_man where manid = 8;
explain select * from (select * from user_man where manid=8) dd;


#(7)NULL:MySQL在优化过程中分解语句,执行时甚至不用访问表或索引
explain select * from user_man where manid = (select min(womanid) from user_woman)




#查询中若使用了覆盖索引,则该索引仅出现在key列表中(为manid、manname建立了索引,单独查这两个字段);
explain select manid ,manname from user_man where manname = 'zzzz' and manage = 43;

你可能感兴趣的:(mysql)