我从以下代码得到的速度非常慢。我一直试图解决它几个小时,但没有结果。我只包含了相关的代码。
编辑:
我正在尝试从数据库中选择一个id,然后使用该id来获取与之关联的所有图像。然后我将那组图像缩小到一个。一旦我拥有该图像,我将通过外部文件调整大小。
我已经尝试删除代码的各个部分来识别问题,似乎减速是由第二个查询引起的,但我不确定为什么。谢谢你的帮助。
$getworks = mysql_query ("SELECT a_id from artists where display_works = '1' and active = '1' order by project_year desc, fullname desc");
while ($getworksrow = mysql_fetch_assoc($getworks)){
$totalimages=1;
$addstyle = "";
$id = $getworksrow["a_id"];
$getimages = mysql_query ("SELECT a_id, image_id from images where a_id = '". $id ."' order by position asc LIMIT 1");
$getimagesrow = mysql_fetch_assoc($getimages);
foreach ($getimagesrow as $getimagesrows){
extract($getimagesrow);
if($totalimages > 1){ $addstyle = 'style="display:none;"'; }
else {
$myimagename = "http://artist.com/$a_id/images/$image_id" . "_large.jpg";
list($width, $height, $type, $attr) = getimagesize("$myimagename");
$myimagename = "http://artist.com/artists/resize.php/$a_id/images/$image_id" . "_large.jpg?resize(157x2000)";
if($getworksrows["layout"] == "vert"){$pl = "_vertical";}else if($getworksrows["layout"] == "website"){$pl = "-s";}else if($getworksrows["layout"] == "video"){$pl = "_video";}else{$pl = "_horizontal";}
echo "<li class='thumbnail_container' $addstyle> <a class='thumbnail' href=\"../works$pl.php?a_id=" . $getworksrows["a_id"] . "\"><span><img src=\"$myimagename\" /></span>\n</a></li>\n";
}
$totalimages++;
}
}
答案 0 :(得分:2)
执行这样的查询是一个很大的性能开销,特别是当父查询有大数量时。记录。
您应该将join artist表与images表一起使用,并通过单个查询获取所有数据。
后来制作每位艺术家和图像的2D数组。并根据2D数组循环显示数据
以下是您应该使用的联接查询:
SELECT * from artists as art
left join images as img on art.a_id=img.a_id
where display_works = '1' and active = '1'
order by project_year desc, fullname desc
In make数据阵列:
while ($getworksrow = mysql_fetch_object($getworks)){
$data['a_id']['img_id']=$getworksrow->image; //Make 2D array
........
........
}
循环并显示数据:
foreach($data as $id=>$images)
{
foreach($images as $val){
// Do your stuff for displaying data
}
}
所以请做必要的修改。