简体   繁体   中英

Optimizing this query

I'm trying to optimize the following query but I don't know what to do. I know little of query optimization but 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM