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.

rulesindex-foreign-keys.md

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

Index All Foreign Key Columns

Impact: CRITICAL (Prevents full table scans on JOINs and relationship queries)

Every Eloquent relationship query uses a WHERE clause on the foreign key column. Without an index, the database performs a full table scan for each relationship lookup. On tables with millions of rows, this turns millisecond queries into multi-second operations.

Incorrect

// ❌ Manual column without index — full table scan on every relationship query
Schema::create('posts', function (Blueprint $table): void {
    $table->id();
    $table->unsignedBigInteger('user_id'); // No index, no constraint
    $table->unsignedBigInteger('category_id'); // No index, no constraint
    $table->string('title');
    $table->text('body');
    $table->timestamps();
});

// Every query on this relationship triggers a full table scan:
// SELECT * FROM posts WHERE user_id = 42  — scans entire table
$user->posts;

Problems:

  • WHERE user_id = ? scans every row in the table without an index
  • JOIN operations between parent and child tables become prohibitively slow
  • CASCADE deletes on the parent table must scan the entire child table to find related rows

Correct

// ✅ Use foreignId() — creates column, index, and constraint automatically
Schema::create('posts', function (Blueprint $table): void {
    $table->id();
    $table->foreignId('user_id')->constrained()->cascadeOnDelete();
    $table->foreignId('category_id')->constrained()->cascadeOnDelete();
    $table->string('title');
    $table->text('body');
    $table->timestamps();
});
// foreignId() creates: unsignedBigInteger column + index + foreign key constraint

// ✅ For existing tables, add the index in a migration
Schema::table('posts', function (Blueprint $table): void {
    $table->index('user_id');
    $table->index('category_id');
});

// ✅ For polymorphic relationships, index both columns
Schema::create('comments', function (Blueprint $table): void {
    $table->id();
    $table->morphs('commentable'); // Creates commentable_type, commentable_id + composite index
    $table->text('body');
    $table->timestamps();
});

Benefits:

  • foreignId()->constrained() creates the column, index, and foreign key constraint in one call
  • Index-backed WHERE clauses use B-tree lookups instead of full table scans
  • morphs() automatically creates a composite index on both polymorphic columns

Reference: Laravel Foreign Key Constraints

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