Удаляем свойство(property) [b_iblock_element_property] несуществующих элементов [b_iblock_element]
1 2 3 4 5 6 7 |
SELECT prop.IBLOCK_ELEMENT_ID FROM `b_iblock_element_property` as `prop` LEFT JOIN `b_iblock_element` AS `element` ON prop.IBLOCK_ELEMENT_ID = element.ID WHERE element.ID IS NULL GROUP BY IBLOCK_ELEMENT_ID limit 1000 |
Пример скрипта
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<?php $startTime = microtime(true); $pid = getmypid(); echo "ID процесса : ", $pid, "\n"; $file = fopen('pid.txt', 'w+'); $message = "pid: [$pid] time: " . date("d.m.Y H:i:s"); fwrite($file, $message); fclose($file); $DBHost = "localhost"; $DBLogin = "test"; $DBPassword = "test"; $DBName = "test"; mysql_connect($DBHost, $DBLogin, $DBPassword); mysql_select_db($DBName); mysql_set_charset('utf8'); $limit = 50000; $temp = mysql_query("SELECT prop.IBLOCK_ELEMENT_ID FROM `b_iblock_element_property` as `prop` LEFT JOIN `b_iblock_element` AS `element` ON prop.IBLOCK_ELEMENT_ID = element.ID WHERE element.ID IS NULL GROUP BY IBLOCK_ELEMENT_ID limit $limit"); $array = []; for ($i = 0; $i < $limit; $i++) { $id = mysql_fetch_object($temp)->IBLOCK_ELEMENT_ID; $array[$id] = $id; } $count = 0; foreach ($array as $key) { mysql_query("DELETE FROM `b_iblock_element_property` WHERE `IBLOCK_ELEMENT_ID` = $key"); $count++; } echo "Удалено $count записей\n"; $endTime = microtime(true); echo "Время выполнения скрипта: " . ($endTime - $startTime) . " Сек.\n"; //вывод результата |
Удаление из сопутствующих таблиц
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
<?php $startTime = microtime(true); $pid = getmypid(); echo "ID процесса : ", $pid, "\n"; $file = fopen('pid.txt', 'w+'); $message = "pid: [$pid] time: " . date("d.m.Y H:i:s"); fwrite($file, $message); fclose($file); $DBHost = "localhost"; $DBLogin = "test"; $DBPassword = "test"; $DBName = "test"; mysql_connect($DBHost, $DBLogin, $DBPassword); mysql_select_db($DBName); mysql_set_charset('utf8'); $limit = 20000; /** Удаляем b_iblock_element_property */ /** * проперти * @var [type] */ $temp = mysql_query("SELECT prop.IBLOCK_ELEMENT_ID FROM `b_iblock_element_property` as `prop` LEFT JOIN `b_iblock_element` AS `element` ON prop.IBLOCK_ELEMENT_ID = element.ID WHERE element.ID IS NULL GROUP BY IBLOCK_ELEMENT_ID limit $limit "); $count_prop = 0; for ($i = 0; $i < $limit; $i++) { $id_prop = mysql_fetch_object($temp)->IBLOCK_ELEMENT_ID; if ($id_prop) { mysql_query("DELETE FROM `b_iblock_element_property` WHERE `IBLOCK_ELEMENT_ID` = $id_prop"); $count_prop++; } } /** Удаляем b_search_content */ /** * content * @var [type] */ $temp2 = mysql_query("SELECT s.ID FROM `b_search_content` as s LEFT JOIN `b_iblock_element` as `e` ON s.ITEM_ID = e.ID WHERE e.ID IS NULL AND s.PARAM1 IN ('catalog','offers') limit $limit "); $count_search_content = 0; for ($i = 0; $i < $limit; $i++) { $id_search = mysql_fetch_object($temp2)->ID; if ($id_search) { mysql_query("DELETE FROM `b_search_content` WHERE `ID` = $id_search"); $count_search_content++; } } /** Удаляем b_catalog_price */ /** * b_catalog_price * @var [type] */ $temp3 = mysql_query("SELECT cp.ID FROM `b_catalog_price` as cp LEFT JOIN `b_iblock_element` as `e` ON cp.PRODUCT_ID = e.ID WHERE e.ID IS NULL limit $limit "); $count_catalog_price = 0; for ($i = 0; $i < $limit; $i++) { $id_cp = mysql_fetch_object($temp3)->ID; if ($id_cp) { mysql_query("DELETE FROM `b_catalog_price` WHERE `ID` = $id_cp"); $count_catalog_price++; } } /** Удаляем b_catalog_product */ /** * b_catalog_product * @var [type] */ $temp4 = mysql_query("SELECT cp.ID FROM `b_catalog_product` as cp LEFT JOIN `b_iblock_element` as `e` ON cp.ID = e.ID WHERE e.ID IS NULL limit $limit "); $count_catalog_product = 0; for ($i = 0; $i < $limit; $i++) { $id_cpp = mysql_fetch_object($temp4)->ID; if ($id_cpp) { mysql_query("DELETE FROM `b_catalog_product` WHERE `ID` = $id_cpp"); $count_catalog_product++; } } /** Удаляем b_search_content_title */ /** * b_search_content_title * @var [type] */ $temp5 = mysql_query("SELECT b.SEARCH_CONTENT_ID FROM `b_search_content_title` as b LEFT JOIN `b_iblock_element` as `e` ON b.SEARCH_CONTENT_ID = e.ID WHERE e.ID IS NULL limit $limit "); $count_catalog_content_title = 0; for ($i = 0; $i < $limit; $i++) { $id_ct = mysql_fetch_object($temp5)->SEARCH_CONTENT_ID; if ($id_ct) { mysql_query("DELETE FROM `b_search_content_title` WHERE `SEARCH_CONTENT_ID` = $id_ct"); $count_catalog_content_title++; } } /** Удаляем b_iblock_5_index */ /** * b_iblock_5_index * @var [type] */ $temp6 = mysql_query("SELECT b.ELEMENT_ID FROM `b_iblock_5_index` as b LEFT JOIN `b_iblock_element` as `e` ON b.ELEMENT_ID = e.ID WHERE e.ID IS NULL limit $limit "); $count_5_index = 0; for ($i = 0; $i < $limit; $i++) { $id_5_index = mysql_fetch_object($temp6)->ELEMENT_ID; if ($id_5_index) { mysql_query("DELETE FROM `b_iblock_5_index` WHERE `ELEMENT_ID` = $id_5_index"); $count_5_index++; } } /** Удаляем b_search_content_site */ /** * b_search_content_site * @var [type] */ $temp7 = mysql_query("SELECT b.SEARCH_CONTENT_ID FROM `b_search_content_site` as b LEFT JOIN `b_iblock_element` as `e` ON b.SEARCH_CONTENT_ID = e.ID WHERE e.ID IS NULL limit $limit "); $count_content_site = 0; for ($i = 0; $i < $limit; $i++) { $id_content_s = mysql_fetch_object($temp7)->SEARCH_CONTENT_ID; if ($id_content_s) { mysql_query("DELETE FROM `b_search_content_site` WHERE `SEARCH_CONTENT_ID` = $id_content_s"); $count_content_site++; } } /** Удаляем b_search_content_text */ /** * b_search_content_text * @var [type] */ $temp8 = mysql_query("SELECT b.SEARCH_CONTENT_ID FROM `b_search_content_text` as b LEFT JOIN `b_iblock_element` as `e` ON b.SEARCH_CONTENT_ID = e.ID WHERE e.ID IS NULL limit $limit "); $count_content_text = 0; for ($i = 0; $i < $limit; $i++) { $id_content_text = mysql_fetch_object($temp8)->SEARCH_CONTENT_ID; if ($id_content_text) { mysql_query("DELETE FROM `b_search_content_text` WHERE `SEARCH_CONTENT_ID` = $id_content_text"); $count_content_text++; } } echo "Удалено $count_prop property\n"; echo "Удалено $count_search_content content\n"; echo "Удалено $count_catalog_price price\n"; echo "Удалено $count_catalog_product product\n"; echo "Удалено $count_catalog_content_title title\n"; echo "Удалено $count_5_index iblock 5\n"; echo "Удалено $count_content_site content_site\n"; echo "Удалено $count_content_text content_text\n"; $endTime = microtime(true); echo "Время выполнения скрипта: " . ($endTime - $startTime) . " Сек.\n"; //вывод результата |