[英]There has got to be a more efficient way to do this
基本上,我試圖將圖像表縮減為3500行未保存或未過濾的圖像。 未保存和未過濾的引用數據庫中的其他表。 用戶可以保存圖像以及將圖像標記為可以安全工作等。我想將所有圖像保留在保存表和過濾器表中。
最終,圖像表將被縮小為(3,500個未保存/未過濾的圖像)+(“ X”已保存)+(“ X”未過濾)。 保存/過濾器/圖像之間的鏈接是為images
表中的記錄賦予的auto_incremented ID。 因此,saves具有image_id字段,該字段獲取images
表中記錄的鍵。 與過濾器表相同
//connect to database
$connect = xxxxxxxxxxx;
//GET ALL IMAGES FROM `images` Table - NEWEST FIRST (`id` is auto_increment)
$sql = mysql_query("SELECT * FROM `images` ORDER BY `id` DESC") or die(mysql_error());
$x = 0;
while($row = mysql_fetch_array($sql)){
//GET CURRENT IMAGES ID AND URL
$id = $row['id'];
$file = $row['url'];
//SEE IF IMAGE IS IN THE saves Table
$saves = mysql_query("SELECT `id` FROM `saves` WHERE `img_id` = '".$id."'") or die(mysql_error());
if(mysql_num_rows($saves) == 0){$saved = FALSE;}
else {$saved = TRUE;}
//SEE IF IMAGE IS IN THE filters Table
$filter = mysql_query("SELECT `id` FROM `filter` WHERE `img_id` = '".$id."'") or die(mysql_error());
if(mysql_num_rows($filter) == 0){$filtered = FALSE;}
else {$filtered = TRUE;}
//If the image has not been saved or filtered then put it in a que for deletion
if(!$saved || !$filtered){
$IdQue[$x] = $id;
$FileQue[$x] = $file;
$x++;
}//END if
}//END while
//Process the delete que: Delete node from database, delete file on server. Keep 3,500 of the newest images.
for($i=3500; $i<$x; $i++){
mysql_query("DELETE FROM `images` WHERE id='".$IdQue[$i]."' LIMIT 1") or die("line 33".mysql_error());
if(file_exists($FileQue[$i])){
unlink($FileQue[$i]) or die("file Not deleted");
}//END if
}//END for
echo ($i-3500)." files deleted<br/>";
//terminate connection
mysql_close($connect);
這應該可以使您走到某個地方,盡管不是完整的代碼:
首先,將選擇的ID減少至2個查詢:
$missing_from_filters = SELECT id FROM images WHERE img_id NOT IN (SELECT img_id FROM filter)
$missing_from_saves = SELECT id FROM images WHERE img_id NOT IN (SELECT img_id FROM saves)
然后,獲取兩個數組中的id
$to_delete = array_intersect($missing_from_filters,$missing_from_saves);
運行一個查詢以將其全部刪除
$remove_query = 'DELETE FROM images WHERE img_id IN ( '.implode(',',$to_delete).') LIMIT 3500';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.