1. 索引概述
1.1 索引功能
索引类型 | 功能 |
---|---|
普通索引 | index : 加速查找 |
唯一索引 | unique index:加速查找+约束 (唯一) |
全文索引 | fulltext index: 用于搜索很长一篇文章的时候,效果最好。 |
组合索引 |
1.2 优点
1.3 缺点
2. 创建索引
2.1 一般索引
create index 索引名 on 表名 (列名);
alter table 表名 add index 索引名(列名);
create table 表名(index[索引名](列名));
2.2 唯一索引
create unique index 索引名 on 表名 (列名);
alter table 表名 add unique index 索引名(列名);
create table 表名 (unique index[索引名](列名));
2.3 全文索引
create fulltext index 索引名 on 表名 (列1,列2);
alter table 表名 add fulltext index 索引名(列1,列2);
create table 表名 (fulltext index[索引名](列1,列2));
3. 显示索引
show index from 表名;
4. 删除索引
drop index 索引名 on 表名;
alter table 表名 drop index 索引名;
5. 判断查询是否使用索引
explain 查询命令;
(命令行里把;换成\G)
eg:
在创建索引后使用查询命令,可发现possible_keys 为 i_name;
删除索引后,可发现possible_keys 为 NULL;
参考:
https://zhuanlan.zhihu.com/p/29118331