Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Laravel's chunk to avoid running out of memory?

I am pulling around 100k records from temporary tables, doing some slight modification on the data, downloading photos, and then putting the fields I need to keep in a "master" table. This is quickly causing my app to crash as it runs out of memory.

I read the very brief docs on using chunk() with Laravel's eloquent ORM but do not know how to even begin to implement it in my class.

Here is what I am currently doing:

    public function fire()
{
    // Turn off query logging
    DB::connection()->disableQueryLog();

    $feeds = RetsFeed::where('active','=',1)->get();
    foreach ($feeds as $feed)
    {

        $class = "TempListing{$feed->board}";

        $listings = $class::orderBy('MatrixModifiedDT','desc')->get();

        $listings->each(function($listing) use ($feed) {
            ListingMigrator::migrateListing($listing,$feed);
            echo "Feed: $feed->board\r\n";
            echo "SubcondoName: $listing->SubCondoName\r\n";
            echo "Development: $listing->Development\r\n";
            echo "\r\n";
        });
    }

}

Each feed (or datasource) is dumped into a temp table in a different chore. That works fine. Then, I grab all of hte listings out of one table (which is around 30k on average) and run my ListingMigrator method.

Where do I put the chunk in this example? Would it replace the line:

$listings = $class::orderBy('MatrixModifiedDT','desc')->get();

I don't fully understand the closure in the eloquent docs. This is all they have to say about it and here's the code example from the Laravel site:

    User::chunk(200, function($users)
{
    foreach ($users as $user)
    {
        //
    }
});
like image 419
Chris Farrugia Avatar asked Jan 22 '15 06:01

Chris Farrugia


1 Answers

The chunk call should replace the get call - it is designed to work on the pre-get() query builder object.

$listings = $class::orderBy('MatrixModifiedDT','desc');

$listings->chunk(200, function($listings) use($feed) {
    $listings->each(function($listing) use ($feed) {
        ListingMigrator::migrateListing($listing,$feed);
        echo "Feed: $feed->board\r\n";
        echo "SubcondoName: $listing->SubCondoName\r\n";
        echo "Development: $listing->Development\r\n";
        echo "\r\n";
    });
});
like image 66
ceejayoz Avatar answered Oct 19 '22 05:10

ceejayoz