0
这里是我的查询:为什么我的查询不使用任何索引?
select u.*,
concat(user_fname, ' ', user_lname) like concat(?, '%') `both`,
user_lname like ? last_name
from users u
where concat(user_fname, ' ', user_lname) like concat(?, '%')
or user_lname like concat(?, '%')
order by `both`*2 DESC, last_name
而且我有两个指标:users(user_fname,user_lname)
,users(user_lname)
。
这里是EXPLAIN
结果:
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra
1 | SIMPLE | u | ALL | user_lname | NULL | NULL | NULL | 9 | Using where
看到了吗?它不使用任何索引。为什么?以及如何使查询最佳?
您正在使用函数的结果。索引(如果有的话)是基于列值而不是函数的结果。 – Hogan
这个'concat(user_fname,',user_lname)like concat(?,'%')' –
要了解更多关于索引检查的知识:MySQL index [** TIPS **](http://mysql.rjweb.org/ doc.php/index_cookbook_mysql)这个网站http://use-the-index-luke.com/和这个视频.https://www.youtube.com/watch?v = ELR7-RdU9XU&index = 53&list = WL –