MySQL提高了查询性能

时间:2012-04-08 19:27:08

标签: mysql optimization query-optimization

我有这样的疑问:

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

“city”表架构:

enter image description here

“city_localization”表架构:

enter image description here

解释输出:

enter image description here

如何避免使用filesort和临时?

1 个答案:

答案 0 :(得分:1)

您需要为localization.city_id创建一个索引,为localization.name创建另一个索引(您目前还有一个也包含localization.lang)。

如果您要检索大量行,则可能需要从选择中删除city.country_idlocalization.lang,毕竟您已经拥有这些值。