From aigroup-workflow
Optimizes SQL queries, designs database schemas, troubleshoots performance in PostgreSQL, MySQL, SQL Server, Oracle. Handles complex joins, CTEs, window functions, indexing, EXPLAIN/ANALYZE, dialect migrations.
npx claudepluginhub codeape-7/ai-agent-workflowgroupThis skill uses the workspace's default tool permissions.
1. **Schema Analysis** - Review database structure, indexes, query patterns, performance bottlenecks
Optimizes SQL queries, designs database schemas, and troubleshoots performance issues for PostgreSQL, MySQL, SQL Server, Oracle. Use for slow queries, complex joins, CTEs, window functions, indexing strategies, EXPLAIN/ANALYZE.
Analyzes and optimizes SQL queries for performance in PostgreSQL and MySQL. Use for slow query tuning, rewriting, index verification, join optimization, and EXPLAIN plan analysis.
Optimizes SQL queries with indexing strategies, EXPLAIN analysis, and patterns to debug slow queries, design performant schemas, and improve app performance.
Share bugs, ideas, or general feedback.
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 proceedingLoad 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 |
-- 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
-- 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;
-- 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:
ANALYZE <table> to refresh statisticsread count signals missing cache / index-- 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);
When implementing SQL solutions, provide: