Optimize SQL Query — Joins, Cardinality, and Wasted Work
A five-table join that got slow as data grew: establish cardinality first, check each join strategy against the data shape, and hunt the fan-out doing wasted work.
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.
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.
Get past the table format
EXPLAIN ANALYZE or FORMAT=JSON — attached conditions and cost detail live there.
Cost the clustering
Every secondary index carries the primary key — index advice accounts for it.
Pin advice to the version
Hash joins, subquery handling, ICP — what the optimizer can do depends on the release.
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.
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.
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.
A five-table join that got slow as data grew: establish cardinality first, check each join strategy against the data shape, and hunt the fan-out doing wasted work.
"Optimize this query" gets generic indexing advice. The optimization contract demands evidence: real bottlenecks, justified indexes with their write tax, and no invented plans.
Optimize what the engine does, not what the SQL looks like: cost concentration, estimate-vs-actual gaps, and plan warnings — with forensic evidence rules.
"Fix this error" gets guesses. The investigation contract gets a ten-stage diagnosis: facts separated from assumptions, alternatives weighed, fixes justified.
"Review this code" gets shallow comments. The review contract gets findings with severities, a checklist, and a verdict.
"Refactor this code" invites silent behavior changes. The refactoring contract preserves business rules, outputs, and side effects — and flags uncertainty instead of deciding it.
Build evidence-based SQL optimization prompts — goal, platform, and the evidence you have turn into a query tuning contract.