> For the complete documentation index, see [llms.txt](https://bing.gitbook.io/phper/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bing.gitbook.io/phper/data-base/my-sql/note/suo-yin-shi-xiao-de-chang-jing.md).

# 索引失效的场景

1. like的模糊查询以%开头时索引会失效；
2. 应该尽量避免在where字句中使用！=或者<>操作符，否则存储引擎将放弃使用索引而进行全表扫描；
3. 应该尽量避免在where子句中用or来连接条件，否则索引失效；
4. 对于多列索引（联合索引），比如(A,B,C)，如果没有命中第一个（如B,C或者C），那索引失效；
5. 如果列的类型是字符串，那一定要在条件中将数据用引号引用起来，否则不会使用索引；
6. 应该避免在where字句中对字段进行表达式操作，比如select id from t where num/2=10；应该改为select id from t where num=100\*2；
7. 不能在where字句中对字段进行函数操作，否则索引失效；
8. 不要在where字句中的"="左边进行函数、算术运算符或其它表达式运算，否则索引失效；
9. 如果Mysql估计使用全表扫描要比使用索引快，则不用索引；
10. 不适用键值比较少的列（重复数据多的列）
