0

I have such a query:

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" table schema:

enter image description here

"city_localization" table schema:

enter image description here

Explain output:

enter image description here

How can I avoid using filesort and temporary?

1
  • I don't know about filesort and temporary, but I've noticed that you filter right side of left join, replacing it in effect with inner join. One should filter city_localization in join itself: LEFT OUTER JOIN city_localization localization ON ( localization.city_id = city.id and localization.lang = 'en'). Commented Apr 8, 2012 at 20:13

1 Answer 1

1

You'll need to create an index for localization.city_id and another one for localization.name (you currently have one that also includes localization.lang).

If you are retrieving a lot of rows you might want to remove city.country_id and localization.lang from your select, after all you already have those values.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.