mysql 使用like索引是否生效
程序员文章站
2022-06-02 19:06:13
...
实验
在表中已经添加索引:device_serial_number -> idx_serial_number, access_time -> idx_access_time
% 在左边:
sql:
EXPLAIN SELECT tar.id FROM t_access_record tar
where 1=1
and tar.device_serial_number like 'D20001005%'
ORDER BY tar.access_time desc
LIMIT 0,20 )tarid
结果:使用idx_access_time索引而不是idx_serial_number
%在右边
sql:
EXPLAIN SELECT tar.id FROM t_access_record tar
where 1=1
and tar.device_serial_number like 'D20001005%'
ORDER BY tar.access_time desc
LIMIT 0,20
结果:使用idx_serial_number
%在两边
代码就和结果就不贴了,可想而知:使用idx_access_time索引
结论
like语句,只有%号在右边的时候才会生效!
下一篇: Linux——route