douzou8074
douzou8074
2015-07-16 18:16
浏览 19

是什么让工匠的日常工作变得如此之慢?

I'm running a small loop of code to import some old records into a new database as follows:

$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
                    ]);
                }
            }
        }
    }
}

When I run the command to execute this loop, it fires beautifully. Hundreds of records each second. Then, it starts crawling to 1 record every 5 seconds before it shuts down about 600 records in with a memory error of some sort. I'm running Laravel and using the php artisan cli. What am I doing that causes all this memory to just fall to the floor, and what can I do to stop it?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

相关推荐