🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] ## 索引失效的场景 ### 隐式转换导致索引失效. ``` 错误的例子:select * from test where tu_mdn=13333333333; 正确的例子:select * from test where tu_mdn='13333333333'; ``` ### 对索引列进行运算导致索引失效 ``` 错误的例子:select * from test where id-1=9; 正确的例子:select * from test where id=9+1; ``` ### 使用内部函数导致索引失效 ``` // 错误的例子: select * from test where round(id)=10; //说明,此时id的索引已经不起作用了 //正确的例子:首先建立函数索引 create index test_id_fbi_idx on test(round(id)); //然后 select * from test where round(id)=10; ``` ### 不要将空的变量值直接与比较运算符(符号)比较 如果变量可能为空,应使用 IS NULL 或 IS NOT NULL 进行比较,或者使用 ISNULL 函数。 ### 不要在 SQL 代码中使用双引号 因为字符常量使用单引号。如果没有必要限定对象名称,可以使用(非 ANSI SQL 标准)括号将名称括起来。 ### 以下使用会使索引失效,应避免使用 ``` a. 使用 <> 、not in 、not exist、!= b. like "%_" 百分号在前(可采用在建立索引时用reverse(columnName)这种方法处理) d. 字符型字段为数字时在where条件里不添加引号. e. 当变量采用的是times变量,而表的字段采用的是date变量时.或相反情况。 ``` ### 使用` <> ` 失效场景详情 建立了 `age height weight`索引 在 `SELECT * FROM user WEHRE age=1 and height>2 and weight=7`索引只到 `age height`