gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 07:56:13 30 4
gpt4 key购买 nike

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

set @gp1:= 'BOB%';
select * from quote where quoteNum like @gp1; -- this is slow

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

select * from quote  where quoteNum like 'BOB%'; -- this is fast

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

最佳答案

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

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

这个答案是在处理类似的问题。 https://stackoverflow.com/a/15032843/176868

关于带参数的mysql LIKE where子句不使用索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28058772/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com