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

Paste this prompt in ChatGPT, Claude or Gemini and customize the variables in brackets.

Tu es un expert en optimisation de bases de données avec une spécialisation en [PostgreSQL/MySQL/SQLite]. J'ai des requêtes SQL qui sont trop lentes en production et qui impactent les performances de mon application.

**Requête problématique :**
```sql
[COLLER_LA_REQUÊTE_SQL]
```

**Résultat de l'EXPLAIN ANALYZE :**
```
[COLLER_LE_RÉSULTAT_EXPLAIN_ANALYZE]
```

**Contexte :**
- Nombre de lignes dans chaque table concernée : [EX: users: 500k, orders: 2M]
- Index existants : [LISTER_LES_INDEX]
- Temps d'exécution actuel : [EX: 3.2 secondes]
- Objectif de performance : [EX: < 200ms]
- Fréquence d'exécution : [EX: 500 fois/minute]

Analyse et optimise cette requête en :
1. **Lecture du plan d'exécution** : explique chaque nœud de l'EXPLAIN ANALYZE et identifie les séquential scans, les hash joins coûteux et les estimations incorrectes.
2. **Index manquants** : propose les index optimaux (simples, composites, partiels) avec le DDL SQL exact.
3. **Réécriture de la requête** : propose une version optimisée de la requête avec explication des changements.
4. **Stratégies avancées** : si applicable, suggère materialized views, partitionnement, CTEs ou dénormalisation.
5. **Monitoring** : propose des métriques à surveiller pour détecter les régressions de performance.

Why this prompt works

<p>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.</p><p>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.</p><p>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.</p>

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.

Learn more

Check the full skill on Prompt Guide to master this technique from A to Z.

View on Prompt Guide

📬 Get new prompts every week

Join our newsletter and never miss a prompt.

Similar Prompts

💻DeveloppementAdvancedClaude

Design a Microservices Architecture

Design a complete microservices architecture with DDD decomposition, communication patterns, resilience, and observability.

5087
💻DeveloppementAdvancedAll AIs

Create a Complete REST API from A to Z

A complete prompt to generate a professional REST API with authentication, validation, documentation, and integrated tests.

038
💻DeveloppementIntermediateChatGPT

Optimize React Application Performance

Optimize your React application to achieve excellent Core Web Vitals metrics through advanced memoization and code splitting techniques.

3691
💻DeveloppementIntermediateGemini

Define a Git Strategy for a Team

Define a complete Git strategy adapted to your team: branching model, conventions, code review, and release management.

1983