我在使用键值对的关联数组上进行迭代。在循环内部有SQL Update,SQL Select和另一个Update。是否可以将两个或所有这些语句合二为一,以减少执行时间?简而言之:foreach正在通过给定点向玩家循环。它应该将这些点添加到点表中。然后它应该获取球员的总当前分数并将新分数(foreach的值)添加到该总分。然后,它应该更新球员的总分。
此代码可以正常工作并提供正确的结果,但是在拥有大量玩家时只需要很长时间才能运行。到目前为止,它为我提供了12秒的总执行时间来运行此脚本,只有800名玩家。
foreach($arrPoints as $key => $value)
{
$sqlUpdatePoints = "UPDATE tblPoints SET Points = '$value' WHERE Game='FPS' AND PlayerId ='$key' AND Gamenumber = 2";
$PointsBefore=0;$PointsAfter=0;
$sqlReadPoints = "SELECT TotalPointsBefore, TotalPointsAfter FROM tblplayer WHERE Id=$key";
$parameters=array('');
$resultReadPoints = dataQuery($sqlReadPoints,$parameters);
foreach($resultReadPoints AS $rowRP)
{
$PointsBefore= $rowRP['TotalPointsBefore'];
$PointsAfter= $rowRP['TotalPointsAfter'];
}
$NewTotalBefore= $PointsBefore + $value;
$NewTotalAfter= $PointsAfter+ $value;
$sqlUpdateNewPoints = "UPDATE tblplayer SET TotalPointsBefore='$NewTotalBefore', TotalPointsAfter='$NewTotalAfter' WHERE Id=$key";
}
答案 0 :(得分:0)
我解决了这个问题。我可以通过增加update语句本身内部的值来消除foreach。这大大减少了执行时间,因为不需要进行查找。最终执行不包括在下面的样本中,应该使用PDO作为@ArtisticPhoenix
的statetforeach($arrPoints as $key => $value)
{
$sqlUpdatePoints = "UPDATE tblPoints SET Points = '$value' WHERE Game='FPS' AND PlayerId ='$key' AND Gamenumber = 2";
$sqlUpdateNewPoints = "UPDATE tblplayer SET TotalPointsBefore=TotalPointsBefore+$value, TotalPointsAfter=TotalPointsAfter+$value WHERE Id=$key";
}