選項

優化 SQL 查詢、設計資料庫模式,並排除效能問題。當使用者詢問查詢為何緩慢、需要協助撰寫複雜的聯結或彙總、提及資料庫效能問題,或是希望設計或遷移資料庫模式時,可運用此服務。 適用於處理複雜查詢、視窗函數、CTE、索引策略、查詢計畫分析、建立覆蓋索引、遞迴查詢、解讀 EXPLAIN/ANALYZE 結果、查詢前後效能基準測試,或將查詢在不同資料庫之間遷移。

...展開全部
57
更新時間 2026-06-29

關於sql-pro

SQL Pro 是一項專門的 AI 技能,旨在提升資料庫查詢效率、優化資料結構設計,並排除效能問題。它能解決關聯式資料庫中常見的挑戰,例如查詢執行緩慢或難以維護,特別是在處理複雜的聯結、彙總或大型資料集時。 透過分析 SQL 查詢、執行計畫及索引策略,此技能能協助使用者找出瓶頸,並實施最佳實務,以實現更快且更可預測的資料庫效能。

此技能提供結構化的工作流程,首先進行模式分析,評估資料庫結構、索引及查詢模式,以精準找出效能瓶頸;接著協助運用進階技術(如共通表式 (CTE)、視窗函數及適當的聯結策略)來設計查詢。 優化功能包括執行計畫分析、建立覆蓋索引、消除全表掃描,以及透過迭代精進來達成效能目標。 SQL Pro 亦著重透過 `EXPLAIN ANALYZE` 輸出結果進行驗證,確保查詢高效執行且索引得以有效運用。本技能提供關於查詢、索引設計依據及效能指標的完整文件,以支援系統維護性與知識傳承。

SQL Pro 專為使用 PostgreSQL、MySQL、SQL Server 或 Oracle 資料庫,且需要具備查詢調優、模式遷移或效能優化專業知識的資料庫管理員、開發人員及資料工程師所設計。 典型應用情境包括加速緩慢查詢、設計正規化模式、在不同資料庫方言間轉換查詢、分析執行計畫,以及在不犧牲效率的前提下實作複雜分析。 其指引在需要使用進階 SQL 功能(如遞迴查詢、視窗函數及效能基準測試)的場景中尤為寶貴,使其成為致力於維護高效能、可擴展資料庫應用程式的團隊不可或缺的關鍵工具。

常見問題

如何使用 SQL Pro 來優化緩慢的查詢?

請提供查詢語句及相關資料結構資訊。SQL Pro 將分析執行計畫、建議索引改進方案,並運用 CTE 或彙總聯結等高效模式重寫查詢語句。

SQL Pro 支援哪些資料庫系統?

SQL Pro 支援 PostgreSQL、MySQL、SQL Server 及 Oracle,並針對這些資料庫方言之間的差異提供指引。

SQL Pro 能否處理包含視窗函數的複雜分析查詢?

可以。它提供指引,說明如何在分區內高效使用 ROW_NUMBER、RANK、SUM 以及 LAG/LEAD 等視窗函數,同時避免不必要的自聯結。

使用 SQL Pro 執行 EXPLAIN ANALYZE 時,應檢查哪些項目?

關鍵檢查項目包括:偵測大型資料表上的順序掃描、比較實際與預估的列數,以及檢視緩衝區命中率和讀取次數,以識別缺失的索引或過期的統計資料。

使用 SQL Pro 是否有任何限制或先決條件?

使用者應具備存取資料庫模式的權限,以及執行查詢和分析執行計畫所需的足夠權限。效能提升取決於適當的索引設定、查詢結構以及資料庫統計資料。

在 GitHub 上查看

SQL Pro

Core Workflow

  1. Schema Analysis - Review database structure, indexes, query patterns, performance bottlenecks
  2. Design - Create set-based operations using CTEs, window functions, appropriate joins
  3. Optimize - Analyze execution plans, implement covering indexes, eliminate table scans
  4. Verify - Run EXPLAIN ANALYZE and confirm no sequential scans on large tables; if query does not meet sub-100ms target, iterate on index selection or query rewrite before proceeding
  5. Document - Provide query explanations, index rationale, performance metrics

Reference Guide

Load detailed guidance based on context:

TopicReferenceLoad When
Query Patternsreferences/query-patterns.mdJOINs, CTEs, subqueries, recursive queries
Window Functionsreferences/window-functions.mdROW_NUMBER, RANK, LAG/LEAD, analytics
Optimizationreferences/optimization.mdEXPLAIN plans, indexes, statistics, tuning
Database Designreferences/database-design.mdNormalization, keys, constraints, schemas
Dialect Differencesreferences/dialect-differences.mdPostgreSQL vs MySQL vs SQL Server specifics

Quick-Reference Examples

CTE Pattern

-- Isolate expensive subquery logic for reuse and readabilityWITH 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_amountFROM ranked_ordersWHERE rn = 1;                           -- latest completed order per customer

Window Function Pattern

-- Running total and rank within partition — no self-join requiredSELECT    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_rankFROM employees;

EXPLAIN ANALYZE Interpretation

-- PostgreSQL: always use ANALYZE to see actual row counts vs. estimatesEXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)SELECT *FROM orders oJOIN customers c ON c.id = o.customer_idWHERE 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 read count 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_countFROM orders o;-- AFTER: single aggregation join (fast)SELECT o.order_id, COALESCE(agg.item_count, 0) AS item_countFROM orders oLEFT 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:

  1. Optimized query with inline comments
  2. Required indexes with rationale
  3. Execution plan analysis
  4. Performance metrics (before/after)
  5. Platform-specific notes if applicable

Documentation

安裝 sql-pro

請下載並將技能檔案解壓縮至您的 .claude/skills/ 目錄中。

下載 ZIP

複製儲存庫並將技能檔案複製到您的專案中。

git clone https://github.com/Jeffallan/claude-skills/blob/main/skills/sql-pro/SKILL.md # Copy SKILL.md to your .claude/skills/ directory

複製 複製
快速設定: 將技能資料夾複製到 .claude/skills/,Claude 會自動偵測並使用該技能

相關技能

microservices-patterns
更新時間 2026-06-29
fabric-lakehouse
更新時間 2026-06-30
jpa-patterns
更新時間 2026-06-30
prisma-expert
更新時間 2026-06-29
OR