Back to Cookbook

Query Surgeon

Diagnose and fix slow SQL without being a DBA

Paste a slow query and get back a diagnosis in plain English — what's slow, why it's slow, and which fixes are most likely to help. Reads EXPLAIN plans so you don't have to. Suggests indexes, rewrites, and pre-aggregation strategies.

CommunitySubmitted by CommunityWork1 min

PROMPT

Create a skill called "Query Surgeon". When I paste a SQL query that's running slowly, analyze it for performance issues. If I include EXPLAIN ANALYZE output, parse it and translate every node into plain English (e.g., "Sequential Scan on orders — scanning all 2.3 million rows because there's no index on customer_id"). If I don't include EXPLAIN output, infer likely bottlenecks from the query structure. For each issue found, explain: what's happening, why it's slow, and how to fix it. Generate an optimized version of the query with comments. Suggest CREATE INDEX statements where applicable. If the query runs repeatedly, suggest a materialized view or pre-aggregation table. Estimate the likely improvement range for each suggestion and state the assumptions behind it.

How It Works

Your query takes 45 minutes and you have no idea why. EXPLAIN ANALYZE

produces a wall of text that only DBAs understand. This skill translates

query plans into plain English, identifies the specific bottleneck, and

generates an optimized version with likely impact ranges and clear assumptions.

What You Get

  • Plain-English diagnosis of why the query is slow
  • Identification of the specific bottleneck (full table scans, bad join order, missing indexes, correlated subqueries)
  • An optimized rewrite with inline comments explaining each change
  • Index recommendations with CREATE INDEX statements
  • Expected impact range for each suggestion, with assumptions called out
  • Pre-aggregation table suggestions for queries that run repeatedly

Setup Steps

  1. Ask your Claw to create a "Query Surgeon" skill with the prompt below
  2. Paste your slow query (and EXPLAIN output if you have it)
  3. Tell it which database engine you're using
  4. Get back a diagnosis and optimized query

Tips

  • Include the EXPLAIN ANALYZE output if you can — it makes the diagnosis much more precise
  • For queries that run on a schedule, the pre-aggregation suggestions save the most time
  • The skill will ask about table sizes and indexes if it needs more context
  • Works on any SQL dialect — Postgres, BigQuery, Snowflake, MySQL, Redshift, SQL Server
Tags:#sql#performance#optimization#databases