索引失效
...大约 1 分钟
索引失效
索引失效的情况有哪些
- 使用左或者左右模糊匹配(
like %xx
或者like %xx%
),xx%
可以走索引 - 对索引使用函数 (
where sum(age)= 5、where length(name) = 6
等) - 对索引进行表达式计算 (
where id + 1 = 10
),但是进行计算式可以的,如where id = 10 - 1
- 对索引进行隐式类型转换,索引是字符串类型,查询条件中,输入的是整型,此时就不会走索引,直接走全表扫描,用
where age = 60
去查age varchar
此时不会走索引,直接走全表扫描;但是,如果索引是整型,查询条件中即使是字符串,也会走索引 (用where age = ‘ 60’
去查age int
) - 联合索引非最左匹配。创建一个(a,b,c)联合索引
- where a=1;
- where a=1 and b=2 and c=3;
- where a=1 and b=2;
- 以上三种情况是走索引
- where b=2;
- where c=3;
- where b=2 and c=3;
- 这三种情况不走索引
- where 子句中的 or 比如
where id = 1 or age = 18
,null、!=操作符
一张表最多有多少个索引?
取决于使用数据库管理系统(DBMS)和版本
MySQL
- InnoDB : 64个索引
- MyISAM :64个索引
- 聚集索引 : 1 个
索引的数量和长度有什么限制吗?
- InnoDB :最多64个索引,最长767字符,行大小限制为64KB
- MyIsam : 最多64个索引,最长1000个字符
怎么分析一个SQL语句有没有使用索引?
使用 explain
explain select * from your_table where column = 'value'