2017-04-08 10 views
0

これは、計算されたゲームを計算するクエリです。各プレイヤーのウィンドローとロス、およびポイントの合計です。私は同じクエリ要素の多くを繰り返しているようです。新鮮な目が私をより効率的な方向に導くことを望んでいる。クエリの簡略化:mysqlでの繰り返しの計算を避ける

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; 

は我慢すると迅速にトリックをした次改正のクエリのバージョンを削除@learning

+----+-------------+------------+------------+--------+---------------+---------+---------+-------------------------+------+----------+----------------------------------------------------+ 
| 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          | 
+----+-------------+------------+------------+--------+---------------+---------+---------+-------------------------+------+----------+----------------------------------------------------+ 

答えて

0

EXPLAIN。私は彼のクエリから編集唯一のことは、明らかにコメントアウトされたビットが0に変数をリセットしますが、それは、それでは動作しませんでした/* */

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)[email protected] 
    AS Points, 
@[email protected][email protected] 
    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; 

の間に含まれる。

どんな問題がありますプロダクションサイトでこのラインナップをコメントしていますか?

関連する問題