私は Web アプリケーション (私の最初のアプリケーション) を作成しましたが、これは現在非常に遅く実行されており、一部の ajax '$post's は最大 40 秒かかります。
スペースを取りすぎるため、サイトのコード全体を提供することはできません。より高速に実行されるように、再因数分解する必要があります。
ウェブサイトが読み込まれると、約 7 つのスクリプトが同時に呼び出され、そのすべてが mysql データベースからデータを取得します。
Q1 php でデータベース キューを作成できますか (obj-c の fmdatabase.queue のように)。データベースへの接続が1つあり、呼び出しが次々に発生するように
データベースには約 100 万個のポイントがあります (これはパフォーマンスの問題につながるべきではないと言われました)。私が行った読書から、mysqliではなくmysqlを使用しています。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
";
これは、ユーザー データを取得するためのスクリプトの 1 つです。
<?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>';
}
}
?>