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
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 GuideGlossary Terms
Similar Prompts
Learn the basics of Git for beginners
Learn Git from scratch with illustrated explanations, concrete examples, a practical workflow and a cheatsheet of essential commands.
Define a Git strategy for a team
Define a comprehensive Git strategy adapted to your team: branching model, conventions, code review and release management.
Configure a CI/CD pipeline with GitHub Actions
Configure a professional CI/CD pipeline with GitHub Actions covering testing, security, Docker build and multi-environment deployment.
Write integration tests for an API
Create comprehensive API integration tests with database setup, authentication, CRUD and end-to-end scenarios.