Laravel. Chunk Large Queries

When working with large datasets, chunking is the key: instead of loading everything in the memory at once, you load only a chunk of records. Here is a reliable manual chunking solution for Laravel.

Laravel. Chunk Large Queries

When you work with large queries, you may find yourself in position when meory consumption increases rapidly, which may cause your server to run out of memory. To circumvent it, the large queries are split into smaller chunks and then processed separately to keep the memory footprint down.

The Laravel Eloquent has a nice method, which is called chunk, and is supposed to work really nicely. However, it appears it has some kind of bug, which makes it inconsistent, and you will find it usually stops halfway. Here is the snippet, if you want to give it a try.


$currentPosition = 0;
Product::whereRaw('Updated  > Uploaded')->chunk(5, function($products) use ($arg1,$arg2) {
    foreach ($products as $product) {
        $currentPosition = ($currentPosition + 1);
        $this->productUpload($product);
    }
});

The final solution, involves a bit of manual intervention, but it works reliably, very readable, and does not use closures.


$query = Product::whereRaw('Updated  > Uploaded');

$totalToUpload = $query->count();
$perChunk = 5;
$numChunks = ceil($totalToUpload / $perChunk);
$currentPosition = 0;

for ($i = 0; $i < $numChunks; $i++) {
    $limitStart = $i * $perChunk;
    $limitEnd = $perChunk;
    $products = $query->skip($limitStart)->take($limitEnd)->get();
    foreach ($products as $product) {
        $currentPosition = ($currentPosition + 1);
        $this->productUpload($product);
    }
}

Happy coding!

Keep Reading

Explore more articles and insights

Ork: SSH Server Automation in Go

Ork: SSH Server Automation in Go

Read Article
Deno Deploy Killed My Start Page

Deno Deploy Killed My Start Page

Read Article
Back to Golang: Why I Switched From Static to Dynamic Again

Back to Golang: Why I Switched From Static to Dynamic Again

Read Article
View All Blog Posts