php - 改善脚本执行时间

标签 php mysql

我想提高插入查询的执行时间,因为我的虚拟主机不会更改 max_execution_time。下面的脚本包含对挂钟执行时间的测试。这会导致 22 秒的执行时间,这太长了,因为 Web 主机将默认值保持为 30 秒。在下面的代码之后,还需要执行更多的代码。作为引用,tblPlayers 中约有 1000 名玩家。

$time_start = microtime(true);

$sqlTotalPoints = array();
$sqlCompetingPlayers = "SELECT Id FROM tblPlayers WHERE Game='" . $gamenumber. "' AND Joined='1'";
$resultCompetingPlayers = mysql_query($sqlCompetingPlayers);
while($row= mysql_fetch_array($resultCompetingPlayers))
{
    $PlayerId = $row['Id'];
    $sqlAlreadyHasPoints = "SELECT PlayerId FROM tblPlayerPoints WHERE PlayerId='" . $PlayerId . "'";
    $resultAlreadyHasPoints = mysql_query($sqlAlreadyHasPoints);
    $PointsRowFound = mysql_num_rows($resultAlreadyHasPoints);
    if($PointsRowFound < 1 ) {
        $sqlTotalPoints[] = '("' . $gamenumber . '", "FPS", "' . $PlayerId . '", "0")';
    }
}
echo 'INSERT INTO tblPlayerPoints (Game,GameNaam,PlayerId,PointsCollected) VALUES ' . implode(',',$sqlTotalPoints);

$time_end = microtime(true);
$execution_time = ($time_end - $time_start);
echo '<b>Total Execution Time:</b> '.$execution_time.' secs';

这会产生正确的 INSERT 语句,总执行时间为 22 秒。

我曾经在 while 循环内插入而不是内爆,但执行时间更糟。

最佳答案

这整段代码可以在单个 SQL 语句中完成。尝试使用 INSERT INTO ... SELECT 类型查询,并连接两个选择表,而不是循环第一个表的结果。即使没有表索引,您的数据大小也足够小,您不必担心时间限制。

关于php - 改善脚本执行时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31278070/

相关文章:

javascript - 如何将 react-native 与 php 一起使用,获取返回数据始终为 null

php - 如何在 Magento 中更改订单起始编号

MySQL 使用变量触发错误

MySQL 查询中的查询

mysql - 需要提高sql性能

php - 严格标准 : Only variables should be assigned by reference in ADONewConnection

java - 使用java进行curl操作

php - 将图像链接更新为自己的 URL

php - 一个数据库的 MySQL 4 兼容模式下的 MySQL 5?

php - 安装 php5-mcrypt 时出错