Optimize A Slow SQL Query
Diagnoses why a query is slow and rewrites it with targeted, explained optimizations and an index plan.
Prompt
ROLE: You are a database performance engineer specializing in [DATABASE_ENGINE] query tuning. CONTEXT: The query below runs slowly (current runtime ~[CURRENT_RUNTIME] on ~[ROW_COUNTS] rows). Relevant schema, indexes, and partitioning: [SCHEMA_AND_INDEXES]. EXPLAIN/EXPLAIN ANALYZE output (if available): [EXPLAIN_OUTPUT]. Query: ```sql [SLOW_QUERY] ``` TASK: 1. Walk through the execution plan and name the top 1-3 bottlenecks (e.g., full scans, spills, nested-loop blowups, redundant sorts, non-sargable predicates). 2. For each bottleneck, explain the root cause in one sentence. 3. Produce a rewritten query that is logically equivalent but faster, preserving the exact result set. 4. Recommend concrete physical changes (indexes, partition keys, clustering, materialization) with the exact DDL. 5. Estimate the expected improvement and call out any tradeoffs. OUTPUT FORMAT: (1) Diagnosis table [Issue | Cause | Fix], (2) Optimized ```sql```, (3) Index/DDL recommendations, (4) Expected impact & tradeoffs. CONSTRAINTS: Guarantee identical results to the original (note any edge case where they could differ). Prefer set-based logic over row-by-row. Do not recommend hints unless justified. Flag any change that alters NULL or duplicate handling.
How to use this prompt
- 1
Copy the prompt above and paste it into ChatGPT, Claude, or Gemini — or open it in the visual Studio to edit each part on a canvas and run it with your own key.
- 2
Replace any bracketed placeholders with your specifics. The more concrete your context and constraints, the sharper the result — see the 5-part prompt structure.
- 3
Run it, then refine. Ask the model to critique and improve its own answer with self-critique prompting.
Techniques in this prompt
Assigns the model an expert persona so it adopts the right vocabulary, depth, and standards for the task.
Learn this techniqueAsks the model to reason step by step before answering — ideal for multi-step, logical, or analytical tasks.
Learn this techniqueForces explicit intermediate reasoning instead of jumping to a conclusion, which improves accuracy on hard tasks.
Learn this techniqueRecommended models
Build on this prompt
Open it in the visual Studio to wire it into a full workflow with your own API key — or learn the craft behind prompts like this.
More in Data Analysis & SQL
Translate Business Questions Into SQL
Turns a plain-English stakeholder question into a correct, well-commented SQL query against a known schema.
Debug A SQL Query That Returns Wrong Results
Systematically finds the logic error producing incorrect numbers and delivers a corrected, verified query.
Explain An Unfamiliar SQL Query In Plain English
Reverse-engineers a complex inherited query into a clear narrative, business meaning, and risk list.
Design A Star Schema For Analytics
Produces a dimensional model with fact and dimension tables, grain, keys, and SCD strategy for a reporting need.