确定数据中的连胜条件

时间:2015-04-22 10:30:26

标签: php mysql performance mysqli

我知道还有其他帖子可以找到数据中的连胜条件,并且我已经使用它们来提出以下代码...问题是我的数据库是近20,000行,当找到条纹时一个团队已经玩了超过500场比赛(大多数球队)这个脚本需要很长时间才能运行并最终超时。我希望这个代码可以提高效率,所以我可以在我的网站上返回输出并避免服务器超时问题。如果团队玩的游戏数量较少,则查询按预期工作,只需要2-20秒即可显示。

该表包含这些列

ID | GameID | TeamID | Team | Opponent | WL

ID是唯一标识符,GameID是游戏编号,TeamID是该特定团队的游戏编号。

 $query =  "  

  SELECT C.WL, C.StartDate,
    (SELECT Team FROM MatchDetails WHERE TeamID = C.StartDate AND Team = '$Team') AS Club,
    (SELECT GameID FROM MatchDetails WHERE TeamID = C.StartDate AND Team = '$Team') AS StartStreakGameID ,
    (SELECT Opponent FROM MatchDetails WHERE TeamID = C.StartDate AND Team = '$Team') AS StartStreakOpposition ,
    (SELECT RoundNo FROM MatchDetails WHERE TeamID = C.StartDate AND Team = '$Team') AS StartRoundNo,
    (SELECT Season FROM MatchDetails WHERE TeamID = C.StartDate AND Team = '$Team') AS StartSeason, 
    C.EndDate,
    (SELECT GameID FROM MatchDetails WHERE TeamID = C.EndDate AND Team = '$Team') AS EndStreakGameID ,
    (SELECT Opponent FROM MatchDetails WHERE TeamID = C.EndDate AND Team = '$Team') AS EndStreakOpposition ,
    (SELECT RoundNo FROM MatchDetails WHERE TeamID = C.EndDate AND Team = '$Team') AS EndRoundNo,
    (SELECT Season FROM MatchDetails WHERE TeamID = C.EndDate AND Team = '$Team') AS EndSeason,
    C.Games

        FROM 

        (SELECT B.WL,B.StartDate,B.EndDate,B.Games
     FROM 
      (SELECT WL, MIN(TeamID) as StartDate, MAX(TeamID) as EndDate, COUNT(*) as Games FROM 
      (SELECT TeamID, WL, (SELECT COUNT(*) FROM MatchDetails G WHERE G.WL <> GR.WL AND G.TeamID <= GR.TeamID AND Team = '$Team') 
                as  RunGroup FROM MatchDetails GR WHERE Team = '$Team')  A 
     GROUP BY WL, RunGroup ORDER BY (Games))
    B WHERE WL = 'W' AND Games > 1 ORDER BY (Games) DESC LIMIT 10 ) C";

上面是SQL - 下面是PHP的输出和用法。

$result = mysqli_query($link, $query);
            $numrows = mysqli_num_rows($result) ;
            if($numrows > 0)
            {
            echo "<table class = tablescores>
            <tr>
            <th>Wins</th>
                    <th align=left>Team</th>
            <th align=left>Start Opponent</th>
            <th>Start Game</th>
            <th align=left>End Opponent</th>
            <th>End Game</th>
            </tr>";
            while($row = mysqli_fetch_array($result))
             {
                $venues = 'venues';
                echo "<tr class=$venues>";
                echo "<td align=center width=10%>".$row['Games']."</td>";            
                echo "<td width=20%>"."<a href='teamidx.php?Team=".$row['Club']."'>".$row['Club']."</td>";
                echo "<td width=20%>"."<a href='teamidx.php?Team=".$row['StartStreakOpposition']."'>".$row['StartStreakOpposition']."</td>";
                echo "<td align=center width=15%>"."<a href='gamestats.php?GameID=".$row['GameID']."'>".$row['StartRoundNo'].",".$row['StartSeason']."</td>";   
                echo "<td width=20%>"."<a href='teamidx.php?Team=".$row['EndStreakOpposition']."'>".$row['EndStreakOpposition']."</td>";
                echo "<td align=center width=15%>"."<a href='gamestats.php?GameID=".$row['GameID']."'>".$row['EndRoundNo'].",".$row['EndSeason']."</td>";       
             }
                echo "</tr>";

1 个答案:

答案 0 :(得分:1)

如何改进SQL查询的JOIN:

使用类似此链接http://www.codeproject.com/KB/database/Visual_SQL_Joins/Visual_SQL_JOINS_orig.jpg的内容来改进从逗号(,)到完整JOIN语句的联接,可以使用各种类型的联接:

SQL Join Types

请注意,使用图表 - 可以在不必声明TableA A的情况下进行联接,只需将其保留为Table A即可:

图表左上角的图像可以略微缩小为:

SELECT <...> FROM TABLEA LEFT JOIN TableB ON TableA.Key = TableB.Key 

在某些情况下可能有理由避免这样做,但这超出了本答案的范围(和我目前的知识)。

我的第一个/主要思想是为什么你不能将它分成多个较小的查询?我认为总体而言,这可能是改善这种情况的更好方法。

否则,选择您想要用于所需结果类型的JOIN类型,这样可以提高您的效率。

额外注意: 如果您还没有这样做,那么在列上设置索引对您来说将是一个很大的帮助。