有没有人知道更有效的方式来编写以下内容。我的加载时间现在超过一分钟。这是我在更长的代码集之后想出来的。
$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++;
}
如果代码是自我解释的,请告诉我。
答案 0 :(得分:2)
$sql_statementj = "Select * from vinyl_figures WHERE ImageID = '$title'";
$sql_statementj = "Select Series from vinyl_figures WHERE ImageID = '$title'";
在这两个语句中,如果我的代码正确,你似乎会为同一个文件发出一个mysql请求。在循环之外提出这些请求,这应该做一些工作。
答案 1 :(得分:0)
编写一个存储过程(可能是一个简单的查询/ View就足够了)来完成所有这些查询工作并监视数据库索引。
答案 2 :(得分:0)
WHERE EXISTS子句几乎肯定是罪魁祸首,它应该被重构。慢查询的一般准则......
答案 3 :(得分:0)
除了回复的问题,我建议你在将页面发送到浏览器之前使用mod_deflate压缩页面,并且应该有一些效果。如果您在本地环境中也可能导致执行缓慢,那就是机器上的负载。
而且我认为你可以在查询中使用LIMIT $i, 25
而失去第一个我认为它会产生相同的效果。当你用一个简单的foreach获取所有内容并完成时我会建议你尽可能多地推送一个查询,这会增加一点负载,但它应该减少加载时间。
我不是100%的专业人士,但我正在努力寻求帮助。