All skills
asyrafhussin avatar

/laravel-database-optimization

@ac26821

Laravel database optimization patterns. Use when writing Eloquent queries, creating migrations, configuring caching, debugging slow queries, or optimizing database performance. Triggers on tasks involving N+1 queries, indexing, Redis caching, pagination, or database transactions.

Use this Skill: https://skilld.dev/gh/asyrafhussin/agent-skills/laravel-database-optimization

This session only. Nothing lands on disk.

rulesdata-cursor-iteration.md

≈584 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Use cursor() for Memory-Efficient Iteration

Impact: HIGH (Reduces memory from O(n) to O(1) — iterate millions of rows without exhausting RAM)

Loading an entire result set with all() or get() hydrates every row into an Eloquent model simultaneously, consuming memory proportional to the result size. cursor() uses a PHP generator to fetch and hydrate one row at a time, keeping memory usage constant regardless of how many rows are processed.

Incorrect

// ❌ Loads all 500,000 users into memory at once
$users = User::all();

$users->each(function (User $user): void {
    dispatch(new SendWeeklyDigest($user));
});

// Peak memory: ~500,000 Eloquent models × ~2KB each ≈ 1GB+ RAM

Problems:

  • Entire result set is hydrated into memory before iteration begins
  • Memory usage grows linearly with row count — easily causes out-of-memory errors
  • No benefit from loading all rows upfront when processing them sequentially

Correct

// ✅ Eloquent cursor — fetches one model at a time via PHP generator
User::where('subscribed', true)
    ->cursor()
    ->each(function (User $user): void {
        dispatch(new SendWeeklyDigest($user));
    });

// Peak memory: ~1 Eloquent model at a time, regardless of total rows

// ✅ Query Builder lazy() — same concept without Eloquent hydration overhead
DB::table('users')
    ->where('subscribed', true)
    ->lazy()
    ->each(function (object $user): void {
        dispatch(new SendWeeklyDigest($user));
    });

// ✅ lazy() with chunk size control
DB::table('users')
    ->lazyById(500, 'id')
    ->each(function (object $user): void {
        // Fetches 500 rows at a time internally, yields one at a time
        dispatch(new SendWeeklyDigest($user));
    });

Benefits:

  • Constant memory usage regardless of result set size — safe for millions of rows
  • cursor() returns a LazyCollection backed by a PHP generator
  • lazyById() combines the memory efficiency of generators with the consistency of ID-based chunking

Reference: Laravel Cursors

Source: SKILL.md on GitHub

1 warning16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    This skill is a comprehensive documentation set for optimizing database performance in Laravel 13 applications. It provides best practices for Eloquent queries, indexing, Redis caching, and safe migrations. No malicious patterns, prompt injections, or data exfiltration attempts were detected.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer6mo

    6/39 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at ac26821. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub last month.

Steadyupdated 7 months ago
metadata
{
  "author": "agent-skills",
  "version": "1.1.1"
}

README badge

README badge for asyrafhussin/agent-skills/laravel-database-optimization