0
点赞
收藏
分享

微信扫一扫

#yyds干货盘点# 24 MySQL 索引

萨科潘 2022-03-20 阅读 75

一、什么是索引

我们小时候都用过汉语字典,如果我们查询一个字的时候,我们肯定不会从第一页开始找,一直找到这个字为止。而是通过字典提供根据拼音、偏旁部首的目录来快速定位。那这个目录在MySQL我们就可以叫索引。


创建表

create table test_index (

id int not null primary key auto_increment,

name varchar(10) not null

);
set profiling = 1;


select * from test_index where name = '我的年龄49999';


show profiles ;


创建索引

create index name_index on test_index(name(10));
select * from test_index where name = '我的年龄49999';

show profiles;


显示索引

show index from test_index;


删除索引

drop index 索引名字 on test_index;


二、索引种类

1.普通索引:仅加速查询


2.主键索引:加速查询 + 列值唯一(不可以有null)+ 表中只有一个


3.唯一索引:加速查询 + 列值唯一(可以有null)

create unique index 索引名字 on 表名(字段(长度));


4.组合索引:多列值组成一个索引,专门用于组合搜索,其效率大于索引合并

create index 索引名字 on 表名(字段1(长度),字段2(长度));

create unique index 索引名字 on 表名(字段1(长度),字段2(长度));


5.全文索引:对文本的内容进行分词,进行搜索

create fulltext index 索引名字 on 表名 (字段1(长度));

create fulltext index 索引名字 on 表名 (字段1(长度),字段2(长度));

三、索引的作用

索引可以提高我们的查询效率


四、索引何时建立

  • 数据量非常大的时候

  • 字段在查询次数非常多

  • 字段内容不频繁变化


五、索引的缺点

  • 会占用一定磁盘空间

  • 会影响更新、插入、删除效率


举报

相关推荐

0 条评论