Laravel DB Insert Error: Allowed Memory Size Exhausted

风格不统一 提交于 2019-12-19 03:11:35

问题


I'm running into an issue when trying to insert ~20K records into my DB. I notice that even though I'm echoing inside my foreach loop, I'm not getting anything outputted in the command line. Instead, I get an error after inserting ~9440 records relating to...

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 91 bytes) in /Users/me/Sites/Laravel/database/connection.php on line 293

Here is my code (tried using both Eloquent and Fluent):

<?php

class Process_Controller extends Base_Controller
{
    public function action_migrate()
    {
        $properties = DB::table('raw_properties')->get('id');
        $total = count($properties);

        foreach ($properties as $x => $p) {
            $r = RawProperty::find($p->id);
            $count = $x + 1;

            $prop_details = array(
                'column' => $r->field,
                // Total of 21 fields
            );

            DB::table('properties')->insert($prop_details);

            echo "Created #$count of $total\n";
        }
    }
}

回答1:


This error depicts that your PHP script has exhausted memory limit due to insufficient memory allocated for script.

You need to increase memory_limit using the ini_set function e.g ini_set('memory_limit','128M');




回答2:


The accepted answer is fixing the symptom rather then the problem. The problem is the Laravel query log (in memory) is eating all your RAM when you execute such a large # of queries. See the answer here: https://stackoverflow.com/a/18776710/221745

Or, in brief, turn off query logging via:

DB::disableQueryLog()

Before executing 20k queries



来源:https://stackoverflow.com/questions/12443321/laravel-db-insert-error-allowed-memory-size-exhausted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!