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.

ruleseloquent-query-builder-hot-paths.md

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

Use Query Builder for Hot Paths

Impact: HIGH (30-50% faster on large result sets by skipping model hydration)

Eloquent models add overhead per row through model instantiation, attribute casting, and event dispatching. On hot API endpoints returning hundreds or thousands of rows, switching to the Query Builder eliminates this overhead entirely.

Incorrect

// ❌ Eloquent on a high-traffic endpoint returning thousands of rows
use App\Models\User;

// Each row instantiates a User model, runs casts, and fires events
$activeUsers = User::where('active', true)
    ->select('id', 'name', 'email')
    ->get();

// In a loop — model overhead multiplied across every iteration
foreach ($activeUsers as $user) {
    $result[] = [
        'id' => $user->id,
        'name' => $user->name,
    ];
}

Problems:

  • Each row triggers model instantiation, attribute casting, and accessor resolution
  • Model events (retrieved, etc.) fire for every single row
  • Memory usage scales with model weight, not just data size
  • On 10,000 rows, overhead can add 50-100ms compared to raw Query Builder

Correct

// ✅ Query Builder on hot paths — returns plain stdClass objects
use Illuminate\Support\Facades\DB;

$activeUsers = DB::table('users')
    ->where('active', true)
    ->select('id', 'name', 'email')
    ->get();

// stdClass objects — lightweight, no model overhead
foreach ($activeUsers as $user) {
    $result[] = [
        'id' => $user->id,
        'name' => $user->name,
    ];
}

Benefits:

  • Skips model instantiation, casts, accessors, and event dispatching
  • Returns lightweight stdClass objects with minimal memory footprint
  • 30-50% faster on large result sets (benchmarked on 5,000+ rows)
  • Keep Eloquent for CRUD operations where events, casts, and mutators add value

Reference: Laravel Query Builder

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