简体   繁体   中英

MySQL Query (with subquery) optimization

I'm having trouble figuring how to speed up the query below.

SELECT 
(SELECT SUM(pi2.OperationMW) FROM projectsimport pi2 
    WHERE pi2.CommissionYear  <=  pi.CommissionYear 
    AND pi2.TurbineOEM = pi.TurbineOEM 
    AND pi2.Country = 'US' )  as y,
    TurbineOEM as Name , CommissionYear  
FROM projectsimport pi 
WHERE TurbineOEM != '' AND Country = 'US' 
AND CommissionYear > 1993 
AND CommissionYear <= 2018 
AND CommissionYear IS NOT NULL    
GROUP BY TurbineOEM, CommissionYear 
Order by TurbineOEM, CommissionYear

Running explain gives me the following results:

explain results

I feel like I'm missing something pretty obvious but any help would be appreciated

Composite indexes for projectsimport :

INDEX(Country, TurbineOEM, CommissionYear, OperationMW)
INDEX(Country, CommissionYear, TurbineOEM)

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