1.什么是索引
1)索引就好比一本书的目录,它能让你更快的找到自己想要的内容。
2)让获取的数据更有目的性,从而提高数据库检索数据的性能。
2.索引的种类
1)BTREE:B+树索引(Btree,B+tress,B*tree)
2)HASH:HASH索引 (memery搜索引擎支持)
3)FULLTEXT:全文索引 (只可以用在myisam搜索引擎,一般纯文本数据格式才会使用)
4)RTREE:R树索引 (仅支持geometry数据类型,经纬度)
3.Btree
4.B+tree
5.B*tree
6.索引根据算法分类
索引是建立在表的字段上面的
当where后面接的条件里有索引会加快查询速度
①主键索引
建表时直接添加主键索引
create table student(id int not null primary key); #方式1
create tbale student(id int not null,primary key(id)); #方式2
已经建好的表添加主键索引
alter table student add primary key name(id); #给id添加主键索引,name自定义
查看索引
show index from student;
②唯一键索引
建表时直接添加唯一键索引
create table student(id int not null unique key); #方式1
添加唯一键索引
alter table student add unique key name(id); #给id添加唯一键索引,name自定义
注意事项:添加唯一键索引时,同一列不能有相同数据 ,以下两个值相同时可以添加
select count(name) from student;#查看数据行数量 类似wc -l
select distinct(name) from student;#数据行去重 类似uniq -c
③普通索引
添加普通索引
alter table student add index idx_name(name); #idx_name名字自定义 方式1
create index idx_name on student(name); #在表student列name 添加普通索引
④全文索引
create table pull(id int, bookname varchar(10), content text, fulltext(content));#给书的内容添加全文索引
查询数据时:select * from pull where match(content) against(‘查找的字符串’);
7.删除索引
alter table city drop index idx_dis;