optimize-performance について
Railsアプリのパフォーマンス上のボトルネックを特定し、解消するためのスキルです。修正前の基準測定と失敗する回帰スペックを必須とし、データベース変更はEXPLAIN ANALYZEで確認します。
主な機能
- 01基準測定の必須化
- 02修正前の失敗する回帰スペック
- 03クエリ数アサーション
- 04DB変更時のEXPLAIN ANALYZE
- 057段階のレポート形式
活用シーン
- 01遅いRailsエンドポイントの調査
- 02過剰なデータベースクエリの削減
- 03クエリやインデックス変更の確認
- 04パフォーマンス最適化の記録
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ igmarin/ rails-agent-skills --skill optimize-performanceインストール後、Claude Code で/optimize-performanceを入力して読み込まれたか確認します。
同梱ファイルEXAMPLES.mdreferences/tools.md
詳細
パフォーマンスの最適化
Rails アプリケーションにおけるパフォーマンスのボトルネックを特定して修正します。
クイックリファレンス
| ツール | 用途 |
|---|---|
bullet | 開発環境での N+1 検出 |
rack-mini-profiler | エンドポイントの所要時間内訳 |
EXPLAIN ANALYZE | クエリプランの分析 |
HARD-GATE
NEVER optimize without a baseline measurement
ALWAYS write a regression spec before optimizing (query count assertion)
ALWAYS verify with EXPLAIN ANALYZE for database changes
NEVER write the report as "I applied includes(:author), then wrote a spec
to lock it in." The spec MUST be written and shown failing BEFORE the fix
appears in your output. Reordering for narrative flow fails the audit even
when the underlying work was correct.
出力スタイル
パフォーマンス最適化を完了する際、出力はこの 7 ステップのレポート順序に従わなければなりません。各ステップは出力に含める必要があります:
# Performance Optimization — [Description]
## 1. Baseline
<N> queries for <endpoint/action> — source: <log line / profiler output>
## 2. Bottleneck
<N+1 on association X / missing index on column Y> — tool: <bullet / rack-mini-profiler / EXPLAIN ANALYZE>
## 3. Regression Spec — RED
`make_database_queries(count: <N>)` at <path>:<line> — failure: expected <M>, got <N>
## 4. Fix
<path>:<line> — <includes(:association) / add_index / cache block>
## 5. Regression Spec — GREEN
Spec passes ✓ (<M> queries)
## 6. EXPLAIN ANALYZE
Before: Seq Scan, actual time=<X>ms → After: Index Scan, actual time=<Y>ms
## 7. Quantified Improvement
Queries: <N> → <M> | p95: <X>ms → <Y>ms
言語: 明示的に指定されない限り英語。
拡張リソース
あまり目立たない最適化
# Use counter_cache to avoid COUNT queries in loops
# In migration: add_column :users, :posts_count, :integer, default: 0
# In Post model: belongs_to :user, counter_cache: true
user.posts_count # no extra query
リグレッションスペック (クエリ数のアサーション)
RSpec.describe "Post index performance" do
it "loads posts with authors in a fixed number of queries" do
create_list(:post, 10, :with_author)
expect do
get posts_path
end.to make_database_queries(count: 2) # 1 posts query + 1 authors query
end
end
db-query-matchers gem またはカスタムの make_database_queries マッチャーを使用します。
インデックスまたはクエリの変更を適用した後、rails dbconsole (PostgreSQL) で直接実行します:
EXPLAIN ANALYZE
SELECT posts.*, users.name
FROM posts
INNER JOIN users ON users.id = posts.author_id
WHERE posts.published = true;
参照ファイルと外部リンク
これらのファイルは、その具体的な内容が必要な場合にのみ読み込んでください:
- references/tools.md — Bullet、rack-mini-profiler、または EXPLAIN ANALYZE の詳細な設定とインストール手順が必要な場合に使用します
外部参照:
統合
| スキル | チェーンするタイミング |
|---|---|
| write-tests | リグレッションスペック用 |
| review-migration | インデックスを追加する場合 |
コメント
Markdown 対応