- 本博客是《MySQL基础》系列博客的第九部分,主要介绍MySQL中的索引
- 本博客既为方便自己查看复习而作,亦为你而作,望能有所裨益
- 学习交流请联系 April_0911@163.com
基本概念
定义
- 索引(Index),在MySQL中也叫键(Key),是存储引擎快速找到记录的一种数据结构。
- 你可以将它形象地比喻为目录之类的东西,能够快速检索出你所需要的东西。
类型
类型 | 关键字 | 简介 |
---|---|---|
普通索引 | INDEX | 最基本常用的索引,仅加速查询,没有任何限制 |
唯一索引 | UNIQUE | 唯一索引列的值必须唯一,但允许有空值,如果是组合索引,则列值的组合必须唯一 |
全文索引 | FULLTEXT | 仅可以适用于MyISAM引擎的数据表:作用于CHAR、VARCHAR、TEXT数据类型的列 |
主键索引 | UNIQUE INDEX | 一种特殊的唯一索引,不允许有空值,一般是在创建表的时候同时创建主键索引 |
组合索引 | INDEX() | 将几个列作为一条索引进行检索,使用最左匹配原则 |
索引应用
示例数据表
由于索引是用来加快查询速度的,因此越大型的数据集其查询效率的优势越明显,因此我们需要在原有的数据表的基础上再创建一个新的大型数据表用来学习:
create table payment1(
payment_id smallint(5) unsigned not null,
country varchar(50) not null,
country_id smallint(5) unsigned not null
)
select payment_id,country,country_id from payment,country;
创建完成后,生成一个大型数据集:
预备知识
为了能让其效果简洁易懂,也为了方便日后章节的学习,我们在这里就要开始学习如何在命令行使用MySQL了。
- 如何在命令行连接数据库见《MySQL基础(一):安装配置》
- 显示当前账户下的数据库
show databases
- 使用某个数据库
use <数据库名>
- 显示该数据库中的数据表
show tables
- 在当前数据库下执行相关操作
- 查看该表所有的索引
show index from <数据表>
创建索引
如前面所示,我们在查询该数据表的时候花了1.49秒,这个时间对于数据库来说已经不算短了,因此我们需要通过创建索引来提高查询效率。
create index <索引名> on <数据表>(索引字段)
这时我们可以查询一下所创建的索引:
如果感觉这样看太别扭,可以在命令后面加下 \G
:
这时我们再来看下查询效率,是不是很惊讶!
普通索引
添加普通索引的语句除了上面提及的
create index <索引名> on <数据表>(索引字段);
还有
alter table <数据表> add index <索引名>(索引字段);
注:还可以在创建表的时候直接添加索引
create table test1(
id int not null,
username varchar(50) not null,
index idx_username(username) -- 添加名为idx_username的索引,索引字段为username
);
唯一索引
create table test2(
id int not null,
username varchar(50),
unique index idx_username(username) -- 增加唯一索引
);
为了测试唯一索引的特性,我们试着往新建的表中插入记录:
insert into test2(id,username) values(1,'xy'); -- 添加数据
insert into test2(id,username) values(1,'xy'); -- 数据唯一插不进去
insert into test2(id,username) values(1,null); -- 空值不视为唯一值
主键索引
create table test3(
id int primary key not null,
username varchar(14),
unique index idx_username(username) -- 添加主键索引
);
为了测试主键索引的特性,我们试着往新建的表中插入记录:
insert test3(id,username) values(1,'xy'); -- 添加数据
insert test3(id,username) values(1,'xy'); -- 数据唯一插不进去
insert test3(id,username) values(null,'xy'); -- 空值插不进去
组合索引
为了更好地展示组合索引,我们创建一个包含多个字段的表:
create table payment2(
payment_id smallint(5) unsigned not null,
language_id tinyint(5) unsigned not null,
store_id tinyint(5) unsigned not null,
index idx_payment_language_store(payment_id,language_id,store_id)
-- 创建组合索引
)
select payment_id,language_id,store_id from payment,language,store;
除了在创建表的时候直接添加索引,也可以后续自行添加
create index idx_payment_language_store
on payment2(payment_id,language_id,store_id);
-- 创建组合索引
为了测试组合索引的特性,即最左匹配原则,我们可以这样做:
explain select * from payment2 where payment_id=1 and
language_id=1 and store_id=2;
-- 所有字段均参与查询
explain select * from payment2 where payment_id=1 and language_id=1;
-- 去掉store_id字段
explain select * from payment2 where payment_id=1 and store_id=2;
-- 去掉language_id字段
explain select * from payment2 where language_id=1 and store_id=2;
-- 去掉payment_id字段
/* 显示查询的具体信息 组合索引遵循最左匹配原则 */
观察上图,细细品味何谓最左匹配原则吧 😃
修改索引
就我目前的知识而言,如果想修改索引的话只能先删除当前索引,再创建新的索引
(如果有哪位兄弟姐妹知道其他方法,请告诉小弟一声,感激不尽)
删除索引
drop index <索引名> on <数据库>.<数据表>
alter table <数据库>.<数据表> drop index <索引名>