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.

rulesquery-eager-loading.md

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

Always Eager Load Known Relationships

Impact: CRITICAL (Reduces N+1 queries — 101 queries down to 2)

Accessing relationships inside loops without eager loading triggers a separate query for every iteration. A page listing 100 posts with their authors fires 101 queries (1 for posts + 100 for each author), degrading response time linearly with dataset size.

Incorrect

// ❌ N+1 problem — 101 queries for 100 posts
$posts = Post::all(); // 1 query

foreach ($posts as $post) {
    echo $post->author->name; // 1 query per post (100 queries)
    echo $post->tags->pluck('name')->join(', '); // 1 query per post (100 more)
}
// Total: 201 queries

Problems:

  • Each loop iteration fires a separate SQL query for every accessed relationship
  • Query count grows linearly with the number of records, causing severe slowdowns
  • Database connection overhead multiplied across hundreds of round-trips

Correct

// ✅ Eager load all known relationships — 3 queries total
$posts = Post::with(['author', 'tags'])->get(); // 3 queries

foreach ($posts as $post) {
    echo $post->author->name;       // No additional query
    echo $post->tags->pluck('name')->join(', '); // No additional query
}

// ✅ Constrained eager loading — load only what you need
$posts = Post::with([
    'author:id,name,avatar',
    'comments' => fn ($q) => $q->latest()->limit(5),
])->get();

Benefits:

  • Constant query count regardless of result set size (2-3 queries instead of 201)
  • Constrained eager loads reduce memory usage by loading only relevant related records
  • Predictable, measurable database load that scales with relationship count, not row count

Reference: Laravel Eager Loading

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