mysql LIKE where 带参数的子句不使用索引

Sam*_*ppe 4 mysql

在我的测试中,包含与参数比较的 LIKE 的 where 子句的 mysql select 语句不会使用索引。全表扫描完成,性能受到影响。例如

set @gp1:= 'BOB%';
select * from quote where quoteNum like @gp1; -- this is slow
Run Code Online (Sandbox Code Playgroud)

如果值是内联的,则使用索引。例如

select * from quote  where quoteNum like 'BOB%'; -- this is fast
Run Code Online (Sandbox Code Playgroud)

有没有办法强制mysql在第一个例子中使用索引?

Sam*_*ppe 6

变量的字符集和排序规则必须与查询工作的列相同。

SET character_set_connection = latin1;
SET collation_connection = latin1_swedish_ci;
set @gp1:= 'BOB%';
select * from quote where quoteNum like @gp1; -- this is fast now
Run Code Online (Sandbox Code Playgroud)

这个答案正在处理类似的问题。/sf/answers/1052299041/