1. 负向条件不能使用索引

select id,name from orders where status != 2;

not in/not exists 都不太好,可以改写成in

2. 前导模糊查询不能使用索引

select id,name from users where username like '%vilay'; //不使用索引
select id,name from users where username like 'vilay%';//会使用索引

3. 数据区分度不大的字段不宜使用索引

select id,name from users where sex=1; //

理论上可以过滤80%以上才建议使用索引,具体看情况。

4. 如果业务大部分是单条查询,使用Hash索引性能更好

比如查询用户数据,可能大部分都只是查一条

B-Tree索引的时间复杂度是O(log(n)) Hash索引的时间复杂度是O(1)

5. 复合索引最左前缀,并不是指SQL语句的where顺序要和复合索引一致

比如用户表建立了符合索引idx_username_password(username,password)

select id,name from users where username=xx and password=xx; //可以命中索引
select id,name from users where  password=xx and username=xx; //可以命中索引
select id,name from users where username=xx; //可以命中索引
select id,name from users where password=xx;//不会命中索引 ,不符合最左前缀   ?这个可能需要进行测试,使用explain的时候possible_keys为null,但是key有值