1、模糊匹配:当进行左模糊或全模糊查询时,例如 `like '%米'` 和 `like '%米%'`,索引通常不会被使用。
场景:
1)select * from user where name like '%米';
2)select * from user where name like '%米%';
2、隐式转换:如果查询条件中的数据类型与表中字段的数据类型不一致,可能会导致隐式类型转换,例如 `where name = 123` 或者 `join on` 关联字段数据类型不一致。
场景:
1)select * from user where name = 123;
2)select * from user a left join dept b on where a.userid = b.userid;//user表的userid是int类型,而dept表的userid是string类型
3、函数运算:在查询条件中使用函数运算时,索引不会被使用,例如 `where year(create_time) = '2024'`。
场景:select * from user where year(create_time) = '2024';
4、表达式运算:查询条件中包含表达式运算时,索引不会被使用,例如 `where id + 1 = 10`。
场景:select * from user where id + 1 = 10;
5、最左匹配原则:对于联合索引,如果不符合最左匹配原则,索引不会被使用,例如 `index(a,b,c) where a =1 and c =2`。
场景:
1)如果有一个 2 列的索引 (a, b),则已经对 (a)、(a, b) 上建立了索引;
2)如果有一个 3 列索引 (a, b, c),则已经对 (a)、(a, b)、(a, b, c) 上建立了索引。
6、OR操作:不当使用OR操作,可能会导致索引失效,例如 `where id > 1 or id > 10`。
场景:select * from user where id > 1 or id > 10;