知識概括
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升序排序;實質爲數據在插入的時候,會自動按照主鍵索引進行排序。
4.all的全表查詢
在第二條語句中,如果使用索引a的結構,獲取到數據,則由於部分需要的數據在索引a的結構不存在,此時需要逐個從主鍵索引結構中獲取,此時性能更加低下。
5. index 按照索引順序的全表掃描
按照索引順序的全表掃描
6. range 有範圍的索引掃描
,between,and以及'>','<'外,in和or均是索引範圍掃描
7.ref 普通索引查找
使用了非主鍵或者非唯一索引的普通索引(即索引可以重複情況下的)
8.const 使用了主鍵索引的查詢