laravel-database-optimization
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.
下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o laravel-database-optimization.zip https://jpskill.com/download/23223.zip && unzip -o laravel-database-optimization.zip && rm laravel-database-optimization.zip
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/23223.zip -OutFile "$d\laravel-database-optimization.zip"; Expand-Archive "$d\laravel-database-optimization.zip" -DestinationPath $d -Force; ri "$d\laravel-database-optimization.zip"
完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。
💾 手動でダウンロードしたい(コマンドが難しい人向け)
- 1. 下の青いボタンを押して
laravel-database-optimization.zipをダウンロード - 2. ZIPファイルをダブルクリックで解凍 →
laravel-database-optimizationフォルダができる - 3. そのフォルダを
C:\Users\あなたの名前\.claude\skills\(Win)または~/.claude/skills/(Mac)へ移動 - 4. Claude Code を再起動
⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。
🎯 このSkillでできること
下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。
📦 インストール方法 (3ステップ)
- 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
- 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
- 3. 展開してできたフォルダを、ホームフォルダの
.claude/skills/に置く- · macOS / Linux:
~/.claude/skills/ - · Windows:
%USERPROFILE%\.claude\skills\
- · macOS / Linux:
Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。
詳しい使い方ガイドを見る →- 最終更新
- 2026-05-18
- 取得日時
- 2026-05-18
- 同梱ファイル
- 2
📖 Claude が読む原文 SKILL.md(中身を展開)
この本文は AI(Claude)が読むための原文(英語または中国語)です。日本語訳は順次追加中。
Laravel Database Optimization
Comprehensive database optimization guide for Laravel 13 applications. Contains 33 rules across 9 categories for writing performant database queries, proper indexing, efficient caching, naming conventions, and debugging slow queries in Laravel 13.
Metadata
- Version: 1.1.0
- Framework: Laravel 13.x
- PHP: 8.3+
When to Apply
Reference these guidelines when:
- Writing Eloquent queries or using the query builder
- Diagnosing and fixing N+1 query problems
- Adding database indexes to migrations
- Implementing Redis or cache-based optimizations
- Paginating or processing large datasets
- Wrapping operations in database transactions
- Creating or modifying migrations for production databases
- Debugging slow queries with EXPLAIN or Laravel Debugbar
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Query Performance & N+1 | CRITICAL | query- |
| 2 | Indexing Strategies | CRITICAL | index- |
| 3 | Eloquent Optimization | HIGH | eloquent- |
| 4 | Caching with Redis | HIGH | cache- |
| 5 | Pagination & Large Datasets | HIGH | data- |
| 6 | Transactions & Locking | HIGH | lock- |
| 7 | Migrations | HIGH | migrate- |
| 8 | Query Debugging | MEDIUM | debug- |
| 9 | Naming & Structure | HIGH | naming- |
Quick Reference
1. Query Performance & N+1 (CRITICAL)
query-eager-loading- Use eager loading to eliminate N+1 queriesquery-prevent-lazy-loading- Prevent lazy loading in developmentquery-auto-eager-loading- Configure automatic eager loading on modelsquery-select-columns- Select only needed columns instead of SELECT *
2. Indexing Strategies (CRITICAL)
index-foreign-keys- Index all foreign key columnsindex-composite-indexes- Create composite indexes for multi-column queriesindex-covering-indexes- Use covering indexes for read-heavy queriesindex-full-text- Use full-text indexes for search functionality
3. Eloquent Optimization (HIGH)
eloquent-query-builder-hot-paths- Use query builder for performance-critical pathseloquent-with-count-aggregates- Use withCount instead of loading relations to counteloquent-subquery-selects- Use subquery selects to avoid extra querieseloquent-where-has-optimization- Optimize whereHas with whereIn subqueries
4. Caching with Redis (HIGH)
cache-remember- Use Cache::remember for expensive queriescache-invalidation- Invalidate cache on model changescache-tags- Use cache tags for group invalidationcache-ttl- Set appropriate TTL values for cached data
5. Pagination & Large Datasets (HIGH)
data-cursor-pagination- Use cursor pagination for large datasetsdata-chunk-by-id- Process large datasets with chunkByIddata-cursor-iteration- Use lazy cursors for memory-efficient iterationdata-avoid-unbounded- Never use unbounded queries on large tables
6. Transactions & Locking (HIGH)
lock-short-transactions- Keep transactions short and focusedlock-deadlock-retry- Implement deadlock retry logiclock-pessimistic-locking- Use pessimistic locking for critical updates
7. Migrations (HIGH)
migrate-zero-downtime- Write zero-downtime migrationsmigrate-concurrent-indexes- Create indexes concurrently in productionmigrate-safe-column-additions- Add columns safely without locking tables
8. Query Debugging (MEDIUM)
debug-explain-analyze- Use EXPLAIN ANALYZE to understand query plansdebug-laravel-debugbar- Use Laravel Debugbar to find query bottlenecksdebug-slow-query-log- Enable and monitor slow query logs
9. Naming & Structure (HIGH)
naming-tables- Table naming conventions (plural snake_case, pivot alphabetical)naming-columns- Column naming conventions (FKs, booleans, timestamps, polymorphic)naming-relationships- Relationship method naming (singular/plural matching)naming-migrations- Migration and index naming conventions
Essential Patterns
Prevent Lazy Loading in Development
<?php
namespace App\Providers;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function boot(): void
{
Model::preventLazyLoading(!app()->isProduction());
}
}
Cache Expensive Queries with Redis
<?php
use Illuminate\Support\Facades\Cache;
// Cache a query result for 1 hour (3600 seconds)
$popularPosts = Cache::remember('posts:popular', 3600, fn () =>
Post::query()
->withCount('comments')
->orderByDesc('comments_count')
->take(10)
->get()
);
Cursor Pagination for Large Datasets
<?php
// Cursor pagination — efficient for infinite scroll and large tables
$posts = Post::query()
->where('published_at', '<=', now())
->orderByDesc('published_at')
->cursorPaginate(15);
Aggregate Counts Without Loading Relations
<?php
// Instead of loading all posts just to count them
$users = User::withCount('posts')->get();
foreach ($users as $user) {
echo "{$user->name} has {$user->posts_count} posts";
}
Process Large Datasets with chunkById
<?php
// Memory-efficient processing of large tables
User::query()
->where('last_login_at', '<', now()->subYear())
->chunkById(1000, function ($users) {
foreach ($users as $user) {
$user->update(['status' => 'inactive']);
}
});
Short Database Transactions
<?php
use Illuminate\Support\Facades\DB;
// Keep transactions short and focused
DB::transaction(function () {
$order = Order::create([
'user_id' => auth()->id(),
'total' => $this->calculateTotal(),
]);
$order->items()->createMany($this->cartItems());
$order->user->decrement('credits', $order->total);
});
How to Use
Read individual rule files for detailed explanations and code examples:
rules/query-eager-loading.md
rules/index-composite-indexes.md
rules/cache-remember.md
rules/_sections.md
Each rule file contains:
- YAML frontmatter with metadata (title, impact, tags)
- Brief explanation of why it matters
- Bad Example with explanation
- Good Example with explanation
- Laravel 13 and PHP 8.3 specific context and references
References
Full Compiled Document
For the complete guide with all rules expanded: AGENTS.md
同梱ファイル
※ ZIPに含まれるファイル一覧。`SKILL.md` 本体に加え、参考資料・サンプル・スクリプトが入っている場合があります。
- 📄 SKILL.md (7,274 bytes)
- 📎 README.md (2,560 bytes)