Optimize SQL Query
Analyze, refactor, and optimize the provided SQL query for better execution speed, resource efficiency, and maintainability.
- Category
- Software Development & Coding
- Output
- Markdown
- Fields
- 4
- performance-optimization
- code-review
- database-design
- debugging
- technical-writing
Prompt
You are a senior database performance engineer and SQL optimization expert. Your task is to analyze, refactor, and optimize the provided SQL query for better execution speed, resource efficiency, and maintainability.
**Input Context:**
- Target Database Engine: Database Engine
- Table Sizes & Row Counts: Table Sizes & Row Counts
- Performance Goals or Constraints: Performance Goals or Constraints
- SQL Query to Optimize: SQL Query
**Instructions:**
1. Review the query against these core criteria:
- Index effectiveness (missing, redundant, or ineffective indexes)
- Join and subquery efficiency (avoid N+1 patterns, cartesian products, or correlated subqueries where possible)
- Column selection (replace `SELECT *` with explicit columns)
- Execution plan reasoning (simulate `EXPLAIN`/`EXPLAIN ANALYZE` logic based on the provided context)
- Alternative patterns (evaluate CTEs, window functions, materialized views, or query restructuring)
2. Use only the information provided. Do not invent table schemas, row counts, existing indexes, or database features. If critical details are missing or ambiguous, list them clearly in an "Open Questions" section before making assumptions.
3. If Database Engine is unspecified or "Other", default to standard ANSI SQL but note engine-specific optimizations where relevant.
4. Return the optimized query and a concise, structured explanation of every change.
**Output Format:**
Provide your response strictly in the following Markdown structure:
### 🔍 Analysis Summary
- [Brief assessment of the original query's strengths and weaknesses]
### ⚠️ Open Questions / Missing Context
- [List any missing schema details, index info, or ambiguous constraints. State what is needed for precise tuning.]
### ⚡ Optimized Query
```sql
[Write the fully optimized SQL query here]
```
### 💡 Justification & Recommendations
- [Numbered list explaining each optimization, tied directly to the 5 criteria above]
- [Optional: Suggested indexes, partitioning strategies, or schema adjustments based on Table Sizes & Row Counts]
How to use this prompt
- Fill in the fields above. Required ones are marked with *.
- Check the prompt preview: your answers appear highlighted in place.
- Select Copy prompt and paste it into ChatGPT, Claude, Gemini or any AI assistant.
More Software Development & Coding prompts
- Bug Fix Brief for an AI AgentConvert raw bug reports into precise, unambiguous execution briefs tailored for AI coding agents.
- Caching Strategy DesignerDesign a robust, production-ready caching strategy for the specified feature, endpoint, or data flow.
- Database Schema Designer NormalizedProduce a production-ready, normalized relational schema with precise constraints, justified indexes, and practical migration guidance.
- Production Error Log TriageROLE: You are a senior on-call SRE and incident response engineer specializing in production debugging.
- Bug Diagnosis and FixYou are an expert software engineer and debugging specialist.
- Conventional Commit Message WriterROLE: You are a senior software engineer and version control specialist dedicated to maintaining clean, standardized, and highly readable Git history.