SQL Query Optimization and Schema Review
Get SQL rewrites, evidence-based index recommendations, schema reviews, and dialect migrations with validation steps.
Before & after
Same prompt, same agent — the only difference is the skill.
A 30-second query on 500K rows suggests there might be some performance issues. It could be a missing index, a suboptimal query plan, or maybe the query is doing a full table scan when it shouldn't. To help you optimize it, I'd need to see the actual query and ideally the EXPLAIN or EXPLAIN ANALYZE output from your database. I'd also want to know what columns you're filtering or joining on, and what version of the database you're using. Once I see those details, I can identify the bottleneck—whether it's the join strategy, the index situation, or maybe something in the WHERE clause that's preventing index usage.
QUERY PERFORMANCE ANALYSIS DIAGNOSTIC FIRST STEP: Run EXPLAIN ANALYZE EXPLAIN ANALYZE SELECT * FROM orders o JOIN customers c ON o.customer_id = c.id WHERE o.created_at > NOW() - INTERVAL '30 days' AND c.status = 'active' ORDER BY o.created_at DESC LIMIT 100; COMMON BOTTLENECK: Sequential scan on orders table (30 sec runtime = ~16K rows/sec = full table scan) ROOT CAUSE CHECK 1. Missing index on (created_at, customer_id)? Add composite index: CREATE INDEX idx_orders_date_customer ON orders(created_at DESC, customer_id); 2. Stale statistics? Run ANALYZE on table (planner uses outdated row estimates) 3. JOIN order wrong? Planner chose nested loop instead of hash join? OPTIMIZED QUERY SELECT o.id, o.total, c.name FROM orders o JOIN customers c ON o.customer_id = c.id WHERE o.created_at > NOW() - INTERVAL '30 days' AND c.status = 'active' ORDER BY o.created_at DESC LIMIT 100; EXPECTED PLAN: Index scan on idx_orders_date_customer (milliseconds), hash join with customers, early LIMIT cut REWRITE TACTICS -> Filter early: Move c.status to JOIN condition (not WHERE) to reduce joined rows -> Add covering index: Include columns needed in SELECT (c.name) to avoid table lookup -> Partition strategy: If table > 1GB, partition by month (query scans fewer partitions) VERIFY: Rerun EXPLAIN ANALYZE; target: < 100 ms, uses index scan, < 500 rows examined
About this skill
Your query is slow, but another index or a shorter SQL statement may only move the problem. This skill diagnoses execution plans, catches joins that inflate results, and produces targeted SQL and schema changes with a concrete way to verify them.
What it does
- Separates incorrect query results from expensive execution before proposing a rewrite.
- Reads execution plans for cardinality errors, repeated work, spills, and excessive row access.
- Recommends indexes against actual predicates and ordering, including write and storage costs.
- Writes and reviews joins, aggregations, window functions, recursive queries, and pagination.
- Reviews schema constraints, relationships, data types, and migration sequencing against real workloads.
- Ports SQL between PostgreSQL, MySQL, SQL Server, and Oracle while identifying semantic differences.
What you get
Copyable SQL or DDL, the reasoning behind each material change, and targeted correctness and performance checks. When execution evidence is missing, recommendations are clearly provisional rather than presented as measured improvements.
Best for
Developers and database engineers troubleshooting slow endpoints, reviewing complex SQL, evolving relational schemas, or migrating queries between database engines.
SKILL.md preview
---
name: sql-query-optimization-and-schema-review
description: >-
Writes and reviews SQL, diagnoses execution plans, designs workload-driven indexes and schemas, and ports queries across PostgreSQL, MySQL, SQL Server, and Oracle. Uses result-grain checks, plan evidence, and controlled validation to separate correctness fixes from performance hypotheses. Use when the user says "my query keeps timing out", "why does this join double my totals", "read this execution plan", "which index would help", "write a window function", "review this schema", or "convert this SQL to another database".
---
# SQL Query Optimization and Schema Review
… (sign up to view the full skill)Featured in
More development skills
View all Development skills →C# API and Blazor Implementation
Build and fix C# APIs and Blazor components with safe EF Core queries, cancellation, and focused tests.
Rust Code Implementation and Debugging
Get Rust code and targeted fixes for ownership, lifetimes, traits, Tokio concurrency, errors, and unsafe boundaries.
Type-Safe PHP Applications and APIs
Build and repair typed PHP applications with Laravel or Symfony, safe database changes, and targeted tests.
Modern C++ Implementation, Debugging & Optimization
Build and repair modern C++ with explicit ownership, constrained templates, race analysis, profiling, and target-based CMake.