PhP, MySql - 优化代码

标签 php mysql optimization

我不是一个优秀的 php 程序员(我来自 C++)。我仅将 php 用于数据库输入。

我有一个包含以下内容的数据库:

UserId (an unique int)
AsyncPointsAverage (float)
AsyncPointsAverageRank (a position based on the value immediately above)
AsyncPointsRecentAverage (float an average for the last 5 tests only)
AsyncPointsRecentAverageRank (a position based on the value immediately above)

该表中大约有 1000-1500 个条目。每天上午和下午有 5 个人进行测试,影响他们的总体平均水平和近期平均水平。 (这是在别处更新的,这里就不展示了。) 计算完这5个人之后,就会影响所有1000-1500的排名,所以我写了下面的代码。它是最优的吗?

我最关心的是我正在执行大约 1000 次 MySql 更新。那很棒吗?我应该换一种方式吗? (也可以随意优化函数中的任何其他代码。正如我所说,我来自 C++ 背景,所以不太了解 php 的细微差别。)

// Sorts by array entry 1
function ReRankCompareAverage($a, $b)
{
    if($a[1] == $b[1]) return 0;
    else return ($a[1] > $b[1] ? 1 : -1);
}
// Sorts by array entry 2
function ReRankCompareAverageRecent($a, $b)
{
    if($a[2] == $b[2]) return 0;
    else return ($a[2] > $b[2] ? 1 : -1);
}

function ReRank($db)
{
    $i = 0, $j = 0;
    $usersARR = null;

    $stmt = $db->prepare("SELECT UserId, AsyncPointsAverage, AsyncPointsRecentAverage FROM studenttable");
    $stmt->execute();
    if($stmt && isset($stmt) && $stmt->rowCount() > 0)
    {
        $i = 0;
        while(($row = $stmt->fetch(PDO::FETCH_ASSOC)))
        {
            $usersARR[$i][0] = intval($row['UserId']);
            $usersARR[$i][1] = floatval($row['AsyncPointsAverage']);
            $usersARR[$i][2] = floatval($row['AsyncPointsRecentAverage']);
            $i++;
         }
    }
    $stmt->closeCursor(); // mysql_free_result equivalent

    // The first pass of $j == 3 does the ranking by Average, filling position $usersARR[][3] with that rank
    // The second pass of $j == 4 does the ranking by AverageRecent, filling position $usersARR[][4] with that rank
    for($j = 3, $j <= 4; $j++)
    {
        $iCompare = $j == 3 ? 1 : 2;

        usort($usersARR, $j == 3 ? "ReRankCompareAverage" : "ReRankCompareAverageLast");
        $count = count($usersARR);
        if($count > 0)
        {
            // Start it off, with the person with the highest average is rank 1
            $usersARR[$count - 1][$j] = 1; // Position $j is filled with the rank
            // Now loop starting from the second one down
            for($i = $count - 2, $rank = 1; $i >= 0; $i--)
            {
                // Only change the rank if the next one down is strictly lower than the one above, otherwise will share the same rank
                if($usersARR[$i][$iCompare] < $usersARR[$i+1][$iCompare]) $rank = $count - $i; // Otherwise keep the same rank, because they are equal
                $usersARR[$count - 1][$j] = $rank;
            }
        }
     }

     // Now $usersARR is filled with the correct rankings, and they are asscoiated with $UserId
    // Now we must put all of these rankings into the database
    $count = count($usersARR);
    for($i = 0; $i < $count; $i++)
    {
         $stmt = $db->prepare("UPDATE studenttable SET AsyncPointsAverageRank=:AsyncPointsAverageRank, AsyncPointsRecentAverageRank=:AsyncPointsRecentAverageRank "
                        . "WHERE UserId=:UserId");
         $stmt->execute(array(':AsyncPointsAverageRank' => $usersARR[$i][3],
                        ':AsyncPointsRecentAverageRank' => $usersARR[$i][4],
                        ':UserId' => $usersARR[$i][0]));
    }
}

最佳答案

您需要如何使用排名?也许您不需要存储排名?它们可能很容易计算:

SELECT COUNT(*) 
FROM studenttable 
WHERE AsyncPointsAverage > $currentUserVariableAsyncPoints

显示 TOP 10:

SELECT * FROM studenttable ORDER BY AsyncPointsAverage DESC LIMIT 0,10

等等

编辑:

要显示带有位置编号的完整排名,您可以在 PHP 中执行(您已经有了它 - 在获取行的内部循环中,只显示 $i++ 变量)。或者你可以尝试使用纯 SQL(我个人更喜欢它):

SET @rank=0; SELECT @rank := @rank +1 AS rank, UserId, AsyncPointsAverage
FROM studenttable
ORDER BY AsyncPointsAverage DESC 

关于PhP, MySql - 优化代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36180811/

相关文章:

python - 使用lambda python将文件上传到s3后用表单数据更新mysql

c - 防止在初始化之前使用函数,类似于 C 中的构造函数

PHP 脚本和 MYSQL 查询返回不同的(且不需要的)数据

php - mysql 中的排序应该与 excel 中的排序相同?

mysql 调整时间戳性能?

c - 使用相同的内存地址,但代码在程序内存中多占用 8 个字节?

c++ - 调用常量复杂度 O(1) 5 行函数会显着影响性能,为什么?

php - 原则 2 映射一对多/多对一

php - 在 <select> 中显示 MySQL 数据失败

php - 通过 AJAX 发送到 mySQL 查询的总选项的最佳方法