2012-04-08 74 views
0

我有這樣一個查詢:MySQL的提高查詢性能

SELECT city.id, city.country_id, localization.lang , localization.name, ... some other fields ... 
FROM city city 
LEFT OUTER JOIN city_localization localization ON (localization.city_id = city.id) 
WHERE city.country_id = '196' AND localization.lang = 'en' 
ORDER BY localization.name 

「城市」 表模式:

enter image description here

「city_localization」 表模式:

enter image description here

說明輸出:

enter image description here

如何避免使用filesort和臨時?

+0

我不知道關於filesort和臨時的,但我注意到你過濾左連接的右側,用內連接取代它。應該在連接本身中過濾'city_localization':'LEFT OUTER JOIN city_localization localization ON(localization.city_id = city.id and localization.lang ='en')'。 – 2012-04-08 20:13:05

回答

1

您需要爲localization.city_id創建一個索引,併爲localization.name創建一個索引(您目前有一個索引也包含localization.lang)。

如果您正在檢索很多行,您可能想要從您的選擇中刪除city.country_idlocalization.lang,畢竟您已經擁有這些值。