简体   繁体   中英

Mysql subquery conversion inner join

I've read that join request are generally more efficient that subquery.

I was wondering if the query below could be converted to join. Personnaly, I don't see how this is possible.

Just for you to know, the calculation of distance being expensive, I just want to perform it on a subset (the result of the subquery). But then, the outer query is not keeping the "order by". This is why I must do a second order by on the outer query.

Is there a way to do that better ?

 SELECT alias.*, [calculation of distance using table cities] AS distance
 FROM ( SELECT item.*  FROM item  
        WHERE  item.postalCode IN ([Array of postal codes]) 
        ORDER BY item.creationTimestamp ASC LIMIT 0, 19 ) as myalias,
       country_cities 
 WHERE cities.postalCode = alias.postalCode 
 ORDER BY myalias.creationTimestamp ASC

YOU CAN CHANGE IT TO INNER JOIN as follows

SELECT alias.*, [calculation of distance using table cities] AS distance
 FROM ( SELECT item.*  FROM item  
        WHERE  item.postalCode IN ([Array of postal codes]) 
        ORDER BY item.creationTimestamp ASC LIMIT 0, 19 ) myalias INNER JOIN country_cities  
 WHERE cities.postalCode = alias.postalCode 
 ORDER BY myalias.creationTimestamp ASC

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