(1) 范围查询, 右边的列不能使用索引, 否则右边的索引也会失效.
索引生效案例
select * from tb_seller where name = "小米科技" and status = "1" and address = "北京市";
select * from tb_seller where name = "小米科技" and status >= "1" and address = " 北京市";
索引失效案例
select * from tb_seller where name = "小米科技" and status > "1" and address = "北京市";
address 索引失效, 因为 status 是大于号, 范围查询.
(2) 不要在索引上使用运算, 否则索引也会失效.
比如在索引上使用切割函数, 就会使索引失效.
select * from tb_seller where substring(name, 3, 2) = "科技";
(3) 字符串不加引号, 造成索引失效.
如果索引列是字符串类型的整数, 条件查询的时候不加引号会造成索引失效. Mysql 内置的 优化会有隐式转换.
索引失效案例
select * from tb_seller where name = "小米科技" and status = 1
(4) 尽量使用覆盖索引, 避免 select *, 这样能提高查询效率.
如果索引列完全包含查询列, 那么查询的时候把要查的列写出来, 不使用 select *
select sellerid, name, status from tb_seller where name = "小米科技" and staus = "1" and address = "西安市";
(5) or 关键字连接
用or 分割开的条件, 如果 or 前面的列有索引, or 后面的列没有索引, 那么查询的时候前后索引都会失效。
如果一定要用or 查询, 可以考虑下 or 连接的条件列都加索引, 这样就不会失效了.