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.
当您有子查询时,需要创建一个临时表并将数据插入其中。
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.