我正在尝试优化以下查询:

Select distinct cadData.id FROM CAD_Data cadData
INNER JOIN Sales_Data SD
ON cadData.CAD_Acct=SD.CAD_Acct
and SD.List_Date = (
                    select max(List_Date)
                    from Sales_Data
                    where Sales_Data.CAD_Acct=cadData.CAD_Acct
                   )
where cadData.GMA_Tag = 101
AND SD.List_Status NOT IN('ACT','OP','PEND','PSHO','pnd')
ORDER BY cadData.id asc limit 10


这两个表都有超过1000万行。 CAD_Data表由CAD_Acct和GMA_Tag列索引。除Sales_data外,还通过CAD_Acct,GMA_Tag,List_Date,List_Status列进行索引。解释显示mysql - 优化选择查询。使用联接,不在和子查询-LMLPHP

我需要一些建议来优化此查询。提前致谢。

最佳答案

Select distinct cadData.id
FROM CAD_Data cadData
INNER JOIN Sales_Data SD
ON cadData.CAD_Acct=SD.CAD_Acct
INNER JOIN
(
select CAD_Acct,max(List_Date) AS List_Date
from Sales_Data
GROUP BY CAD_Acct
) T
ON SD.List_Date=T.List_Date
AND T.CAD_Acct=cadData.CAD_Acct
where cadData.GMA_Tag = 101
AND SD.List_Status NOT IN('ACT','OP','PEND','PSHO','pnd')
ORDER BY cadData.id asc limit 10

07-28 02:47