15/10/2026
Process Millions of Rows Without Running Out of Memory π€
Did you know? chunk() can silently skip records if you update the column youβre filtering on, but lazyById() avoids that and streams rows without loading them all into memory.
The Tip: lazyById() pages through a table using the primary key and returns a LazyCollection, so only one chunk sits in memory at a time. Because it paginates by ID rather than by offset, it stays correct even when your loop changes the rows.
Why It Matters: Memory usage stays flat no matter how big the table gets. It also avoids the skipped-row bug that chunk() can cause when the loop updates the filtered column, such as setting subscribed = true.
Pro Tip: Use lazy() instead when you donβt need ID-based pagination, such as for a simple read-only report. Use chunkById() when you need a callback with explicit chunk handling.
For more: Developer Mithu