{
  "$schema": "https://myflames.dev/schemas/sidecar-v1.json",
  "schema_version": "1.3",
  "generated_at": "2026-04-25T12:51:22Z",
  "myflames_version": "1.5.0",
  "source": {
    "type": "file",
    "engine": "mariadb",
    "fixture_path": "/Users/viniciusgrippa/Downloads/git/myflames/test/mariadb-explain-block-nl-join.json"
  },
  "plan_summary": {
    "total_time_ms": 185.701,
    "rows_sent": 3000,
    "rows_examined_estimate": 4000,
    "operator_count": 3,
    "max_depth": 2
  },
  "optimizer_switches": [
    {
      "name": "block_nested_loop",
      "value": "on",
      "explanation": "Buffers a batch of outer rows in a join buffer and scans the inner table once per batch instead of once per row. Inner still does a full/index/range scan — a missing index on the join column is the root cause.",
      "node_labels": [
        "Table scan [t1]"
      ]
    }
  ],
  "warnings": [
    {
      "severity": "error",
      "category": "nonsargable_join",
      "text": "Non-sargable join predicate: CONVERT(...) in t1.d = convert(concat('r',t2.y) using utf8mb4) — a function wrapped around the join column prevents index use.",
      "source": "plan",
      "node_labels": [
        "Table scan [t1]"
      ]
    },
    {
      "severity": "warn",
      "category": "full_scan",
      "text": "Full table scan: t2 (1000 rows), t1 (3000 rows)",
      "source": "plan",
      "node_labels": [
        "Table scan [t2]",
        "Table scan [t1]"
      ]
    },
    {
      "severity": "warn",
      "category": "bnl",
      "text": "Block Nested-Loop (BNL) join buffer detected — uses join_buffer_size (Extra: 'Using join buffer (Block Nested Loop)', type ALL/index/range)",
      "source": "plan",
      "node_labels": [
        "Table scan [t1]"
      ]
    }
  ],
  "suggestions": [
    {
      "severity": "high",
      "category": "rewrite",
      "action": "Rewrite the join condition to compare the bare column on both sides (e.g. 'a.id = b.other_id' instead of 'CONCAT(a.id)=CONCAT(b.other_id)').",
      "source": "plan",
      "why": "wrapping a column in a function means the optimizer cannot use any index on that column — every row-pair is evaluated in the server layer, making the cost O(outer × inner). Dropping the function lets MySQL/MariaDB pick an index lookup or hash join with real selectivity, typically 100–1000× faster on mid-sized tables."
    },
    {
      "severity": "high",
      "category": "index",
      "action": "Add indexes on filter/join columns to avoid full table scans",
      "source": "plan"
    },
    {
      "severity": "medium",
      "category": "tuning_variable",
      "action": "Add indexes to eliminate BNL full/index scans, or increase join_buffer_size. In MySQL 8.0.20+, set block_nested_loop=off to force hash join instead",
      "source": "plan",
      "target_variable": "join_buffer_size"
    }
  ],
  "executive_summary": "Query scans 2 tables and joins via block nested-loop; returns 3,000 rows in 186 ms. Main finding: join predicate wraps the column in CONVERT() — no index can be used.",
  "plan_tree": {
    "node_id": "n:4ee53c8ca1ad",
    "short_label": "Nested loop inner join",
    "folded_label": "NESTED LOOP INNER starts=1 rows=3000",
    "children": [
      {
        "node_id": "n:7561bc6d0312",
        "short_label": "Table scan [t2]",
        "folded_label": "TABLE SCAN [t2] starts=1 rows=1000",
        "children": []
      },
      {
        "node_id": "n:d36789d76d9a",
        "short_label": "Table scan [t1]",
        "folded_label": "TABLE SCAN [t1] starts=1 rows=3000",
        "children": []
      }
    ]
  },
  "index_suggestions": [
    {
      "table": "t1",
      "columns": [
        "d"
      ],
      "ddl": "CREATE INDEX idx_t1_d ON t1 (d);",
      "reason": "Full scan on t1 with filter on (d)"
    }
  ],
  "primary_action": {
    "ref": "suggestions[0]"
  },
  "teach_hooks": [
    {
      "lesson": "nested_loop",
      "match": {
        "folded_label": "NESTED LOOP INNER starts=1 rows=3000",
        "short_label": "Nested loop inner join"
      },
      "controls": {
        "outer_rows": 1000,
        "inner_rows": 3000,
        "row_size": 200
      },
      "note": "Nested loop inner join"
    },
    {
      "lesson": "full_scan",
      "match": {
        "folded_label": "TABLE SCAN [t2] starts=1 rows=1000",
        "short_label": "Table scan [t2]"
      },
      "controls": {
        "rows": 1000,
        "row_size": 200,
        "selectivity": 100.0
      },
      "note": "Table scan on t2 (t2)"
    },
    {
      "lesson": "bnl",
      "match": {
        "folded_label": "TABLE SCAN [t1] starts=1 rows=3000",
        "short_label": "Table scan [t1]"
      },
      "controls": {
        "outer_rows": 3000,
        "inner_rows": 3000,
        "row_size": 200
      },
      "note": "Table scan on t1 (t1)"
    }
  ],
  "operator_complexities": [
    {
      "node_id": "n:4ee53c8ca1ad",
      "folded_label": "NESTED LOOP INNER starts=1 rows=3000",
      "short_label": "Nested loop inner join",
      "complexity": {
        "big_o": "O(n · m)",
        "short": "n · m",
        "severity": "bad",
        "rationale": "Unindexed nested loop: every outer row drives a full scan of the inner side. This is the classic O(n²)-class blow-up.",
        "confidence": "exact",
        "learn_more": "nested_loop_join"
      }
    },
    {
      "node_id": "n:7561bc6d0312",
      "folded_label": "TABLE SCAN [t2] starts=1 rows=1000",
      "short_label": "Table scan [t2]",
      "complexity": {
        "big_o": "O(n)",
        "short": "n",
        "severity": "medium",
        "rationale": "Full table scan: the storage engine returns every row; cost scales with the table size.",
        "confidence": "exact",
        "learn_more": "full_table_scan"
      }
    },
    {
      "node_id": "n:d36789d76d9a",
      "folded_label": "TABLE SCAN [t1] starts=1 rows=3000",
      "short_label": "Table scan [t1]",
      "complexity": {
        "big_o": "O(n)",
        "short": "n",
        "severity": "medium",
        "rationale": "Full table scan: the storage engine returns every row; cost scales with the table size.",
        "confidence": "exact",
        "learn_more": "full_table_scan"
      }
    }
  ]
}
