I have a Companies table and a Zipcodes table and I want to get the samllest subset of companyIDs before I apply additional processing. So often I put that in an inner select, grabbing the companyIDs on some WHERE condition and then go some aggregation or calculation (like maybe a distance calculation) on the outside.
For some reason I'm not understanding why this outer SELECT is slow while the inner query is blazing fast.
I know it has to do with the JOIN on the inside. Because removing it fixes the problem. Somehow MySQL is deriving the tables...
SELECT _i1.companyID FROM (
SELECT companyID
FROM Companies
JOIN Zipcodes ON Zipcodes.zipcode = Companies.companyZipcode
WHERE Companies.companyType = 'plumbing'
) _i1
Time: .8s
Without the outer query: .00x seconds
There are indexes on Zipcodes.zipcode
and Companies.companyZipcode
and Companies.companyType
and Companies.companyID
is the Primary Key.
Doing an EXPLAIN shows that the outer query causes MySQL to have a select_type of Derived instead of Simple.