惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

有赞技术团队
有赞技术团队
B
Blog
IT之家
IT之家
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
人人都是产品经理
人人都是产品经理
博客园 - 聂微东
量子位
博客园 - 叶小钗
T
Tailwind CSS Blog
小众软件
小众软件
WordPress大学
WordPress大学
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - Franky
雷峰网
雷峰网
博客园 - 三生石上(FineUI控件)
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Blog — PlanetScale
Blog — PlanetScale
V
V2EX
博客园_首页
I
InfoQ
B
Blog RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Laravel chunk() vs cursor() vs lazy() — Handle Large Data...
codebysuraj · 2026-05-29 · via DEV Community

codebysuraj

 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...
}

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
    }
});

✅ 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
    }
});

✅ 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
}

✅ 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()
}

✅ Memory efficient

✅ Supports eager loading with()

✅ Cleaner syntax

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

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
    ]);
}

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