2014-09-01 53 views
0

我在MySQL特定查詢它使用整理兩個不同的表(索引)比較值,但這並不執行了幾個小時。查詢提供如下: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 
    ) 

FYI的elig_temp表有70K的記錄,而med_temp有100萬條記錄。
另外,table_elig的id字段和table_med表是從同一個表加密另一個字段的值的哈希值。因此,我嘗試使用二進制排序規則技術,如udf8_bin和latin1_bin使查詢運行,但我再次卡住。

我甚至通過用相同的排序規則的技術限定,即我與查詢中使用,用於table_med和table_elig但沒有運氣每個字段(VARCHAR和焦炭)嘗試。

請與有效執行此查詢時間的任何可能的解決方案建議我。

回答

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

然後你可以運行上面的查詢 - 沒有明確的排序規則 - 它應該使用索引。

+0

感謝戈登的建議。是的,查詢在沒有整理的情況下運行。當我嘗試使用下面的解決方案**中的代碼時,elig_temp和med_temp中的字段名稱顯示爲** id collat​​e latin1_general_cs **,這不正確。 – Syrus 2014-09-02 09:21:12

+0

@Syrus。 。 。只需使用'as'來設置正確的列名。 – 2014-09-02 12:04:09

+0

糟糕!我錯過了什麼,沒有我。非常感謝,這解決了我的問題。 – Syrus 2014-09-03 06:11:06