索引 index unique(辅助索引) primary key(聚簇索引)
创建:create index 索引名 on 表名(字段名);
删除:drop index 索引名 on 表名
知识兔正确的使用索引(**)
1.只有对创建了索引的列进行条件筛选的时候效率才能提高.
2.索引对应的列做条件不能参与运算,不能使用函数.
3.当某一列的区分度非常小(重复率高),不适合创建索引.
4.当范围作为条件的时候,查询结果的范围越打越慢,越小越快.
5.like关键字:如果使用%/_开头都无法命中索引.
6.多个条件:如果只有一部分创建了索引,条件用and相连,那么可以提高查询效率.如果用or相连,不能提高查询效率
and
select * from s1 where id =100000 and email = 'eva100000@oldboy'
or
select * from s1 where id =100000 or email = 'eva100000@oldboy'
7.联合索引:
条件不能用or相连
要服从最左前缀原则
从使用了范围的条件开始之后的索引都失效
create index ind_mix on s1(id,name,email);
知识兔
基础概念(介绍)
explain 执行计划
explain select * from s1 where id =100000 and email = 'eva100000@oldboy'
覆盖索引
在查询的过程中不需要回表--覆盖索引
select count(id) from s1 where id<1000;
索引合并
explain select count(*) from s1 where id =100000 or email = 'eva100000@oldboy'
知识兔