[英]PHP Query Loading too slow. How to speed up?
Does anyone know a more efficient way to write the following. 有没有人知道一种更有效的方式来编写以下内容。 my load time over a minute right now.
我的加载时间现在超过一分钟。 This what I came up with after an even longer codeset.
这是我在更长的代码集之后提出的。
$max = $i + 25;
while ($i < $max)
{
$memberno = $memberid;
if($pulledid!=$memberid)
{
$pulledid = mysql_result($result99,$i,"id_member");
$sql_statement = "Select vinyl_fortrade.ImageID,vinyl_fortrade.MemberID from vinyl_fortrade where exists (Select ImageID from vinyl_wanted WHERE MemberID = $memberno AND vinyl_wanted.ImageID = vinyl_fortrade.ImageID) AND vinyl_fortrade.MemberID=$pulledid";
$result=mysql_query($sql_statement)or die(mysql_error());
$num=mysql_numrows($result);
if($num>0)
{
$sql_statement2 = "Select vinyl_wanted.ImageID, vinyl_wanted.MemberID from vinyl_wanted where exists (Select ImageID from vinyl_fortrade WHERE MemberID = $memberno AND vinyl_wanted.ImageID = vinyl_fortrade.ImageID) AND vinyl_wanted.MemberID=$pulledid";
$result2=mysql_query($sql_statement2)or die(mysql_error());
$num2=mysql_numrows($result2);
$membername = mysql_result($result99,$i,"smf_members.member_name");
$userid2 = mysql_result($result99,$i,"smf_members.id_member");
if ($num2 > 0)
{
$lastlogged = mysql_result($result99,$i,"smf_members.last_login");
echo '<td><a href="http://vinylmationconnection.com/forum/index.php?action=pm;sa=send; u='.$userid.'"style="color: #FFF">'.$membername.'</a><br>Last Logged In:<br>';
echo date('Y-m-d', $lastlogged);
echo ':</td>';
echo '<td>';
$n = 0;
$m = 0;
while ($n < $num)
{
$title=mysql_result($result,$n,"vinyl_fortrade.ImageID");
$sql_statementj = "Select * from vinyl_figures WHERE ImageID = '$title'";
$resultj=mysql_query($sql_statementj)or die(mysql_error());
$seriess=mysql_result($resultj,0,"vinyl_figures.Series");
echo '<img src="http://vinylmationconnection.com/images/p'.$seriess.'/'.$title.'.jpg">';
$n++;
if($n%4=="0")
echo "<br>";
}
echo '</td><td>';
while ($m < $num2)
{
$title=mysql_result($result2,$m,"vinyl_wanted.ImageID");
$sql_statementj = "Select Series from vinyl_figures WHERE ImageID = '$title'";
$resultj=mysql_query($sql_statementj)or die(mysql_error());
$seriess=mysql_result($resultj,0,"vinyl_figures.Series");
echo '<img src="http://vinylmationconnection.com/images/p'.$seriess.'/'.$title.'.jpg">';
$m++;
if($m%4=="0")
echo "<br>";
}
echo '</td> </tr>';
}
}
}
$i++;
}
Please let me know if ths code is self explanatory. 如果代码是自我解释的,请告诉我。
$sql_statementj = "Select * from vinyl_figures WHERE ImageID = '$title'";
$sql_statementj = "Select Series from vinyl_figures WHERE ImageID = '$title'";
in these 2 statements you seem to make a mysql request for the same file if I got your code right. 在这两个语句中,如果我的代码正确,你似乎会对同一个文件发出一个mysql请求。 make these requests outside the loops, this should do some work.
在循环之外发出这些请求,这应该做一些工作。
编写一个存储过程(可能是一个简单的查询/ View就足够了)来完成所有这些查询工作并监视数据库索引。
The WHERE EXISTS clause is almost certainly the culprit, and it should be refactored. WHERE EXISTS子句几乎肯定是罪魁祸首,它应该被重构。 General guidelines for slow queries...
慢查询的一般准则......
Except for the answeres that are alredy mentined I could suggest you to use mod_deflate to compress the page before sending it to the browser and in should have some effect. 除了回答的问题,我建议你在将页面发送到浏览器之前使用mod_deflate来压缩页面,并且应该有一些效果。 Althrough if you are on local environment that also may cause a slow execution, becouse of the load on the machine.
通过,如果您在本地环境中也可能导致执行缓慢,因为机器上的负载。
And as well I think that you could lose the first while by using LIMIT $i, 25
in the query I think that it will have the same effect. 而且我认为你可能会丢失第一个,而在查询中使用
LIMIT $i, 25
我认为它会产生相同的效果。 To when you fetch them all with a simple foreach and it is done and I would suggest you to push as much as possible in a single query that will increse the load a bit but it should decrease the loading time. 当你用一个简单的foreach获取所有这些并且它已完成时,我建议你尽可能多地推送一个查询,这会增加一点负载,但它应该减少加载时间。
I am not a 100% professional but am trying to be of help. 我不是100%的专业人士,但我正在努力寻求帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.