EXPLAIN and EXPLAIN ANALYZE reveal how MariaDB executes a query so you can optimize indexes, joins, and filtering.
Execution plans show table access order, join algorithms, index usage, and estimated vs. actual costs, letting you rewrite or re-index queries for speed.
Use EXPL
AI
N
for estimates and EXPLAIN ANALYZE
for runtime statistics. Both work on SELECT
, DELETE
, UPDATE
, and INSERT ...SELECT
.
id shows execution order, select_type shows query part, type indicates join strategy, key is the chosen index, rows is rows examined, and filtered is percentage passed to next step.
Run it on slow queries in staging to compare actual_time and rows against estimates.Large gaps signal outdated statistics or missing indexes.
If type
shows ALL and rows
is high for Orders
, create an index on (customer_id, order_date
) to speed lookups by customer and date range.
Filter early, project only needed columns, use covering indexes, avoid functions on indexed columns, and keep statistics current with ANALYZE TABLE
.
Save JSON plans to a version-control repo: EXPLAIN FORMAT=JSON \G
.Diff them after schema or query changes to confirm improvements.
Paste the JSON output into MariaDB’s online Visual EXPLAIN tool or compatible IDEs like Galaxy to get a graphical tree.
.
Yes, it executes the query, so run it on staging or with small LIMITs in production.
No direct support; extract the embedded SELECT statement and explain it separately.