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
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
Source Code Security Audit
Audit your code security according to the OWASP Top 10 with vulnerability identification, exploitation PoC, and fixes.
Debug a Production Error
Quickly analyze a production error with a structured Root Cause Analysis approach and an immediate action plan.
Refactor Legacy Code Step by Step
This prompt guides AI to analyze legacy code and produce a structured refactoring plan with diagnosis, prioritization, tests, and modernized code.
Create a CLI Tool with Node.js
Create a professional Node.js CLI tool with Commander.js, Inquirer, visual feedback, and npm publishing.