Apply SQL optimization patterns including index usage, query rewriting, JOIN optimization, and window functions. Use when improving query performance or analyzing slow queries. This skill provides comprehensive SQL optimization patterns and best practices: - N+1 query elimination - Index optimization strategies - JOIN vs subquery performance - Window functions for complex aggregations - Query execution plan analysis Triggers: "optimize SQL", "slow query", "improve performance", "SQL最適化", "クエリ改善", "パフォーマンス向上"
Apply SQL optimization patterns including index usage, query rewriting, JOIN optimization, and window functions. Use when improving query performance or analyzing slow queries.
/plugin marketplace add takemi-ohama/ai-agent-marketplace/plugin install ndf@ai-agent-marketplaceThis skill inherits all available tools. When active, it can use any tool Claude has access to.
examples.mdreference.mdこのSkillは、data-analystエージェントがSQLクエリのパフォーマンスを改善する際に使用します。実績のある最適化パターンとベストプラクティスを提供し、遅いクエリを高速化します。
以下のキーワードを含むユーザーリクエストで自動起動されます:
問題: ループ内で繰り返しSELECT文を実行 解決: JOINまたはサブクエリで1回のクエリに統合
問題: WHERE句の列にインデックスがない 解決: 適切なインデックスを作成
問題: 不要な大規模テーブルのJOIN 解決: 必要な列のみ取得、結合順序の最適化
問題: 複雑なサブクエリの入れ子 解決: ROW_NUMBER(), RANK()等のウィンドウ関数を使用
問題: 不要なDISTINCT使用 解決: GROUP BYまたは適切なJOINで代替
問題: サブクエリでINを使用 解決: EXISTSに変更(多くの場合高速)
問題: 全件取得後にアプリ側でフィルタ 解決: SQLでLIMIT/OFFSETを使用
問題: WHERE句で関数を列に適用 解決: 計算済み列を作成してインデックス
詳細な最適化パターンとコード例は、以下のファイルを参照してください:
reference.md: 各パターンの詳細説明examples.md: Before/Afterの実例Before:
-- ループで実行(N+1クエリ)
SELECT * FROM users WHERE id = ?; -- N回実行
After:
-- 1回のクエリで取得
SELECT u.*, o.order_count
FROM users u
LEFT JOIN (
SELECT user_id, COUNT(*) as order_count
FROM orders
GROUP BY user_id
) o ON u.id = o.user_id;
改善: N+1回 → 1回のクエリ、大幅な高速化
Before:
SELECT * FROM orders
WHERE created_at > '2023-01-01'
AND status = 'completed';
-- インデックスなし、フルスキャン
After:
-- インデックス作成
CREATE INDEX idx_orders_status_created ON orders(status, created_at);
-- 同じクエリがインデックスを使用
SELECT * FROM orders
WHERE status = 'completed'
AND created_at > '2023-01-01';
-- ORDER BY の順序を逆にしてインデックス効率化
改善: フルスキャン → インデックススキャン、10倍以上高速化
Before:
-- サブクエリの入れ子
SELECT u.name,
(SELECT COUNT(*) FROM orders o WHERE o.user_id = u.id) as order_count,
(SELECT SUM(total) FROM orders o WHERE o.user_id = u.id) as order_total
FROM users u;
-- usersの各行でordersを2回スキャン
After:
-- ウィンドウ関数で1回のスキャン
SELECT u.name,
COUNT(o.id) OVER (PARTITION BY u.id) as order_count,
SUM(o.total) OVER (PARTITION BY u.id) as order_total
FROM users u
LEFT JOIN orders o ON u.id = o.user_id;
-- 1回のJOINで完結
改善: 2N回スキャン → 1回のJOIN、大幅な高速化
✅ EXPLAINで実行計画を確認: 最適化前後で必ず確認 ✅ インデックスは選択的に作成: WHERE/JOIN/ORDER BYで使用される列 ✅ 必要な列のみSELECT: SELECT *は避ける ✅ 早期フィルタリング: WHERE句を最初に適用 ✅ 統計情報を更新: ANALYZE TABLEで最新状態に
❌ 不要なDISTINCT: データ構造を見直す ❌ 関数をWHERE句の列に適用: インデックスが使用されない ❌ 過剰なJOIN: 必要最小限に絞る ❌ サブクエリの多用: JOINやウィンドウ関数で代替 ❌ インデックスの作り過ぎ: INSERT/UPDATEが遅くなる
実行時間測定:
-- BigQueryの場合
SELECT CURRENT_TIMESTAMP();
-- クエリ実行
SELECT CURRENT_TIMESTAMP();
スキャンバイト数確認:
実行計画比較:
EXPLAIN SELECT ...;
A: 以下を確認:
A: 以下の順で確認:
A: インデックスの見直しが必要:
このSKILL.mdはメインドキュメント(約200行)です。詳細な最適化パターンとコード例は別ファイル(reference.md, examples.md)に分離されています。
This skill should be used when the user asks to "create a slash command", "add a command", "write a custom command", "define command arguments", "use command frontmatter", "organize commands", "create command with file references", "interactive command", "use AskUserQuestion in command", or needs guidance on slash command structure, YAML frontmatter fields, dynamic arguments, bash execution in commands, user interaction patterns, or command development best practices for Claude Code.
This skill should be used when the user asks to "create an agent", "add an agent", "write a subagent", "agent frontmatter", "when to use description", "agent examples", "agent tools", "agent colors", "autonomous agent", or needs guidance on agent structure, system prompts, triggering conditions, or agent development best practices for Claude Code plugins.
This skill should be used when the user asks to "create a hook", "add a PreToolUse/PostToolUse/Stop hook", "validate tool use", "implement prompt-based hooks", "use ${CLAUDE_PLUGIN_ROOT}", "set up event-driven automation", "block dangerous commands", or mentions hook events (PreToolUse, PostToolUse, Stop, SubagentStop, SessionStart, SessionEnd, UserPromptSubmit, PreCompact, Notification). Provides comprehensive guidance for creating and implementing Claude Code plugin hooks with focus on advanced prompt-based hooks API.