Task:
Update the catalog of goods from the price without deleting the lines from the catalog.
The catalog itself has about 200-300 thousand lines.
Price of about 100 thousand.
1. Check and create new lines from the price list
2. Deactivating non-current lines
3. Update information on the line
Found the solution, but the script runs for a very long time.
Looking for script optimization.
newgoods = Product Catalog
goods_update = Price of the supplier of goods

<? set_time_limit(99999); include "../../php/connect.php"; include "../../config.php"; ?> <script> alert("Запускаю проверку наличия товаров из прайса в каталоге."); </script> <? $test_left_q = mysqli_query($conn, " SELECT goods_update.art AS art_update,goods_update.brand AS brand_update,goods_update.name AS name_update, goods_update.price AS price_update,goods_update.sklad AS sklad_update,goods_update.id AS id_update, newgoods.art AS art_sklad,newgoods.brand AS brand_sklad,newgoods.sklad AS sklad_sklad, IF(newgoods.art IS NULL, 'false', 'true') AS test_sklad FROM goods_update LEFT OUTER JOIN newgoods ON goods_update.art = newgoods.art AND goods_update.sklad = newgoods.sklad "); $count_goods = 0; $count_deactive = 0; $count_active = 0; $count_roz_nal = 0; $count_price = 0; ?> <script> alert("Добавляю в каталог новый товар"); </script> <? while ($test_left = mysqli_fetch_assoc($test_left_q)) { if ($test_left['test_sklad'] == "false") { $count_goods++; mysqli_query($conn, " INSERT INTO newgoods (name,brand,art,price,sklad,nal,price_date,roz_date,roz,active) SELECT name,brand,art,price,sklad,nal,CURDATE(),CURDATE(),CEIL(price*1.3),1 FROM goods_update WHERE id = ". $test_left['id_update'] ." "); } } ?> <script> alert("Запускаю проверку наличия товаров из каталога в прайсе."); </script> <? $test_right_q = mysqli_query($conn, " SELECT goods_update.art AS art_update,goods_update.brand AS brand_update,goods_update.price AS price_update, newgoods.art AS art_sklad,newgoods.brand AS brand_sklad,newgoods.id AS id_sklad,newgoods.nal AS nal_sklad, IF(goods_update.art IS NULL, 'false', 'true') AS test_update FROM goods_update RIGHT OUTER JOIN newgoods ON goods_update.art = newgoods.art AND goods_update.sklad = newgoods.sklad "); ?> <script> alert("Запускаю обновление информацию о товаре."); </script> <? while ($test_right = mysqli_fetch_assoc($test_right_q)) { if ($test_right['test_update'] == "false" && $test_right['nal_sklad'] == 0) { $count_deactive++; mysqli_query($conn, "UPDATE newgoods SET active = 0 WHERE id = " . $test_right['id_sklad']); } else if ($test_right['test_update'] == "true") { if ($test_right['nal_sklad'] > 0) { $count_roz_nal++; mysqli_query($conn, "UPDATE newgoods SET roz = CEIL(".$test_right['price_update']."*1.3), roz_date = CURDATE() WHERE id = " . $test_right['id_sklad']); } else { $count_active++; $count_price++; mysqli_query($conn, " UPDATE newgoods SET price = ".$test_right['price_update'].", price_date = CURDATE(), roz = CEIL(".$test_right['price_update']."*1.3), roz_date = CURDATE(), active = 1 WHERE id = " . $test_right['id_sklad'] ); } } } ?> <script> alert("Запускаю проверку актуальности каталога товаров."); </script> <? $test_roz_date_q = mysqli_query($conn, "SELECT id,roz_date,CURDATE() AS curdate FROM newgoods"); ?> <script> alert("Обновление активности товара."); </script> <? while ($test_roz_date = mysqli_fetch_assoc($test_roz_date_q)) { $test_date = date_diff(new DateTime(), new DateTime($test_roz_date['roz_date']))->days; if ($test_date > 6) { $count_deactive++; mysqli_query($conn, "UPDATE newgoods SET active = 0 WHERE id = " . $test_roz_date['id']); } } echo "Добавил " . $count_goods . " карточек. \n"; echo "Деактивировал " . $count_deactive . " карточек. \n"; echo "Активировал " . $count_active . " карточек. \n"; echo "Обновил " . $count_roz_nal . " розничных цен. \n"; echo "Обновил " . $count_price . " закупочных цен. \n"; ?> 
  • Well, the question is what? - Jean-Claude
  • Looking for script optimization - Sergey Dymov
  • Then to you on a freelancing site with such a task - korytoff
  • one
    A " line by line " in the title that means? Why do this exactly line by line? Do you have any restrictions on the time of blocking tables in the database for writing? This is me to the fact that the whole script can be replaced by about 3 sql query, which will do everything on the side of the database. For example, the product activity update could be performed as UPDATE newgoods SET active = 0 WHERE roz_date < curdate() - interval 6 day - Mike
  • one
    The first insert seems to be done as insert into newgoods ... select ваш первый запрос where newgoods.art IS NULL . Well, update can be turned into one join join, only with logic can we figure out when to update - Mike

0