我正在运行一个小的代码循环,将一些旧记录导入到一个新的数据库中,如下所示:
$el = "<br>";
$ordinal = 0;
$groupBy = 100;
$query = 'SELECT count(id) AS totalCount FROM old_warrantySubmissions WHERE Cust_Email = ""';
$eachLimit = Db::select($query);
$eachLimit = ceil($eachLimit[0]->totalCount / $groupBy);
for ($eachOne = 601; $eachOne <= 620; $eachOne++) {
echo $el . "------------------------------" . $eachOne;
$qstate0 = 'SELECT * FROM old_warrantySubmissions WHERE Cust_Email = "" LIMIT ' . $eachOne * $groupBy . ',' . $groupBy;
$oldMachines = Db::select($qstate0);
foreach ($oldMachines as $machine) {
$serialNumber = $machine->Serial_Num;
$qState1 = 'SELECT * from new_machines WHERE serial = "' . $serialNumber . '" LIMIT 1';
$machineInfo = Db::select($qState1);
if (!$machineInfo) {
$ordinal++;
$passToInsert = "example" . str_pad((string)$ordinal, 8, "0", STR_PAD_LEFT);
$phoneToInsert = "(" . $machine->Cust_PhoneArea . ")" . $machine->Cust_PhonePre . "-" . $machine->Cust_PhoneSuff;
$emailToInsert = $passToInsert . "@example.com";
User::create([
'login' => $emailToInsert,
'email' => $emailToInsert,
'first_name' => $machine->Cust_First,
'last_name' => $machine->Cust_Last,
'phone' => $phoneToInsert,
'is_activated' => 1,
'password' => "password",
'password_confirmation' => "password",
'address1' => $machine->Cust_Address,
'address2' => "",
'city' => $machine->Cust_City,
'state' => $machine->Cust_State,
'zip' => $machine->Cust_Zip,
'country' => "USA",
'class' => 'Consumers',
'gender' => '',
'timezone' => ''
]);
$userInfo = Db::select('SELECT * FROM backend_users WHERE email = "' . $emailToInsert . '" LIMIT 1');
foreach ($userInfo as $user) {
$modelNumberToUse = 0;
$qState2 = 'SELECT id from new_products WHERE model = "' . $machine->Model_Num . '" LIMIT 1';
$modelInfo = Db::select($qState2);
if ($modelInfo) {
$modelNumberToUse = $modelInfo[0]->id;
}
if ($modelNumberToUse) {
echo $el . "(" . $serialNumber . ")";
Machines::create([
'retailer_id' => 0,
'consumer_id' => $user->id,
'location_id' => 0,
'product_id' => $modelNumberToUse,
'serial' => $serialNumber,
'notes' => $serialNumber,
'purchase_date' => $machine->Purchase_Date,
'off_site' => 0,
'financed' => $machine->Financed,
'educational' => 0
]);
}
}
}
}
}
当我运行命令来执行这个循环时,它完美地启动了。每秒数百条记录。然后,它开始每5秒爬行到1条记录,然后由于某种内存错误关闭大约600条记录。我正在运行Laravel和使用php工匠cli。我做了什么导致所有的记忆掉到地上,我能做些什么来阻止它?
工匠脚本中的内存泄漏可能是由laravel记录所有db查询而产生的。禁用日志记录:
DB::connection()->disableQueryLog();
在laracast的一个帖子上找到这个答案