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 necessary for an accurate diagnosis: the query, the execution plan, data volumes, and existing indexes. Without this context, the recommendations would be generic and potentially counterproductive.</p><p>The request for a commented reading of the EXPLAIN ANALYZE is particularly educational as it helps understand why the database makes certain decisions, which is essential to avoid repeating 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 a query run once per day.</p>

Use Cases

Database optimization in productionSQL performance auditTraining on indexes and execution plans

Expected Output

An analysis of the execution plan, index recommendations with SQL DDL, 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