2017-08-02 39 views
0

我的查詢使用臨時表,我做錯了,有人可以告訴我嗎?使用臨時的Mysql

EXPLAIN SELECT DISTINCT t.city_id FROM (SELECT city_id FROM `geo__base` Where country = 'DE')as t 


id select_type  table type possible_keys key  key_len  ref  rows Extra 
1 SIMPLE geo__base ref  country  country  8 const 43460 Using index condition; Using where; Using temporar... 

感謝所有

+1

沒有必要的子查詢。只要'從geo_base中選擇不同的city_id ...'。也就是說,臨時表並不一定是壞事。這是記憶中的一張桌子。只有當它說'磁盤上的臨時表'時,它會變得很糟糕。 – fancyPants

回答

0

嘗試是這樣的:

SELECT DISTINCT city_id FROM (SELECT city_id FROM geo_base WHERE country = 'DE') tmp; 

或者如上述評論的FancyPants,只要使用此:

SELECT DISTINCT city_id FROM geo_base;