Optimize Slow SQL Queries
Analyze and optimize your slow SQL queries through execution plan analysis and precise index recommendations.
Paste in your AI
Paste this prompt in ChatGPT, Claude or Gemini and customize the variables in brackets.
You are a database optimization expert with a specialization in [PostgreSQL/MySQL/SQLite]. I have SQL queries that are too slow in production and are hurting my application's performance.
Problematic query:
[paste the SQL query]
EXPLAIN ANALYZE output:
[paste the explain analyze output]
Context:
- Number of rows in each relevant table: [e.g., users: 500k, orders: 2M]
- Existing indexes: [list the indexes]
- Current execution time: [e.g., 3.2 seconds]
- Performance goal: [e.g., < 200ms]
- Execution frequency: [e.g., 500 times/minute]
Analyze and optimize this query by:
- Reading the execution plan: explain each node of the EXPLAIN ANALYZE and identify sequential scans, costly hash joins, and incorrect estimates.
- Missing indexes: propose the optimal indexes (single-column, composite, partial) with the exact SQL DDL.
- Query rewrite: propose an optimized version of the query with an explanation of the changes.
- Advanced strategies: if applicable, suggest materialized views, partitioning, CTEs, or denormalization.
- Monitoring: propose metrics to watch in order to detect performance regressions.
Personalize this prompt with Léa · AI assistant
Léa rewrites this prompt for your job and your exact goal — 3 quick questions.
Why this prompt works
This prompt is structured to provide the AI with all the information needed for an accurate diagnosis: the query, execution plan, data volumes, and existing indexes. Without this context, recommendations would be generic and potentially counterproductive. Requesting a commented reading of the EXPLAIN ANALYZE is particularly educational as it helps understand why the database makes certain decisions, which is essential for avoiding the same performance mistakes in the future. By including execution frequency in the context, the AI can prioritize optimizations with the best ROI: a query executed 500 times per minute deserves more investment than one run once per day.
Use Cases
Expected Output
An execution plan analysis, index recommendations with DDL SQL, the rewritten query, and advanced strategies if relevant.
Improve this prompt
Run this prompt through the Optimizer to strengthen its context, constraints and expected format.
Improve this prompt with the OptimizerComments
Be the first to comment on this prompt.
Glossary Terms
📬 Get new prompts every week
Join our newsletter and never miss a prompt.
Go further
Improve this prompt with the Optimizer
Analyze the prompt, strengthen the context and get a more precise version.
ChatGPT Prompts
Core techniques to guide ChatGPT more effectively.
AI prompts for web developers
Code review and debugging, keeping API keys and secrets out of the prompt.
Security, GDPR & Business Plans
ChatGPT Business, Claude Team, Gemini Workspace and Mistral Vibe compared from vendor sources: training, EU hosting and a GDPR checklist.
Similar Prompts
Task brief for Claude Code: frame a feature end to end
Brief template to delegate a complete feature to Claude Code: goal, starting point, constraints, definition of done, working rules and recap format.
Write a project's CLAUDE.md: the brief Claude Code reads at every session
Turns your project description into a dense CLAUDE.md: commands, structure, conventions, pitfalls, off-limits areas and situations where the agent must ask before acting.
Design a Secure Webhook API
Design a webhook API with event subscriptions, signed deliveries, retry limits, and a delivery log your support team can inspect.
Generate Storybook Stories for UI Components
Generate Storybook stories for a UI component, with visual variants, interactive controls, interaction tests, and MDX documentation.