Simple PHP Script Makes Heavy Server Load

917 views Asked by At

I've got a simple PHP script that, once ran, makes impossible for me to access any other page on the server.

The script is as simple as this:

for($league=11387; $league<=11407; $league++){
 for($i=1; $i<9; $i++){

    //gets the team object here from external resource
    $team = $HT->getYouthTeam($HT->getTeam($HT->getLeague($league)->getTeam($i)->getTeamId())->getYouthTeamId()); 

    if($team->getId() != 2286094){
        $youthTeams[] = $team;
    }

    set_time_limit(10);
 }
}

Obviously, I am supposed to get thousands of "teams" here (except one with the ID of 2286094), but once I run this script I cannot open any other page on the server until this is over and it takes lots of time until the script fetches the results into $youthTeams array.

My intent was to make a progress bar that would tell exactly (in %) where the script is at, but I can't since this script makes impossible for the server to display any other pages (you get any other page "loading" but it never loads because of this script being ran on the server).

Also, addition sub-question: once all of this data is fetched, would it be smart to insert it all into the mysql database in one single query?

I really wanna learn more on this and want to get this finished so please help me out on this one.

1

There are 1 answers

1
Carsten Massmann On

Maybe you can identify which one of your lookups eats the most time by checking on the times?

$t0=microtime(1);
$teamid=$HT->getLeague($league)->getTeam($i)->getTeamId();
echo "lookup teamid:  ".(($t1=microtime(1))-$t0)."<br>";
if (if($team->getId() != 2286094) {
  $youthteamid=$HT->getTeam($teamid)->getYouthTeamId();
  echo "lookup youthteamid:  ".(($t2=microtime(1))-$t1)."<br>";
  $youthteam = $HT->getYouthTeam($youthteamid);
  echo "lookup youthteam:  ".(($t3=microtime(1))-$t2)."<br>total time:  ".($t3-$t0)."<br>";
}