AccueilGlossaire › EXPLAIN / Execution Plan

EXPLAIN / Execution Plan

Data

Commande SQL révélant le plan d'exécution choisi par le query optimizer.

EXPLAIN (ou Execution Plan) est la commande SQL essentielle pour analyser comment le query optimizer va (ou a) exécuter une requête — quels indexes utilisés, ordre des joins, methods access, estimations de coût et rows. Outil #1 pour query tuning.

Syntaxe par DB :
- **PostgreSQL** : `EXPLAIN SELECT ...` (estimate), `EXPLAIN ANALYZE SELECT ...` (actually run + measure), `EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON)` (detailed).
- **MySQL** : `EXPLAIN SELECT ...`, `EXPLAIN FORMAT=JSON`, `EXPLAIN ANALYZE` (depuis 8.0.18).
- **SQL Server** : `SET SHOWPLAN_ALL ON` ou "Display Estimated Execution Plan" SSMS.
- **Oracle** : `EXPLAIN PLAN FOR SELECT ...` puis `SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY)`.

Éléments à lire :
(1) **Node types** — Sequential Scan (full table scan, slow on big tables), Index Scan (uses index), Bitmap Index Scan, Index Only Scan (no table lookup), Nested Loop Join, Hash Join, Merge Join, Hash Aggregate, Sort.
(2) **Cost** — abstract units, comparer plans pas absolute ; lower better.
(3) **Rows** — estimation vs actual (gros écart = stats outdated → ANALYZE).
(4) **Time** (avec ANALYZE) — réel temps d'exécution chaque node.
(5) **Loops** — combien de fois node exécuté (nested loop = N executions).
(6) **Buffers** (Postgres avec BUFFERS) — shared hit/read (cache hit ratio).

Red flags :
(1) **Sequential Scan** sur grande table avec WHERE clause sélective → missing index.
(2) **Rows estimation vs actual gros écart** → stats outdated, run ANALYZE.
(3) **Nested Loop Join** sur joining tables grandes sans index → consider hash/merge join (often forced by missing index on join column).
(4) **Sort node** that doesn't fit memory → spill to disk, slow. Increase `work_mem` Postgres ou add index supporting ORDER BY.
(5) **Filter** after scan removing many rows → push predicate earlier with index.

Visualization tools : (1) **explain.dalibo.com** — Postgres EXPLAIN visualization brilliant ; (2) **pgMustard** — Postgres tuning suggestions ; (3) **MySQL Workbench Visual Explain** ; (4) **SQL Server Execution Plan SSMS** ; (5) **pg_stat_statements** — top slow queries to optimize first. Compétences DP-300, DEA-C01.

Certifications qui couvrent ce concept
DP-300 DEA-C01
Termes liés
Query Optimizer (Database) Slow Query Log B-Tree Index

Préparez vos certifications IT gratuitement

200+ certifications, 400 000+ questions, examens blancs chronométrés.

Voir le catalogue →
← Retour au glossaire