I'm trying to optimize the following query but I don't know what to do. I know little of query optimization but 开发者_C百科doesn't that key_len mean its searching through 386,386 rows? The cities table has 10,053 rows and states only 27.
SELECT c.city_name, s.state_name
FROM res_geo_cities AS c, res_geo_states AS s
WHERE c.id_state = s.id_state AND(
(
(c.metaphone_primary IN ($one,$two) OR c.metaphone_secondary IN ($one,$two)) AND
(s.metaphone_primary IN ($three,$four) OR s.metaphone_secondary IN ($three,$four))
) OR (
(c.metaphone_primary IN ($three,$four) OR c.metaphone_secondary IN ($three,$four)) AND
(s.metaphone_primary IN ($one,$two) OR s.metaphone_secondary IN ($one,$two))
))
LIMIT 50
This is the explain:
Could somebody be so kind as to point me in the right direction?
This means that you have two keys that each have a length of 386. Try rewriting this query with a join that has an 'ON' clause, instead of just selecting from multiple tables. Hope this helps.
精彩评论