0

我在 MySQL 中有一个特定的查询,它使用 collat​​e 比较来自两个不同表(索引)的值,但这不会执行几个小时。查询如下:

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),但没有运气。

请向我建议任何可能的解决方案,以有效地执行此查询时间。

4

1 回答 1

0

当您显式设置排序规则时,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);

然后你可以运行上面的查询——没有明确的排序规则——它应该使用索引。

于 2014-09-01T12:20:15.820 回答