簡體   English   中英

MySQL Collat​​e查詢永遠運行

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM