我目前遇到一个问题,当我在查询中使用“LIKE”时,我会在 2 秒内得到结果。但是当我改用“=”时,结果显示大约需要 1 分钟。 以下是我的查询:
- 查询1
以下查询需要 2 秒:
`select distinct p.Name from Timeset s
join table1 f on (f.id = s.id)
join table2 p on (p.source=f.table_name)
join table3 d on (d.Name = p.Name) WHERE
s.Active = 'Y' AND **p.sourcefrom like '%sometable%'`
- 查询2
将“like”替换为“=”的相同查询需要 1 分钟:
select distinct p.Name from Timeset s
join table1 f on (f.id = s.id)
join table2 p on (p.source=f.table_name)
join table3 d on (d.Name = p.Name) WHERE
s.Active = 'Y' AND **p.sourcefrom = 'sometable'
我真的很困惑,因为我知道 'LIKE' 通常比 '=' 慢,因为 mysql 需要寻找不同的可能性。但我很确定为什么在我的情况下,“=”速度较慢,但差异如此之大。
提前感谢您的帮助,
问候,
最佳答案
当您使用 =
时,与您使用 LIKE
相比,MySQL 可能使用不同的索引。检查两个执行计划的输出,看看有什么不同。然后你可以强制使用性能更好的索引。可能值得为涉及的每个表运行 ANALYZE TABLE
。
关于带有 '=' 的 Mysql 查询比 'LIKE' 慢很多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9726728/