表格: 請在此處查看以查看錶格。 How to query counting specific wins of team and find the winner of the series如何在左連接中使用子查詢時優化查詢
問題:
- 如何讓更多的查詢優化?
- 如何減少查詢冗餘?
- 如何使此查詢更快?
摘要
正如你可以在這個查詢例子這一部分是多次使用看看。
WHERE leagueid = 2096
AND start_time >= 1415938900
AND ((matches.radiant_team_id= 1848158 AND matches.dire_team_id= 15)
OR (matches.radiant_team_id= 15 AND matches.dire_team_id= 1848158))
SELECT matches.radiant_team_id,
matches.dire_team_id,
matches.radiant_name,
matches.dire_name,
TA.Count AS teamA,
TB.Count AS teamB,
TA.Count + TB.Count AS total_matches,
SUM(TA.wins),
SUM(TB.wins),
(CASE
WHEN series_type = 0 THEN 1
WHEN series_type = 1 THEN 2
WHEN series_type = 2 THEN 3
END) AS wins_goal
FROM matches
LEFT JOIN
(SELECT radiant_team_id,
COUNT(id) AS COUNT,
CASE
WHEN matches.radiant_team_id = radiant_team_id && radiant_win = 1 THEN 1
END AS wins
FROM matches
WHERE leagueid = 2096
AND start_time >= 1415938900
AND ((matches.radiant_team_id= 1848158
AND matches.dire_team_id= 15)
OR (matches.radiant_team_id= 15
AND matches.dire_team_id= 1848158))
GROUP BY radiant_team_id) AS TA ON TA.radiant_team_id = matches.radiant_team_id
LEFT JOIN
(SELECT dire_team_id,
COUNT(id) AS COUNT,
CASE
WHEN matches.dire_team_id = dire_team_id && radiant_win = 0 THEN 1
END AS wins
FROM matches
WHERE leagueid = 2096
AND start_time >= 1415938900
AND ((matches.radiant_team_id= 1848158
AND matches.dire_team_id= 15)
OR (matches.radiant_team_id= 15
AND matches.dire_team_id= 1848158))
GROUP BY dire_team_id) AS TB ON TB.dire_team_id = matches.dire_team_id
WHERE leagueid = 2096
AND start_time >= 1415938900
AND ((matches.radiant_team_id= 1848158
AND matches.dire_team_id= 15)
OR (matches.radiant_team_id= 15
AND matches.dire_team_id= 1848158))
GROUP BY series_id
計劃匹配
ID| leagueid| team_a_id| team_b_id| starttime
1| 2096| 1848158| 15| 1415938900
WOOOOOOOOOOH。而已。你是最好的。非常感謝你 。這個問題是1周老〜〜/〜然後在最後。你解決了它:D非常感謝! – ronscript 2014-11-24 00:55:07
我有問題。如果我想使radiant_team_id和dire_team_id動態,該怎麼辦?例如我不想過濾出使用團隊ID。 – ronscript 2014-11-24 01:16:29
這取決於。一系列確定了一個獨特的團隊對?或者它確實解決了可以應用於多個團隊對的階段?除此之外,您需要考慮模型層面有多少邏輯,以及可以使用業務邏輯來管理多少邏輯。 – amenadiel 2014-11-24 01:23:10