sql-pro
できること
AIが、複雑なSQLを書くこと、データベースの構造を計画すること、遅いクエリを速くすることを案内します。データベースを扱う開発者や分析担当者向けです。
使う場面
- ✓データベースのクエリが遅く、原因を知りたいとき。
- ✓結合、ウィンドウ関数、再帰クエリを書く手助けが必要なとき。
- ✓新しいデータベースの構造や、テーブル構成の変更を計画するとき。
スキルのリンク
https://github.com/Jeffallan/claude-skills/blob/main/skills/sql-pro/SKILL.mdコピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。
使い方
いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。
貼り付け用メッセージ
Claude Code、Codex、Cursorの場合:プロジェクトのフォルダで実行するとスキルがインストールされます。
SKILL.mdをダウンロードして、エージェントのスキル用フォルダに入れてください。
AIへの頼み方の例
“この SQLクエリはなぜ10秒もかかるのですか。どうすれば速くなりますか。”
毎週5つの新しいスキル。1分で読めます。
仕組み
AIはテーブルとインデックスを確認し、クエリを書き直し、クエリプランを見て、テーブル全体を読む遅い処理を避けられたかを確かめます。
技術的な内容(元のSKILL.md)
作者による元のテキスト(英語)をGitHubからそのまま表示しています。
SQL Pro
Core Workflow
- Schema Analysis - Review database structure, indexes, query patterns, performance bottlenecks
- Design - Create set-based operations using CTEs, window functions, appropriate joins
- Optimize - Analyze execution plans, implement covering indexes, eliminate table scans
- Verify - Run
EXPLAIN ANALYZEand confirm no sequential scans on large tables; if query does not meet sub-100ms target, iterate on index selection or query rewrite before proceeding - Document - Provide query explanations, index rationale, performance metrics
Reference Guide
Load detailed guidance based on context:
| Topic | Reference | Load When |
|---|---|---|
| Query Patterns | references/query-patterns.md |
JOINs, CTEs, subqueries, recursive queries |
| Window Functions | references/window-functions.md |
ROW_NUMBER, RANK, LAG/LEAD, analytics |
| Optimization | references/optimization.md |
EXPLAIN plans, indexes, statistics, tuning |
| Database Design | references/database-design.md |
Normalization, keys, constraints, schemas |
| Dialect Differences | references/dialect-differences.md |
PostgreSQL vs MySQL vs SQL Server specifics |
Quick-Reference Examples
CTE Pattern
-- Isolate expensive subquery logic for reuse and readability
WITH ranked_orders AS (
SELECT
customer_id,
order_id,
total_amount,
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date DESC) AS rn
FROM orders
WHERE status = 'completed' -- filter early, before the join
)
SELECT customer_id, order_id, total_amount
FROM ranked_orders
WHERE rn = 1; -- latest completed order per customer
Window Function Pattern
-- Running total and rank within partition — no self-join required
SELECT
department_id,
employee_id,
salary,
SUM(salary) OVER (PARTITION BY department_id ORDER BY hire_date) AS running_payroll,
RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS salary_rank
FROM employees;
EXPLAIN ANALYZE Interpretation
-- PostgreSQL: always use ANALYZE to see actual row counts vs. estimates
EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)
SELECT *
FROM orders o
JOIN customers c ON c.id = o.customer_id
WHERE o.created_at > NOW() - INTERVAL '30 days';
Key things to check in the output:
- Seq Scan on large table → add or fix an index
- actual rows ≫ estimated rows → run
ANALYZE <table>to refresh statistics - Buffers: shared hit vs read → high
readcount signals missing cache / index
Before / After Optimization Example
-- BEFORE: correlated subquery, one execution per row (slow)
SELECT order_id,
(SELECT SUM(quantity) FROM order_items oi WHERE oi.order_id = o.id) AS item_count
FROM orders o;
-- AFTER: single aggregation join (fast)
SELECT o.order_id, COALESCE(agg.item_count, 0) AS item_count
FROM orders o
LEFT JOIN (
SELECT order_id, SUM(quantity) AS item_count
FROM order_items
GROUP BY order_id
) agg ON agg.order_id = o.id;
-- Supporting covering index (includes all columns touched by the query)
CREATE INDEX idx_order_items_order_qty
ON order_items (order_id)
INCLUDE (quantity);
Constraints
MUST DO
- Analyze execution plans before recommending optimizations
- Use set-based operations over row-by-row processing
- Apply filtering early in query execution (before joins where possible)
- Use EXISTS over COUNT for existence checks
- Handle NULLs explicitly in comparisons and aggregations
- Create covering indexes for frequent queries
- Test with production-scale data volumes
MUST NOT DO
- Use SELECT * in production queries
- Use cursors when set-based operations work
- Ignore platform-specific optimizations when targeting a specific dialect
- Implement solutions without considering data volume and cardinality
Output Templates
When implementing SQL solutions, provide:
- Optimized query with inline comments
- Required indexes with rationale
- Execution plan analysis
- Performance metrics (before/after)
- Platform-specific notes if applicable
Maintained by @jeffallan, Principal Consultant at Synergetic Solutions
よくある質問
どのデータベースに対応していますか?
PostgreSQL、MySQL、SQL Serverの違いを説明しているため、よく使われる複数のデータベースに対応しています。
データベースへのアクセス権は必要ですか?
クエリプランの確認には、EXPLAIN ANALYZEの実行を勧めます。そのためにはデータベースでクエリを実行できる権限が必要です。
テーブルの設計も手伝ってもらえますか?
はい。正規化、キー、制約、スキーマ設計に対応しています。
詳細
作者による元の説明
Optimizes SQL queries, designs database schemas, and troubleshoots performance issues. Use when a user asks why their query is slow, needs help writing complex joins or aggregations, mentions database performance issues, or wants to design or migrate a schema. Invoke for complex queries, window functions, CTEs, indexing strategies, query plan analysis, covering index creation, recursive queries, EXPLAIN/ANALYZE interpretation, before/after query benchmarking, or migrating queries between database dialects (PostgreSQL, MySQL, SQL Server, Oracle).
repo: Jeffallan/claude-skills
人気
- GitHubのスター
- 12k
- ランキング
- #11460
- ライセンス
- MIT
- 更新日
- 2026/10/30
コメント
- …
このリポジトリの他のスキル
angular-architect
12kエージェントが、スタンドアロンコンポーネント、遅延読み込みのルーティング、NgRxの状態管理、RxJSのパターン、性能の調整を使ったAngular 17以降のアプリを作るのを支援します。
api-designer
12kエージェントが、RESTまたはGraphQLのAPIを設計し、OpenAPI 3.1の仕様を書くのを支援します。リソース、ページネーション、エラーの形式、バージョン管理のルールを含みます。
architecture-designer
12kエージェントが、システムのアーキテクチャを設計し、パターンや技術を比べて、それぞれの選択を説明するアーキテクチャ決定記録(ADR)を書くのを支援します。
atlassian-mcp
12kMCPを通じて、JiraのIssueとConfluenceのページを検索、更新するのを支援します。JQLとCQLのクエリ、スプリント、ドキュメントの同期を含みます。
似ているスキル
discrawl
394k保存済みのローカルなDiscordメッセージのアーカイブを検索して要約し、その新しさを確認し、ダイレクトメッセージを読めるようエージェントを支援します。
clickhouse-io
280kエージェントがClickHouseデータベースのクエリとスキーマを最適化できるようにします。高性能な分析処理向けのベストプラクティスを適用します。分析クエリが遅すぎるときに使います。
scientific-db-pubmed-database
280kエージェントが生物医学分野の科学論文を検索するのに役立ちます。特定の医療情報が必要な研究に便利です。