Ask the boss to take a look at the code that deletes redundant pictures on the website.

code to achieve the function is to query the uploaded directory under the picture in the database if not deleted, I wrote the code as follows, in the local test when the picture is relatively few can be uploaded to the server, because the server has more pictures, the database is also relatively large, the execution has been shown "waiting for www.abc.com response". I would like to ask the boss how to modify the code can be used in a large multi-picture database?

<?php
set_time_limit (0);
header("Content-type: text/html; charset=utf-8");
$_config = array();//
// ----------------------------  CONFIG DB  ----------------------------- //
$_config["dbhost"] = "127.0.0.1";
$_config["dbuser"] = "root";
$_config["dbpw"] = "root";
$_config["dbname"] = "dbname";
$_config["table"] = "pic";//
$_config["url"] = "pic_url";//

$link = mysqli_connect($_config["dbhost"], $_config["dbuser"], $_config["dbpw"]);
if (!$link){
    exit("");
}
mysqli_set_charset($link, "utf8");
mysqli_select_db($link, $_config["dbname"]);

//
$arr=glob("upload/pic/*/*.{jpg,png,gif,jpeg}",GLOB_BRACE);//*
if(count($arr)>0){
$j=count($arr);
$ndel=0;
for($i=0;$i<$j;$iPP){
$sql="select ".$_config["url"]." from ".$_config["table"]." where ".$_config["url"]." like "%$arr[$i]%"";
$myrun = mysqli_query($link, $sql);
echo ($sql."<br>");
if(!mysqli_num_rows($myrun)){
echo "$arr[$i]......<br><br>";
unlink ($arr[$i]);
$ndelPP;
}
$n=count($arr)-$ndel;
echo "<center><div style="margin-top:50px; line-height:25px; font-size:15px; color:-sharp3399FF;">";
if($ndel>0){echo "<font color="-sharpff0000"> ".$ndel." </font>";}
echo "<font color="-sharpff0000"> ".$n." </font></div></center>";
}

if(count($arr)==0){
echo ("");
}
mysqli_close($link);
?>
Php
Apr.14,2022

you have to do a database query for this picture, no wonder it is slow. I don't know whether you only execute this function once or need to execute it multiple times. If you execute this function only once, I think you can do some optimization like this:

  • get the fields that store the path of the picture in the mysql table to form a large array $picPathArr
  • The difference between $picPathArr and your $arr is the target to be deleted $picDelArr
  • iterate through $picDelArr and delete.
< hr >

if executed multiple times:

    The usage scenario of
  • should be taken into account when generating this kind of data, so to maintain the accuracy of the fields stored in the database, you can avoid using like and like .
  • if possible, there is a redundant copy of this data in the cache such as Redis to avoid querying the database; it is much faster to query the existence of images from the cache when you actually traverse $arr .
  • images that will be deleted are generated with tags such as _ del_ or placed in a specific directory to delete
  • later.
< hr >

generally speaking, there are many areas that can be optimized, but they still need to be adjusted according to the actual business. no, no, no.


Thank you for your answer. The purpose of this program is to delete the picture documents that do not exist in the database. I will try to optimize it according to your method.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e4d5c0-446b3.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e4d5c0-446b3.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?