0

我已经编写了一个 Web 应用程序(我的第一个),它现在运行非常缓慢,一些 ajax '$post' 需要长达 40 秒。

我无法提供该网站的全部代码,因为它会占用太多空间。我需要重新考虑它,以便运行得更快。

当网站加载时,它同时执行大约 7 个调用 7 个脚本,所有这些都从 mysql 数据库中检索数据。

Q1 可以在 php 中创建数据库队列(如 obj-c 中的 fmdatabase.queue)吗?以便与数据库建立一个连接,并且呼叫一个接一个地发生

数据库中有大约 100 万个点(有人告诉我这不会导致性能问题)。我正在使用 mysql 而不是 mysqli 从我所做的阅读中我不会看到从更改为 mysqli 的速度有所提高。这是正确的吗?

这是我的数据库创建脚本:

$TripsData = "
    CREATE TABLE IF NOT EXISTS trips_data (
        id INT(10) NOT NULL AUTO_INCREMENT,
        userid INT(10),
        tripid INT,
        gmttimestamp BIGINT,
        latitude DOUBLE(24,20),
        longitude DOUBLE(24,20),
        altitude DOUBLE(24,20),
        accuracy DOUBLE(24,20),
        speed DOUBLE(24,20),
        species INT,
        killcount INT,
        PRIMARY KEY (id)
    ) TYPE =MYISAM
";

这是我获取用户数据的脚本之一

<?php
include ('header.php');
$userid=isloggedin();
if(isset($_POST['tripid'])){
$tripid=$_POST['tripid'];
$tripidbool=true;
}else{
  $tripidbool=false;
}
  if(isset($_POST['gamebook'])){$gamebookbool=true;}else{$gamebookbool=false;}
  if(isset($_POST['bounds'])){$bounds=$_POST['bounds'];$boundsboo=true;         }else{$boundsboo=false;}

if($tripidbool&&!$boundsboo){

    $totaldistanceinm=getTotalDistanceInM($userid, $tripid);        
echo '<tr><td> Distance Traveled </td><td> '.number_format(($totaldistanceinm/1000),2).' Km ('.number_format(($totaldistanceinm/1000)*(0.62137),2).' miles)</td></tr>';

     $query="SELECT MIN(gmttimestamp),
         MAX(gmttimestamp),
         AVG(speed), 
         SUM(killcount)
        from trips_data WHERE tripid=$tripid AND userid=$userid";   
     $result=mysql_query($query)or die(' '.mysql_error());
     $row=mysql_fetch_array($result);


echo'<tr><td>Start Time </td><td> '. date('H:i',$row[0]).'</td></tr>'; 

echo '<tr><td>Finish Time </td><td> '. date('H:i',$row[1]).'</td></tr>';

    $dur=  $row[1]-$row[0];
    if($dur!==0)
    {
        $hrs=($dur-($dur%3600))/3600;
        $mins=((($dur%3600)-($dur%60))/60);
        echo '<tr><td>Duration</td><td>'.$hrs.' Hours '.$mins.' Mins </td></tr> ';
    }

$totalk= $row[3];
echo '<tr><td>Total Kills</td><td>'.$totalk .'</td></tr>';
if($dur!==0)
    {
        echo '<tr><td>Kills/Hour</td><td>'.number_format(($totalk*3600/$dur),2).'</td></tr>';
    }

$total=$row[2];
echo '<tr><td>Avg Speed m/s</td><td>'.number_format($total,2).'</td></tr>';

    $query="SELECT SUM(gmttimestamp) FROM trips_data WHERE userid=$userid AND tripid=$tripid AND speed>0";
$res=mysql_query($query) or die(' '.mysql_error());
$row=mysql_fetch_array($res);
    $timeMoving=$row[0];

    $query="SELECT SUM(gmttimestamp) FROM trips_data WHERE userid=$userid AND tripid=$tripid AND speed=0";
$res=mysql_query($query) or die(' '.mysql_error());
$row=mysql_fetch_array($res);
    $timeNotMoving=$row[0];

    $toatltime=$timeMoving+$timeNotMoving;

    echo '<tr><td>%Time Moving/Stationary</td><td>'.number_format(($timeMoving/$toatltime)*100,2).'/'.number_format(($timeNotMoving/$toatltime)*100,2).'</td></tr>';
   // SELECT sum(trips_data.killcount), trips_data.species,masterspecies.species from trips_data join masterspecies WHERE tripid=335 AND userid=1 AND NOT killcount=0

$query="SELECT sum(trips_data.killcount) as kills,
        trips_data.species as species_id,
        masterspecies.species
            FROM trips_data
            JOIN masterspecies ON masterspecies.speciesidno = trips_data.species
            WHERE tripid =$tripid
            AND userid =$userid
            AND NOT killcount =0
            GROUP BY trips_data.species
            LIMIT 0 , 30";
$res=mysql_query($query)or die('87'.mysql_error());

while ($row=mysql_fetch_assoc($res))
    {
    $species=$row['species_id'];
    $SpeciesName="";

    if($species<0){
        $species*=-1;
        $SpeciesName="Seen ";

    }
    $SpeciesName.=$row['species'];
    $totalkills=$row['kills'];

    echo '<tr><td>Total '.$SpeciesName.'s</td><td> '.$totalkills.' </td></tr>';
}

 }
 elseif ($boundsboo&&!$tripidbool){
    $minlat=$_POST['minlat'];
    $maxlat=$_POST['maxlat'];
    $minlong=$_POST['minlon'];
    $maxlong=$_POST['maxlon'];

    $latlon=getCenterPointForBox($minlat, $minlong, $maxlat, $maxlong);
    $farmerId=  getFarmerIdForCenterPoint($latlon[0], $latlon[1]);

    if($farmerId)
    {
        $where=getWhereClauseForFarmersBoundaries(getBoundaryBoxesForFarmer($farmerId));
        $query="SELECT DISTINCT tripid FROM trips_data WHERE (".$where .") AND userid=$userid order by gmttimestamp desc";

    }  else {
       $query="SELECT DISTINCT tripid FROM trips_data WHERE userid=$userid 
        AND latitude BETWEEN $minlat AND $maxlat 
        AND longitude BETWEEN $minlong AND $maxlong "; 
    }


$res=mysql_query($query) or die(' '.$query.' '.mysql_error());
$totaldistanceinm=0;
    $number=mysql_num_rows($res);

    $dur=0;
for($a=0;$a<$number;$a++){
    $row=mysql_fetch_array($res);
    $ctripid=$row[0];

    $longprev=0;
    $latprev=0;
    $longdegstometers=10000000/90;
    $latdestom=0;


    $query_1="SELECT gmttimestamp,latitude, longitude FROM trips_data WHERE tripid=$ctripid AND userid=$userid ORDER BY gmttimestamp";
    $resa=mysql_query($query_1)or die(' '.mysql_error().$query_1);
    for($i=0;$i<mysql_num_rows($resa);$i++){
        $rowa=mysql_fetch_assoc($resa);
        $long=$rowa['longitude'];
        $lat=$rowa['latitude'];
        if($i==0){
                            $st=$rowa['gmttimestamp'];
            $longprev=$long;
            $latprev=$lat;
            $latdestom=cos($long)*$longdegstometers;
        }
        if($i>=1){
            $dlongM=($longprev-$long)*$longdegstometers;
            $dlatM=($latprev-$lat)*$latdestom;
            $distance=sqrt(($dlatM*$dlatM)+($dlongM*$dlongM));
            $totaldistanceinm+=$distance;
            $longprev=$long;
            $latprev=$lat;
                            $et=$rowa['gmttimestamp'];
        }

    }
            $dur+=($et-$st);


    }

echo '<tr><td>Total Distance Traveled </td><td> '.number_format(($totaldistanceinm/1000),2).
' Km ('.number_format(($totaldistanceinm/1000)*(0.62137),2).' miles)</td></tr>';
$hrs=($dur-($dur%3600))/3600;
$mins=((($dur%3600)-($dur%60))/60);
echo '<tr><td>Total Contact</td><td>'.$hrs.' Hours '.$mins.' Mins </td></tr> ';
echo '<tr><td>Total Number of Trips</td><td>'.$number.'  </td></tr> ';

if(!$where)$where=" latitude BETWEEN $minlat AND $maxlat 
        AND longitude BETWEEN $minlong AND $maxlong ";
$query="SELECT DISTINCT species FROM trips_data WHERE userid=$userid AND killcount>0 AND ".$where ." ";
$re_sp=  mysql_query($query) or die(''.  mysql_error());
    for($i=0;$i<  mysql_num_rows($re_sp);$i++)
    {

    $row=  mysql_fetch_array($re_sp);
    $species=$row[0];
            if($species==0){continue;}
    $SpeciesName="";

            $iiquery="SELECT SUM(killcount) FROM trips_data WHERE userid=$userid
      AND species=$species AND (".$where.") ";

    $iires=mysql_query($iiquery) or die('196 '.mysql_error());
    $iirow=mysql_fetch_array($iires);
    $totalkills=$iirow[0];

    if($species<0){
        $species*=-1;
        $SpeciesName="Seen ";

    }

    $iquery="SELECT species FROM masterspecies WHERE speciesidno=$species";
    $ires=mysql_query($iquery) or die('191 '.mysql_error());
    $irow=mysql_fetch_array($ires);
    $SpeciesName.=$irow[0];



    echo '<tr><td>Total '.$SpeciesName.'s</td><td> '.$totalkills.' </td></tr>';
}

}


if($gamebookbool){
$longprev=0;
$latprev=0;
$longdegstometers=10000000/90;
$latdestom=0;
$totaldistanceinm=0;

$query="SELECT MIN(gmttimestamp) from trips_data WHERE userid=$userid";
$result=mysql_query($query)or die(' '.mysql_error());
$row=mysql_fetch_array($result);
$st=$row[0];
$date=date("j:F",$st);
echo'<tr><td> First Trip </td><td> '.$date.'</td></tr>';

  //for all trips
    $allTripsForHunter=allTripsForHunter($userid);
    $yearDistance=0;
    for($i=0;$i<count($allTripsForHunter);$i++){
$query="SELECT latitude, longitude FROM trips_data WHERE tripid=$allTripsForHunter[$i] AND userid=$userid ORDER BY gmttimestamp";

 $res=mysql_query($query)or die(' '.mysql_error());
i f(mysql_num_rows($res)>0){    
    for($i=0;$i<mysql_num_rows($res);$i++){
     $row=mysql_fetch_assoc($res);
    $long=$row['longitude'];
    $lat=$row['latitude'];
    if($i==0){
        $longprev=$long;
        $latprev=$lat;
        $latdestom=cos($long)*$longdegstometers;
    }
    if($i>=1){
        $dlongM=($longprev-$long)*$longdegstometers;
        $dlatM=($latprev-$lat)*$latdestom;
        $distance=sqrt(($dlatM*$dlatM)+($dlongM*$dlongM));
        $totaldistanceinm+=$distance;
        $longprev=$long;
        $latprev=$lat;
        }
    }
            $yearDistance+=$totaldistanceinm;
        }
    }
echo '<tr><td> Distance Traveled </td><td> '.number_format(($yearDistance/1000),2).' Km ('.number_format(($yearDistance/1000)*(0.62137),2).' miles)</td></tr>';


    $yearDuration=0;

    for($i=0;$i<count($allTripsForHunter);$i++){
        $query="SELECT MIN(gmttimestamp) from trips_data WHERE tripid=$allTripsForHunter[$i] AND userid=$userid";   
        $result=mysql_query($query)or die(' '.mysql_error());
        $row=mysql_fetch_array($result);
        $st=$row[0];
        $starttime=date('H:i',$row[0]);

        $query="SELECT MAX(gmttimestamp) from trips_data WHERE tripid=$allTripsForHunter[$i] AND userid=$userid";
        $result=mysql_query($query)or die(' '.mysql_error());
        $row=mysql_fetch_array($result);
        $et=$row[0];
        $starttime=date('H:i',$row[0]); 

        $dur=($et-$st);
        $yearDuration+=$dur;
    }
    $days=($yearDuration-($yearDuration%86400))/86400;
    $hrs=(($yearDuration%86400)-($yearDuration%3600))/3600;
$mins=((($yearDuration%3600)-($yearDuration%60))/60);

    echo '<tr><td>Duration</td><td>'.$days.' Days '.$hrs.' Hours '.$mins.' Mins </td></tr> '; 

    $query="SELECT SUM(killcount) FROM trips_data WHERE userid=$userid";
$res=mysql_query($query) or die(' '.mysql_error());
$row=mysql_fetch_array($res);
$totalk=$row[0];
echo '<tr><td>Total Kills</td><td>'.$totalk.'</td></tr>';
echo '<tr><td>Kills/Hour</td><td>'.number_format(($totalk*3600/$yearDuration),2).'</td></tr>';

$query="SELECT DISTINCT species from trips_data WHERE userid=$userid AND NOT killcount=0";
$res=mysql_query($query)or die('87'.mysql_error());
$number=mysql_num_rows($res);

for($i=0;$i<$number;$i++){
    $row=mysql_fetch_assoc($res);

    $species=$row['species'];
    $SpeciesName="";

            $iiquery="SELECT SUM(killcount) FROM trips_data WHERE userid=$userid AND species=$species";
    $iires=mysql_query($iiquery) or die('107 '.mysql_error());
    $iirow=mysql_fetch_array($iires);
    $totalkills=$iirow[0];

            if($species<1){
        $species*=-1;
        $SpeciesName="Seen ";

    }

    $iquery="SELECT species FROM masterspecies WHERE speciesidno=$species";
    $ires=mysql_query($iquery) or die(' '.mysql_error());
    $irow=mysql_fetch_array($ires);
    $SpeciesName.=$irow[0];



    echo '<tr><td>Total '.$SpeciesName.'s</td><td> '.$totalkills.' </td></tr>';
}



}
?>
4

1 回答 1

4

我被告知不应该导致性能问题

恰恰相反。按原样使用时,它肯定会。必须正确设计他们的查询和数据库以避免性能问题。

您必须优化您的查询。

  1. 在控制台中运行它们
  2. 注意执行时间。
  3. 取最慢的一个并运行它,与单词有关EXPLAIN
  4. 研究输出并提出问题
  5. 重复直到完成。
于 2013-10-27T09:04:43.770 回答