Prompt Engineering SQL MySQL

MySQL Query Optimization — InnoDB Realities, type=ALL

EXPLAIN shows type=ALL on a 1.2M-row table: full scan on every search. MySQL optimization with InnoDB clustering realities, version-aware optimizer limits, and covering reads.

Overview

MySQL optimization is version-and-engine work: advice that ignores InnoDB's clustering or the optimizer's version timeline misleads. This prompt carries the realities: use EXPLAIN ANALYZE (8.0+) or FORMAT=JSON because the table format hides attached conditions; the primary key is the clustered index and every secondary index carries it, so a big primary key taxes everything; the optimizer's limits are version-specific — no hash joins before 8.0.18, historically weak subquery optimization — so rewrites must be pinned to the version; and covering reads ("Using index" in Extra) separate index-only access from table reads, a large difference on busy tables. The loaded scenario is the classic type=ALL full scan on product search.

How to use this resource

  1. Get past the table format

    EXPLAIN ANALYZE or FORMAT=JSON — attached conditions and cost detail live there.

  2. Cost the clustering

    Every secondary index carries the primary key — index advice accounts for it.

  3. Pin advice to the version

    Hash joins, subquery handling, ICP — what the optimizer can do depends on the release.

Why This Works

  • Version-pinned advice prevents recommendations the engine cannot execute
  • InnoDB economics make index costs concrete instead of theoretical
  • The covering-read lens targets MySQL's biggest cheap win

Best for

  • MySQL 8 applications with search and listing queries
  • InnoDB tables whose primary keys grew wide
  • Teams inheriting version-mixed MySQL fleets

Not for

  • MariaDB specifics — its optimizer diverged; use the MariaDB platform mode
  • Generating test data for the queries — that's the Test Case Prompt Generator's world

Use cases

  • Eliminating type=ALL full scans on search queries
  • Designing secondary indexes around the InnoDB primary key
  • Rewriting subqueries the version's optimizer handles badly

FAQ

What happens if I run this MySQL prompt without pasting the EXPLAIN output?

It won't invent a plan. The QUERY CONTEXT says "Execution plan: not provided. Do not invent one," so the generated prompt's first recommendations become the exact commands to capture it — EXPLAIN ANALYZE (8.0+) or EXPLAIN FORMAT=JSON — and it marks every plan-dependent conclusion as pending. You gather that evidence, then run the completed analysis in your own assistant.

Why does the index advice keep mentioning the InnoDB primary key?

Because InnoDB clusters on it: "the primary key is the clustered index and every secondary index carries it — a big primary key taxes every index." So a new secondary index for the type=ALL product search costs the primary key's width on each entry, plus a write tax on every insert. The prompt makes that economics explicit; you verify with EXPLAIN before and after on production-shaped data.

Is this MySQL prompt usable as-is on a MariaDB database?

Not as-is — the notFor line says MariaDB's optimizer diverged and points to the MariaDB platform mode. This prompt pins advice to MySQL specifics like "no hash joins before 8.0.18" and covering reads shown as "Using index" in Extra, which don't transfer cleanly. Regenerate with the MariaDB platform selected so the version-and-engine assumptions match what your database can execute.

More resources from SQL Optimization Prompt

Resources that pair well

Related tools

Tip: Save time by exploring related resources and tools that integrate with this resource.