Я переношу старый проект PHP в новое приложение Laravel. Существует таблица из нескольких миллионов записей user_category_views, которую я планировал перенести частями. Я получаю старые записи с помощью mysqli и вставляю их с помощью Laravel DB::Statement. По какой-то причине после миллиона записей этот код вываливается с исключением:
Неустранимая ошибка: разрешенный размер памяти 268435456 байт исчерпан (пробовал выделить 73728 байт)
Что здесь переполняет память? Может быть, $result->free() работает не так, как я думаю?
$count = 2000000; // actual number will be received from count(*) stmt
$vendors = [561 => '618', 784 => '512' /* and so on */ ];
$step = 5000;
for( $i=0; $imysqli->query($q)) {
$stmt = "INSERT INTO vendor_views (`vendor_id`, `counter`, `created_at`) VALUES";
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
$vendor_id = null;
$id = $row['user_category_id'];
// Here I'm trying to prevent Laravel
// from throwing the exception if the entry
// is not found in $vendors array.
// This habit I've gained from js coding
try{
$vendor_id = $vendors[$id];
} catch (Exception $e) {
continue;
}
if(empty($vendor_id)) continue;
$counter = $row['counter'];
$created = $row['created'] ;
$stmt .= " ($vendor_id, $counter, '{$created}'),";
}
$result->free();
DB::statement( trim($stmt, ",") );
$stmt = null;
}
}
Подробнее здесь: https://stackoverflow.com/questions/587 ... -to-insert