2017-11-04 138 views
0

我需要編寫一個涉及子查詢和索引的查詢。所以,我想出了一個查詢,發現其具有最高的積累速度在一年中的隊名:在MySql Workbench中對索引查詢

SELECT CONCAT(team_long_name, ',', team_short_name) AS TeamName, 
     EXTRACT(YEAR FROM date) AS Year, 
     buildUpPlaySpeed 
FROM team JOIN 
    team_attributes 
    ON team.team_api_id = team_attributes.team_api_id 
WHERE 
    (buildUpPlaySpeed,team_attributes.date) in (SELECT 
      MAX(buildUpPlaySpeed),team_attributes.date 
     FROM team_attributes 
     WHERE team_attributes.date = team_attributes.date 
     group by team_attributes.date) 
ORDER BY date desc; 

指標上存在日期,team_api_id,buildUpPlaySpeed列。

關於如何進一步降低成本的任何建議?

+0

羅恩,似乎你可能會很新的#1。如果問題已得到解答,您需要指出是哪個答案,即您「接受」答案。要接受「[**點擊Tick **](https://ibb.co/ikqyO6)」以獲取更多信息,請參閱[幫助/接受](https://stackoverflow.com/help/someone-answers) –

回答

0

是的。用加入替換相關子查詢:

SELECT CONCAT (
    team_long_name 
    ,',' 
    ,team_short_name 
    ) AS TeamName 
    ,EXTRACT(YEAR FROM [DATE]) AS Year 
    ,buildUpPlaySpeed 
FROM team 
INNER JOIN team_attributes ON team.team_api_id = team_attributes.team_api_id 
INNER JOIN (
    SELECT MAX(buildUpPlaySpeed) maxbuildup 
     ,team_attributes.[DATE] 
    FROM team_attributes 
    WHERE team_attributes.[DATE] = team_attributes.DATE 
    GROUP BY team_attributes.[DATE] 
    ) x ON buildUpPlaySpeed = x.maxbuildup 
    AND team_attributes.DATE = x.[DATE] 
ORDER BY [DATE] DESC 
; 

,並且對於理智的緣故,因爲這是使用SQL請不要命名列「日期」。很混亂。

+0

謝謝它有效:) – Ron

+0

太棒了。你會接受這個答案嗎? –

+0

您對此答案仍有疑問嗎?要接受答案「[**點擊Tick **](https://ibb.co/ikqyO6)」以獲取更多信息,請參閱[help/accepting](https://stackoverflow.com/help/someone-answers)* * ps:當你接受答案時你獲得2點聲望點** –

0

嘗試,這也

SELECT CONCAT(team_long_name,',',team_short_name) AS TeamName,EXTRACT(YEAR FROM DATE) AS Year,buildUpPlaySpeed 
FROM team 
INNER JOIN team_attributes ON team.team_api_id = team_attributes.team_api_id 
INNER JOIN (SELECT MAX(buildUpPlaySpeed) maxbuildup,team_attributes.DATE FROM team_attributes 
    WHERE team_attributes.DATE = team_attributes.DATE 
    GROUP BY team_attributes.DATE ) t1 
where buildUpPlaySpeed = t1.maxbuildup AND team_attributes.DATE = t1.DATE ORDER BY DATE DESC;