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-avoid-unbounded.md

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

Never Use get() on Unbounded Queries

Impact: HIGH (Prevents memory exhaustion and timeouts from loading unpredictable result sets)

Calling get() on a query without a row limit loads every matching row into memory. A query that returns 100 rows today might return 10 million rows next year. Without explicit bounds, a single request can exhaust memory, saturate the database, and crash the application.

Incorrect

// ❌ Unbounded query — loads ALL active users into memory
$users = User::where('active', true)->get();

// Could be 100 rows in development, 5 million in production
// No limit, no pagination, no chunking — a ticking time bomb

// ❌ Unbounded query hidden inside a scope
public function scopeRecent(Builder $query): Builder
{
    return $query->where('created_at', '>', now()->subDays(30));
}

// Calling User::recent()->get() loads every user from the last 30 days
// with no upper bound

Problems:

  • Memory usage is unpredictable and grows with data volume over time
  • A query safe in development can cause out-of-memory crashes in production
  • No early warning — the problem only surfaces when the dataset grows large enough

Correct

// ✅ Paginate for display — always bounded
$users = User::where('active', true)->paginate(25);

// ✅ Chunk for batch processing — bounded memory
User::where('active', true)->chunkById(1000, function (Collection $users): void {
    foreach ($users as $user) {
        $user->notify(new AccountReminder());
    }
});

// ✅ Cursor for sequential processing — O(1) memory
User::where('active', true)->cursor()->each(function (User $user): void {
    $user->recalculateScore();
});

// ✅ Add an explicit limit as a safety net when get() is necessary
$topUsers = User::where('active', true)
    ->orderByDesc('score')
    ->limit(100)
    ->get();

// ✅ Use DB::whenQueryingForLongerThan() to detect runaway queries
DB::whenQueryingForLongerThan(5000, function (Connection $connection, QueryExecuted $event): void {
    Log::warning('Long-running queries detected', [
        'connection' => $connection->getName(),
    ]);
});

Benefits:

  • Predictable memory and time boundaries regardless of data growth
  • Explicit limits make resource consumption visible and reviewable in code review
  • Safety mechanisms like whenQueryingForLongerThan() catch issues before they escalate

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