天天看點

mysql索引執行個體-explain-type類型分析

知識概括

       a.    all  全表掃描

       b.     index  按照索引順序的全表掃描

       c.     range 有範圍的索引掃描 ,between,and以及'>','<'外,in和or均是索引範圍掃描

       d.     ref  使用了非主鍵或者非唯一索引的普通索引(即索引可以重複情況下的)

       e.     ref_eq 使用了唯一索引的查詢

       f.     const 使用了主鍵索引的查詢

1.建表

create table indexTest(

 id int(3) not null auto_increment,

 a int(3) not null default 0,

 b int(3) not null default 0,

 c int(3) not null default 0,

 primary key(id),

 key in_ab (a,b)

)engine=INNODB default charset=utf8;

2.插入資料

insert into indexTest(id,a,b,c) values (3,4,2,4);

insert into indexTest(id,a,b,c) values (2,7,6,5);

insert into indexTest(id,a,b,c) values (4,4,1,6);

insert into indexTest(id,a,b,c) values (1,4,4,9);

insert into indexTest(id,a,b,c) values (5,2,7,2);

insert into indexTest(id,a,b,c) values (7,4,0,0);

3.SELECT * from indexTest;

     查詢到的結果集,自動按照id升序排序;實質為資料在插入的時候,會自動按照主鍵索引進行排序。

mysql索引執行個體-explain-type類型分析

4.all的全表查詢

    在第二條語句中,如果使用索引a的結構,擷取到資料,則由于部分需要的資料在索引a的結構不存在,此時需要逐個從主鍵索引結構中擷取,此時性能更加低下。

mysql索引執行個體-explain-type類型分析

5. index  按照索引順序的全表掃描

    按照索引順序的全表掃描

mysql索引執行個體-explain-type類型分析
mysql索引執行個體-explain-type類型分析

6. range 有範圍的索引掃描

,between,and以及'>','<'外,in和or均是索引範圍掃描

mysql索引執行個體-explain-type類型分析

7.ref 普通索引查找

使用了非主鍵或者非唯一索引的普通索引(即索引可以重複情況下的)

mysql索引執行個體-explain-type類型分析

8.const 使用了主鍵索引的查詢

mysql索引執行個體-explain-type類型分析