Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 提高脚本执行时间_Php_Mysql - Fatal编程技术网

Php 提高脚本执行时间

Php 提高脚本执行时间,php,mysql,Php,Mysql,我想提高插入查询的执行时间,因为我的Web主机不会更改最大执行时间。下面的脚本包含对wallclock执行时间的测试。这导致22秒的执行时间太长,因为webhost将默认值保持在30秒。在下面的代码之后,需要执行更多的代码。作为参考,TBLPlayer中大约有1000名玩家 $time_start = microtime(true); $sqlTotalPoints = array(); $sqlCompetingPlayers = "SELECT Id FROM tblPlayers WHE

我想提高插入查询的执行时间,因为我的Web主机不会更改最大执行时间。下面的脚本包含对wallclock执行时间的测试。这导致22秒的执行时间太长,因为webhost将默认值保持在30秒。在下面的代码之后,需要执行更多的代码。作为参考,TBLPlayer中大约有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';
$time\u start=microtime(真);
$sqlTotalPoints=array();
$sqlCompetingPlayers=“从游戏=”的tblPlayers中选择Id”$游戏号码。“'AND join='1'”;
$resultCompetingPlayers=mysql\u查询($sqlCompetingPlayers);
while($row=mysql\u fetch\u数组($resultCompetingPlayers))
{
$PlayerId=$row['Id'];
$sqlAlreadyHasPoints=“从tblPlayerPoints中选择PlayerId,其中PlayerId=””$PlayerId。“”;
$resultAlreadyHasPoints=mysql\u查询($sqlAlreadyHasPoints);
$PointsRowFound=mysql\u num\u行($resultAlreadyHasPoints);
如果($PointsRowFound<1){
$sqlTotalPoints[]='(“.$gamenumber.”、“FPS”、“.$PlayerId.”、“0”);
}
}
echo“插入到tblPlayerPoints(游戏、游戏名称、玩家ID、收集的点数)值中”。内爆(“,”,$sqlTotalPoints);
$time\U end=微时间(真);
$execution\u time=($time\u end-$time\u start);
回显“总执行时间:”.$Execution_Time.“秒”;
这将生成正确的INSERT语句,总执行时间为22秒


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

最好的方法是使用下面的查询

INSERT INTO tblPlayerPoints
    (Gamenumber,GameNaam,PlayerId,PointsCollected)

SELECT col1,col2,col3,col4

FROM

tblPlayers //Your table from which you are retrieving data.

这整段代码可以在一条SQL语句中完成。尝试将
插入到。。。选择
type query,并连接两个选择表,而不是循环第一个选择表的结果。即使没有表索引,您的数据大小也足够小,不必担心时间限制。

正如其他人提到的,最好使用1条SQL语句

"INSERT INTO tblPlayerPoints(Gamenumber,GameNaam,PlayerId,PointsCollected) ".
"SELECT tp.Id, 'FPS', tpp.PlayerID, 0 " .
" FROM tblPlayers tp " .
" JOIN tblPlayerPoints tpp ON tp.gamenumber = tpp.gamenumber " .
" WHERE tp.game = '" .$gamenumber ."' AND tp.joined = '1'"
您应该向WHERE子句(或JOIN)中存在的列添加索引:


另外,我建议对gamenumber和joined使用整数值。整数的查找速度要快得多。

这里有一条SQL语句供您使用,这条语句与发布的另一条语句略有不同,因为它只会在不存在行的情况下添加新行

INSERT INTO `tblPlayerPoints` (`Game`,`GameNaam`,`PlayerId`,`PointsCollected`)
SELECT `tblPlayers`.`Game`, 'FPS', `tblPlayers`.`Id`, 0 
    FROM `tblPlayers`
    LEFT JOIN `tblPlayerPoints` ON `tblPlayers`.`Id` = `tblPlayerPoints`.`PlayerId` AND `tblPlayers`.`Game` = `tblPlayerPoints`.`Game` 
WHERE `tblPlayers`.`Game`='" . $gamenumber. "' AND `tblPlayers`.`Joined`='1' AND `tblPlayerPoints`.`PointesCollected` IS NULL

如果可以,你应该这样做。它们不再得到维护,而是在使用。学习替代,并考虑使用PDO。表的索引是否正确?因此您知道脚本需要多长时间才能实现,但您是否确切知道每个部分需要多长时间?我假设这是您的while循环,而不是您的
insert
语句,但情况可能并非如此。
“从tblPlayerPoints中选择PlayerId,其中Game=”$游戏号码。"'";
您正在从表中选择所有玩家ID,除了
$gamenumber
之外没有任何过滤器,如果我必须在黑暗中拍摄,在我建议检查索引和数据类型之前,这将使该查询更加健壮。查询
“从tblPlayerPoints WHERE Game=”中选择PlayerId$游戏号码。“'”
为每个玩家运行,但总是相同的查询!你应该在循环外只做一次,或者可能添加一个
player
参数。while循环是不相关的。while循环可能需要17年,并且不会导致30秒超时
INSERT INTO `tblPlayerPoints` (`Game`,`GameNaam`,`PlayerId`,`PointsCollected`)
SELECT `tblPlayers`.`Game`, 'FPS', `tblPlayers`.`Id`, 0 
    FROM `tblPlayers`
    LEFT JOIN `tblPlayerPoints` ON `tblPlayers`.`Id` = `tblPlayerPoints`.`PlayerId` AND `tblPlayers`.`Game` = `tblPlayerPoints`.`Game` 
WHERE `tblPlayers`.`Game`='" . $gamenumber. "' AND `tblPlayers`.`Joined`='1' AND `tblPlayerPoints`.`PointesCollected` IS NULL