0

这个查询需要很长的时间来执行。持续时间/获取:89.778 秒/0.000 秒

这是查询:

SELECT tblcompanyoperat.COPSCountry,
       AVG(tbldshipfees.Dship Total AnnualUSD) / 
                              AVG(tblcopsyearonyear.COPSMinWageUSD) AS Expr1
FROM (SELECT tblcopsyearonyear.Company NAME,
          tblcopsyearonyear.COPSCountry,
          MAX(tblcopsyearonyear.COPSFinYearEnd) AS MaxOfCOPSFinYearEnd
      FROM tblcopsyearonyear
      GROUP BY tblcopsyearonyear.Company NAME,
        tblcopsyearonyear.COPSCountry
      ORDER BY tblcopsyearonyear.COPSCountry,
        MAX(tblcopsyearonyear.COPSFinYearEnd)) AS qryCOPSLatestInfoYr,
     ((tblcompany 
       INNER JOIN tblcompanyoperat 
       ON tblcompany.CompName = tblcompanyoperat.Company NAME) 
      INNER JOIN tblcopsyearonyear 
      ON tblcompanyoperat.COMPOPID = tblcopsyearonyear.COMPOPID)
INNER JOIN (tbldirectorships 
            INNER JOIN tbldshipfees 
            ON tbldirectorships.DIRECTORSHIPSID = bldshipfees.DSHIPID) 
ON tblcompany.CompName = tbldirectorships.DSHIPCOMPANYLINK
WHERE blcompany.CompSector = 'Retail'
GROUP BY tblcompany.CompSector, 
         blcompanyoperat.COPSCountry,
         tbldshipfees.DSHIP Position
HAVING (AVG(tblcopsyearonyear.COPSMinWageUSD) IS NOT NULL
        AND tbldshipfees.DSHIP Position LIKE 'Chief%')
ORDER BY MAX(qryCOPSLatestInfoYr.MaxOfCOPSFinYearEnd);

除了索引之外,我还可以使用哪些优化来加快速度?解释扩展输出:

id selet_type   table                 type possible_keys                                          key                         key_len  ref                                       rows filtered Extra
1, PRIMARY,     tblcompany,           ref, PRIMARY,tblSECTORStblCOMPANY,                          tblSECTORStblCOMPANY,       768,     const,                                    7,   100.00,  Using where; Using index; Using temporary; Using filesort
1, PRIMARY,     tblcompanyoperat,     ref, PRIMARY,CompanyName,                                   CompanyName,                768,     lrsmnc.tblcompany.CompName,               3,   100.00,  Using where
1, PRIMARY,     tblcopsyearonyear,    ref, PRIMARY,COMPOPID,                                      COMPOPID,                   4,       lrsmnc.tblcompanyoperat.COMPOPID,         1,   100.00, 
1, PRIMARY,     tbldirectorships,     ref, PRIMARY,DIRECTORSHIPSID,tblCOMPANYtblDIRECTORSHIPS,    tblCOMPANYtblDIRECTORSHIPS, 768,     lrsmnc.tblcompanyoperat.Company Name,     12,  100.00,  Using where; Using index
1, PRIMARY,     tbldshipfees,         ref, DSHIPID,tbldshipfeesDSHIPID,                           DSHIPID,                    4,       lrsmnc.tbldirectorships.DIRECTORSHIPSID,  1,   100.00, 
1, PRIMARY,     <derived2>,           ALL,                                                                                                                                       310, 100.00,  Using join buffer
2, DERIVED,     tblcopsyearonyear,    ALL,                                                                                                                                       523, 100.00,  Using temporary; Using filesort
4

1 回答 1

1

tbldshipfees.DSHIP Position LIKE 'Chief%'HAVING子句移到子句WHERE将缩短执行时间。这有两个原因。

首先,没有对出现在HAVINGMySQL 子句中的项目进行优化。这意味着不能使用索引排除项目(如EXPLAIN上面的计划中所示。

其次,因为该HAVING子句是在将数据返回给客户端之前立即应用的。结果,在所有操作(聚合函数、ORDER BY 等)被删除之前,时间都不必要地花费在它们上,从而增加了处理时间。

参考: http ://dev.mysql.com/doc/refman/5.5/en/select.html

此外,出于未来维护的原因,建议从隐式连接更改为显式连接,根据 X-Zero 的评论。这可能会带来额外的性能优势,因为它减少了数据库服务器所需的优化。

于 2012-08-10T21:33:01.087 回答