P
💻DeveloppementAdvancedChatGPT

Optimize Slow SQL Queries

Analyze and optimize your slow SQL queries through execution plan analysis and precise index recommendations.

Paste in your AI

Adapt this prompt

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:

  1. Reading the execution plan: explain each node of the EXPLAIN ANALYZE and identify sequential scans, costly hash joins, and incorrect estimates.
  2. Missing indexes: propose the optimal indexes (single-column, composite, partial) with the exact SQL DDL.
  3. Query rewrite: propose an optimized version of the query with an explanation of the changes.
  4. Advanced strategies: if applicable, suggest materialized views, partitioning, CTEs, or denormalization.
  5. 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

Production database optimizationSQL performance auditIndex and execution plan training

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 Optimizer

Comments

Be the first to comment on this prompt.

📬 Get new prompts every week

Join our newsletter and never miss a prompt.

Go further

Similar Prompts

💻DeveloppementIntermediateClaude

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.

1681
💻DeveloppementIntermediateClaude

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.

640
💻DeveloppementAdvancedAll AIs

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.

411
💻DeveloppementIntermediateAll AIs

Generate Storybook Stories for UI Components

Generate Storybook stories for a UI component, with visual variants, interactive controls, interaction tests, and MDX documentation.

474