008.mysql-mysql索引失效几种场景

1.对过滤字段进行了函数处理

对字段做了函数计算,就用不上索引了

比如对时间类型的处理  select month(now())

2.发生了字段类型的隐式转换---尤其在存数据期时一定要注意,为数值类型

 

过滤值为字符型--走索引

 

 过滤值为数值型--字段的vachar发生隐式转换  相当于执行了函数 cast ('10'   as  signed int ),不走索引

 

 

附:

字符类型         '10' 比  '9'  小    返回0

 

 

数值类型   10 比  9  大  返回1

 

 

在数据库中如果一个是varchar  一个是int,  发生隐式转换

 

 相当于 cast ('10'   as  signed int )

 

 

3.关联时失效---字符集类型不同

bbb  utf8;    bb_s   utf8mb4

 

 

 

 

-- utf8   =  utf8b4  走索引
explain 
select 
a.*,b.*
from gaoshuiwei a 
,gaoshuiwei b 
where a.bbb = b.bb_s and a.bbb  = '127'


-- utf8b4    = utf8  不走索引
explain 
select 
a.*,b.*
from gaoshuiwei a 
,gaoshuiwei b 
where a.bb_s = b.bbb and a.bbb = '127'

-- utf8b4 转为utf8   = utf8   走索引
explain 
select 
a.*,b.*
from gaoshuiwei a 
,gaoshuiwei b 
where CONVERT(a.bb_s USING utf8) = b.bbb and a.bbb = '127'

1.

 

 

2.

 

 

3.

 

 

posted @ 2020-09-04 18:42  star521  阅读(225)  评论(0编辑  收藏  举报