使用php和mysql更新多个子竞赛的排行榜排名

时间:2018-12-21 06:24:15

标签: php mysql ranking leaderboard dense-rank

我正在制作一个幻想体育移动应用程序,它包含比赛,并且在比赛中,可以有任意数量的比赛。我们将以一场比赛中进行的100个幻想比赛为例,我想更新所有这些比赛的排行榜排名。现在我正在使用php进行循环。首先,我将所有幻想比赛都放入特定的比赛中,然后循环浏览所有比赛并更新每个比赛的排名。它运行正常,但问题是它花费的时间太长。对于100场比赛,大约需要80-100秒之间的时间,这很多。 我想在一个MySQL查询中更新所有这些查询。帮助吗?

这是我用来更新排行榜的代码。预先感谢。

$stmt=$conn->Prepare("SELECT FC.fantasy_contest_id from fantasy_contests as FC 
where FC.cricket_contest_id = ?");
$stmt->bind_param('i', $cc_id);
$stmt->execute();
$stmt->bind_result($fantasy_contest_id);
$i=0;
$response["fantasy_contests"] = array();
    /* fetch values */
    while ($stmt->fetch()) {
        $output[$i]=array(
            'fantasy_contest_id' => $fantasy_contest_id
                        );
        array_push($response["fantasy_contests"], $output[$i]);
        $i++;    
    }

foreach ($response['fantasy_contests'] as $contest) {

$fan_id = $contest['fantasy_contest_id'];

$stmt=$conn->Prepare("SET @pk1 = NULL");
$stmt->execute();
$stmt->close();

$stmt=$conn->Prepare("SET @rn1 = ?");
$stmt->bind_param('i', $myval);
$stmt->execute();
$stmt->close();

$stmt=$conn->Prepare("SET @sal = NULL");
$stmt->execute();
$stmt->close();

$stmt=$conn->Prepare("SET @val = ?");
$stmt->bind_param('i', $myval);
$stmt->execute();
$stmt->close();

$stmt=$conn->Prepare("UPDATE fantasy_contest_teams dest, (SELECT  
fantasy_contest_teams_id, fantasy_contest_id,
total_points, rank FROM ( SELECT  fantasy_contest_teams_id, fantasy_contest_id,
total_points,
@rn1 := if(@pk1=fantasy_contest_id, if(@sal=total_points, @rn1, @rn1+@val),1) 
as rank,
@val := if(@pk1=fantasy_contest_id, if(@sal=total_points, @val+1, 1),1) as 
value,
@pk1 := fantasy_contest_id,
@sal := total_points FROM ( SELECT AA.fantasy_contest_teams_id, 
AA.fantasy_contest_id, BB.total_points
FROM fantasy_contest_teams as AA 
left join fantasy_teams as BB on AA.fantasy_team_id = BB.fantasy_team_id 
where AA.fantasy_contest_id = ?
ORDER BY fantasy_contest_id,total_points DESC) A) B) src 
SET dest.rank = src.rank 
WHERE dest.fantasy_contest_teams_id = src.fantasy_contest_teams_id;");
$stmt->bind_param('i', $fan_id);
$stmt->execute();
$stmt->close();
 }

0 个答案:

没有答案