覆盖索引---MyISAM 与 InnoDB

覆盖索引—MyISAM 与 InnoDB

如果索引包含满足查询的所有数据,就称为覆盖索引。
覆盖索引是一种非常强大的工具,能大大提高查询性能。只需要读取索引而不用读取数据有以下一些优点:
(1)索引项通常比记录要小,所以MySQL访问更少的数据;
(2)索引都按值的大小顺序存储,相对于随机访问记录,需要更少的I/O;
(3)大多数据引擎能更好的缓存索引。比如MyISAM只缓存索引。
(4)覆盖索引对于InnoDB表尤其有用,因为InnoDB使用聚集索引组织数据,如果二级索引中包含查询所需的数据,就不再需要在聚集索引中查找了。
覆盖索引不能是任何索引,只有B-TREE索引存储相应的值。而且不同的存储引擎实现覆盖索引的方式都不同,并不是所有存储引擎都支持覆盖索引(Memory和Falcon就不支持)。
对于索引覆盖查询(index-covered query),使用EXPLAIN时,可以在Extra一列中看到“Using index”。


MyISAM

create table people(
    pid int not null auto_increment,
    pname varchar(50) not null,
    age int not null,
    primary key (pid),
    key (pname)
) engine MyISAM;

insert into people (pname,age) values ('a',11);
insert into people (pname,age) values ('b',12);
insert into people (pname,age) values ('c',13);
explain select pid, pname from people where pname='a';

MYISAM查询结果

InnoDB

create table man(
    mmid int not null auto_increment,
    mname varchar(50) not null,
    mage int not null,
    primary key (mmid),
    key (mname)
) engine InnoDB;

insert into man (mname,mage) values ('a',11);
insert into man (mname,mage) values ('b',12);
insert into man (mname,mage) values ('c',13);
explain select mmid, mname from man where mname='a';

INNODB

通过上面的结果对比可以看出InnoDB用到了覆盖索引而MyISAM没有!!!

你可能感兴趣的:(MySQL)