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?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 求数据集和代码#有偿答复
    • ¥15 关于下拉菜单选项关联的问题
    • ¥15 如何修改pca中的feature函数
    • ¥20 java-OJ-健康体检
    • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
    • ¥15 使用phpstudy在云服务器上搭建个人网站
    • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
    • ¥15 vue3+express部署到nginx
    • ¥20 搭建pt1000三线制高精度测温电路
    • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况