索引:帮助MySQL高效获取数据的数据结构
提取句子主干,就可以得到索引的本质:索引是数据结构
索引的分类
- 主键索引(primary key):唯一标识,不可重复,只能有一个列作为主键
- 唯一索引(unique key):避免重复的列出现,唯一索引可以重复,多个列都可以标识为唯一索引
- 常规索引(key/index):默认的,index、key关键字设置
- 全文索引(FullText):在特定的数据库引擎下才有,MyISAM
索引的使用
- 在创建表的时候给字段增加索引
- 创建完毕后,增加索引
--显示所有的索引信息
show index from student
--增加一个全文索引
alter table student add fulltext index 'studentName';
-- explain 分析sql语句
explain select * from student; --非全文索引
--全文索引在数据量少的情况下没有用
explain select * from student where match(studentName) against('刘');--查一条数据
测试索引
/*插入一百万条数据*/
delimiter $$ /*写函数之前必须要写,标志*/
create function mock_data()
returns int
begin
declare num int default 1000;
declare i int default 0;
while i<num do
/*插入语句*/
set i = i+1;
end while ;
return i;
end;
–索引常用的命名方式:id_表名_字段名
--create index 索引名 on 表(字段)
create index id_app_user_name on app_user('name');
explain select * from app_user where name='iii';
索引在小数据量的时候,用户不大,但是在大数据的时候,区别十分明显
索引原则
- 索引不是越多越好
- 不要对经常变动的数据加索引
- 小数据量的表不需要加索引
- 索引一般加在常用来查询的字段上
索引的数据结构
hash类型的索引
Btree:InnoDB默认的数据结构