MySQL Query(带子查询)优化

时间:2018-06-09 14:08:21

标签: mysql indexing query-optimization

我无法确定如何加快下面的查询速度。

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

运行说明给出了以下结果:

explain results

我觉得我错过了一些非常明显的东西,但任何帮助都会受到赞赏

1 个答案:

答案 0 :(得分:0)

projectsimport的综合索引:

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