DEV Community

codebysuraj
codebysuraj

Posted on

Laravel chunk() vs cursor() vs lazy() — Handle Large Data Without Crashing Your Server

 If you've ever tried to process thousands of rows in Laravel and got a memory error or server timeout — this article is for you.

I learned this the hard way when a large CSV export caused Apache timeout errors in production. Here's what I found out.

The Problem

// ❌ This will crash on large tables
$users = User::all();
foreach ($users as $user) {
    // process...
}
Enter fullscreen mode Exit fullscreen mode

all() loads every row into memory at once. On 100,000+ rows, your server will run out of memory.

1. chunk() — Process in Batches

User::chunk(500, function ($users) {
    foreach ($users as $user) {
        // processes 500 rows at a time
    }
});
Enter fullscreen mode Exit fullscreen mode

✅ Memory stays low

✅ Good for background jobs

⚠️ Don't modify/delete rows inside chunk() — it can skip records

⚠️ Runs multiple SQL queries

Use when: sending emails in batches, background processing

2. chunkById() — Safer Version of chunk()

User::chunkById(500, function ($users) {
    foreach ($users as $user) {
        // safe even when updating rows
    }
});
Enter fullscreen mode Exit fullscreen mode

✅ Safe to update/delete rows inside

✅ More reliable than chunk()

Use when: updating or deleting large amounts of records

3. cursor() — One Row at a Time

foreach (User::cursor() as $user) {
    // processes one row at a time
}
Enter fullscreen mode Exit fullscreen mode

✅ Most memory efficient

✅ Only one SQL query

❌ Can't use eager loading (with())

❌ Keeps DB connection open the whole time

Use when: read-only processing, CSV exports

4. lazy() — Best of Both Worlds

foreach (User::lazy() as $user) {
    // chunks behind the scenes, feels like cursor()
}
Enter fullscreen mode Exit fullscreen mode

✅ Memory efficient

✅ Supports eager loading with()

✅ Cleaner syntax

// With eager loading ✅
User::with('orders')->lazy()->each(function ($user) {
    // process
});
Enter fullscreen mode Exit fullscreen mode

Use when: you need cursor() but also need relationships loaded

Quick Comparison Table

Method Memory SQL Queries Supports with() Safe to modify?
all() ❌ High 1
chunk() ✅ Low Multiple ⚠️ No
chunkById() ✅ Low Multiple ✅ Yes
cursor() ✅ Lowest 1
lazy() ✅ Low Multiple

My Real-World Example

This fixed my Apache timeout issue on CSV export:

// ❌ Before — caused timeout
$users = User::all();

// ✅ After — works perfectly
foreach (User::cursor() as $user) {
    fputcsv($handle, [
        $user->name,
        $user->email,
        $user->created_at
    ]);
}
Enter fullscreen mode Exit fullscreen mode

Summary

  • chunk() — batch jobs, email sending
  • chunkById() — when modifying data in batches
  • cursor() — read-only, CSV exports, most memory efficient
  • lazy() — when you need cursor() + relationships

Want to dive deeper? Check out the full lesson on my free Laravel tutorial site 👇
https://php-laravel-tutorials.netlify.app/lesson-laravel-large-data

Top comments (0)