简化查询:避免在mysql中重复计算

时间:2017-04-08 11:29:58

标签: mysql

这是一个计算游戏,Wins每个玩家的抽奖和损失以及他们的积分总数的查询。我似乎在重复许多相同的查询元素。我希望一双新鲜的眼睛能指引我走向更有效的方向。

GAMES ->
GameID,     GameDate,       Winner
1           xxxx-xx-xx      A
2           xxxx-xx-xx      D
3           xxxx-xx-xx      B

SELECTIONS ->
GameID,     PlayerID,       Team
1           1               A
1           2               B
2           1               A
2           2               B
3           1               A
3           2               B

PLAYERS ->
PlayerID,   Name
1           John
2           Mike


QUERY ->

SELECT 
    Selections.PlayerID, 
    Players.Name, 
    COUNT(Games.Winner=Selections.Team)+COUNT(Games.Winner='D')+COUNT(Games.Winner!='D' OR Games.Winner!=Selections.Team) 
        AS GamesPlayed,
    COUNT(Games.Winner=Selections.Team) 
        AS GamesWon, 
    COUNT(Games.Winner='D') 
        AS GamesDrawn, 
    COUNT(Games.Winner!=Selections.Team OR Games.Winner!='D') 
        AS GamesLost, 
    (COUNT(Games.Winner=Selections.Team)*3)+(COUNT(Games.Winner='D')) 
        AS Points 

FROM 
    Games,Players,Selections 
WHERE 
    Games.Winner=Selections.Team 
AND 
    Players.PlayerID=Selections.PlayerID 
AND 
    Games.GameID=Selections.GameID 
GROUP BY 
    Selections.PlayerID;

EXPLAIN

+----+-------------+------------+------------+--------+---------------+---------+---------+-------------------------+------+----------+----------------------------------------------------+
| id | select_type | table      | partitions | type   | possible_keys | key     | key_len | ref                     | rows | filtered | Extra                                              |
+----+-------------+------------+------------+--------+---------------+---------+---------+-------------------------+------+----------+----------------------------------------------------+
|  1 | SIMPLE      | Players    | NULL       | ALL    | PRIMARY       | NULL    | NULL    | NULL                    |    2 |   100.00 | Using temporary; Using filesort                    |
|  1 | SIMPLE      | Selections | NULL       | ALL    | NULL          | NULL    | NULL    | NULL                    |    6 |    16.67 | Using where; Using join buffer (Block Nested Loop) |
|  1 | SIMPLE      | Games      | NULL       | eq_ref | PRIMARY       | PRIMARY | 4       | btest.Selections.GameID |    1 |    33.33 | Using where                                        |
+----+-------------+------------+------------+--------+---------------+---------+---------+-------------------------+------+----------+----------------------------------------------------+

1 个答案:

答案 0 :(得分:0)

@learning提出并迅速删除以下修改后的查询版本。我从他的查询中编辑的唯一内容包含在/* */

之间
SELECT 
Selections.PlayerID, 
Players.Name, 
@win:=COUNT(Games.Winner=Selections.Team) 
    AS GamesWon, 
@draw:=COUNT(Games.Winner='D') 
    AS GamesDrawn, 
@lost:=COUNT(Games.Winner!=Selections.Team OR Games.Winner!='D') 
    AS GamesLost, 
(@win*3)+@draw
    AS Points,
@win+@draw+@lost
    AS GamesPlayed 
FROM 
Games,Players,Selections  /*, (select @lost:=0,@win:=0,@draw:=0) c */
WHERE Games.Winner=Selections.Team 
AND Players.PlayerID=Selections.PlayerID 
AND Games.GameID=Selections.GameID 
GROUP BY Selections.PlayerID;

注释掉的位显然会将变量重置为0,但它并没有使用它。

在生产网站中评论此行是否有任何问题?