简体   繁体   English

MySQL Collat​​e查询永远运行

[英]MySQL Collate Query Running Forever

I have a particular query in MySQL where it compares values from two different table (indexed) using collate but this does not execute for hours. 我在MySQL中有一个特定的查询,在该查询中,使用归类比较两个不同表(索引)中的值,但这不会执行数小时。 The query is provided below: 查询提供如下:

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
      )

FYI The elig_temp table has 70k records whereas the med_temp has 1 million records. 仅供参考elig_temp表具有70k条记录,而med_temp表具有100万条记录。
Also, the id fields of table_elig and table_med tables are the hash encrypted values of another field from the same table. 同样,table_elig和table_med表的id字段是同一表中另一个字段的哈希加密值。 Therefore, I tried to use binary collations techniques too, such as udf8_bin and latin1_bin to make the query run but I am stuck again. 因此,我也尝试使用二进制排序规则技术,例如udf8_bin和latin1_bin来使查询运行,但我再次陷入困境。

I have even tried by defining with the same collation techniques, that I used with query, for each fields (varchar and char) of table_med and table_elig but no luck. 我什至尝试通过对table_med和table_elig的每个字段(varchar和char)使用与查询相同的归类技术进行定义,但是没有运气。

Please suggest me with any possible solution for executing this query time efficiently. 请为我提供任何可能的解决方案,以有效地执行此查询时间。

When you explicitly set a collate, then MySQL cannot use indexes on the column. 当您显式设置排序规则时,MySQL将无法在列上使用索引。 This is unfortunate. 这是不幸的。

First, does the query run without collations? 首先,查询是否在没有排序规则的情况下运行?

select COUNT(1) as result 
from med_temp a 
where exists (select 1 from elig_temp b where a.id  = b.id collate );

That is the simplest solution. 那是最简单的解决方案。

The next solution is to make the collations the same when you create the tables: 下一个解决方案是在创建表时使排序规则相同:

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);

Then you can run the above query -- without the explicit collations -- and it should use the indexes. 然后,您可以运行上述查询-无需显式排序规则-并且它应使用索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM