[英]MySQL Collate Query Running Forever
我在MySQL中有一个特定的查询,在该查询中,使用归类比较两个不同表(索引)中的值,但这不会执行数小时。 查询提供如下:
create temporary table elig_temp
select id from table_elig;
create index elig_temp on elig_temp(id);
create temporary table med_temp
select id from table_med;
create index med_temp on med_temp(id);
select COUNT(1) as result
from med_temp a
where exists
(
select 1
from elig_temp b
where a.id collate latin1_general_cs = b.id collate latin1_general_cs
)
仅供参考elig_temp表具有70k条记录,而med_temp表具有100万条记录。
同样,table_elig和table_med表的id字段是同一表中另一个字段的哈希加密值。 因此,我也尝试使用二进制排序规则技术,例如udf8_bin和latin1_bin来使查询运行,但我再次陷入困境。
我什至尝试通过对table_med和table_elig的每个字段(varchar和char)使用与查询相同的归类技术进行定义,但是没有运气。
请为我提供任何可能的解决方案,以有效地执行此查询时间。
当您显式设置排序规则时,MySQL将无法在列上使用索引。 这是不幸的。
首先,查询是否在没有排序规则的情况下运行?
select COUNT(1) as result
from med_temp a
where exists (select 1 from elig_temp b where a.id = b.id collate );
那是最简单的解决方案。
下一个解决方案是在创建表时使排序规则相同:
create temporary table elig_temp
select id collate latin1_general_cs as id
from table_elig;
create index elig_temp on elig_temp(id);
create temporary table med_temp
select id collate latin1_general_cs as id
from table_med;
create index med_temp on med_temp(id);
然后,您可以运行上述查询-无需显式排序规则-并且它应使用索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.