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

Fill in the details

Paste the raw SQL query you want to optimize.

Specify the target database to tailor syntax and features (e.g., PostgreSQL, MySQL, SQLite).

Provide approximate row counts or size categories (e.g., 'users: 1M, orders: 50M') to help with index and partitioning advice.

Optional context like target latency, max rows returned, or specific bottlenecks you've observed.

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

  1. Fill in the fields above. Required ones are marked with *.
  2. Check the prompt preview: your answers appear highlighted in place.
  3. Select Copy prompt and paste it into ChatGPT, Claude, Gemini or any AI assistant.
See all Software Development & Coding prompts