Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel DB Insert Error: Allowed Memory Size Exhausted

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";
        }
    }
}
like image 360
csm232s Avatar asked Sep 15 '12 23:09

csm232s


People also ask

How to fix Allowed memory size exhausted?

The memory_limit setting in your server's PHP configuration controls this. To check the current memory limit setting, go to Utilities > System > PHP Info. Find the memory_limit directive and note the local value column's value. Increase this setting until the error is resolved.


1 Answers

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

like image 174
Erik Avatar answered Oct 02 '22 18:10

Erik