{
    "slug": "mysql_explain",
    "term": "MySQL EXPLAIN",
    "category": "database",
    "difficulty": "intermediate",
    "short": "A MySQL query analysis command showing the execution plan — which indexes are used, how many rows are scanned, and where bottlenecks are.",
    "long": "EXPLAIN prefixed to a SELECT/UPDATE/DELETE shows the query execution plan. Key columns: 'type' (ALL = full scan, ref = index lookup, eq_ref = unique index — lower is better), 'key' (which index was chosen), 'rows' (estimated rows examined), 'Extra' (Using filesort, Using temporary — both indicate optimisation opportunities). EXPLAIN ANALYZE (MySQL 8+) executes the query and shows actual vs estimated row counts.",
    "aliases": [
        "MySQL EXPLAIN query",
        "query execution plan MySQL",
        "EXPLAIN ANALYZE"
    ],
    "tags": [
        "mysql",
        "database",
        "performance"
    ],
    "misconception": "EXPLAIN shows the actual query execution. Standard EXPLAIN shows the estimated plan only — use EXPLAIN ANALYZE (MySQL 8+) or EXPLAIN FORMAT=JSON for actual execution data.",
    "why_it_matters": "EXPLAIN is the primary tool for diagnosing slow queries. A single EXPLAIN output immediately reveals whether a query is doing a full table scan, which index it uses, and whether a sort operation is hitting disk.",
    "common_mistakes": [
        "Not running EXPLAIN before adding an index — the index may already exist or may not be used.",
        "Ignoring the 'Extra' column — 'Using filesort' and 'Using temporary' are expensive operations.",
        "Running EXPLAIN ANALYZE on a slow write query in production — it executes the query, causing the write."
    ],
    "when_to_use": [
        "Run EXPLAIN on any query that feels slow or affects many rows.",
        "Run EXPLAIN before and after adding an index to verify it is actually used."
    ],
    "avoid_when": [
        "Do not run EXPLAIN ANALYZE on write queries (INSERT/UPDATE/DELETE) in production — it actually executes them."
    ],
    "related": [
        "mysql_indexes_explained",
        "slow_query_log",
        "query_optimisation",
        "db_explain_advanced"
    ],
    "prerequisites": [
        "mysql_indexes_explained"
    ],
    "refs": [
        "https://dev.mysql.com/doc/refman/8.0/en/explain.html"
    ],
    "bad_code": "-- Debugging slow query by guessing\n-- Adding indexes blindly without checking EXPLAIN first",
    "good_code": "-- Check execution plan\nEXPLAIN SELECT * FROM orders WHERE user_id = 42 AND status = 'pending';\n-- Look for: type=ref or better, key=idx_user_status, rows=small number\n\n-- MySQL 8+: actual execution stats\nEXPLAIN ANALYZE SELECT * FROM orders WHERE user_id = 42;",
    "quick_fix": "Run EXPLAIN SELECT ... on any slow query — if type=ALL and rows is large, add an index on the WHERE/JOIN column",
    "effort": "low",
    "created": "2026-03-31",
    "updated": "2026-03-31",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/mysql_explain",
        "html_url": "https://codeclaritylab.com/glossary/mysql_explain",
        "json_url": "https://codeclaritylab.com/glossary/mysql_explain.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[MySQL EXPLAIN](https://codeclaritylab.com/glossary/mysql_explain) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/mysql_explain"
            }
        }
    }
}