2019-12-09

[if !vml]

[endif]

[if !supportLists]1.1.1.   [endif]字符串不加单引号导致类型转换,索引失效

下列哪个sql语句可以用到索引

[if !supportLists]Ø  [endif]EXPLAIN SELECTSQL_NO_CACHE * FROM emp WHERE name=123; 

[if !supportLists]Ø  [endif]EXPLAIN SELECT SQL_NO_CACHE* FROM emp WHERE name='123';

[if !vml]

[endif]

[if !supportLists]Ø  [endif]name=123发生类型转换,索引失效。

[if !supportLists]Ø  [endif]设计实体类属性时,一定要与数据库字段类型相对应,否则,就会出现类型转换的情况。

[if !supportLists]1.1.2.   [endif]小总结

假设index(a,b,c)

Where语句索引是否被使用

where a =  3Y,使用到a

where a = 3 and b =  5Y,使用到a,b

where a = 3 and b =  5 and c = 4Y,使用到a,b,c

where b = 3 或者where b = 3 and c =  4  或者where c =  4N

where a = 3 and c =  5使用到a, 但是c不可以,b中间断了

where a = 3 and b  > 4 and c = 5使用到a和b, c不能用在范围之后,b断了

where a is null and  b is not null 


 is null支持索引 但是is not null 不支持,所以 a 可以使用索引,但是  b不可以使用

where a <>  3  


不能使用索引

where abs(a)  =3不能使用 索引

where a = 3 and b  like 'kk%' and c = 4Y,使用到a,b,c

where a = 3 and b  like '%kk' and c = 4Y,只用到a

where a = 3 and b  like '%kk%' and c = 4Y,只用到a

where a = 3 and b  like 'k%kk%' and c = 4Y,使用到a,b,c

[if !supportLists]1.2.  [endif]一般性建议

[if !supportLists]Ø  [endif]对于单键索引,尽量选择过滤性更好的索引(例如:手机号,邮件,身份证)

[if !supportLists]Ø  [endif]在选择组合索引的时候,过滤性最好的字段在索引字段顺序中,位置越靠前越好。

你可能感兴趣的:(2019-12-09)