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.

Workflow

  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

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

Explore all resources