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.
A real plan with the story inside: estimate says 42 rows, actual is 51,840. PostgreSQL analysis grounded in the plan — stale statistics, bloat, planner costs, and the index arsenal.
PostgreSQL analysis starts with the right plan command: EXPLAIN (ANALYZE, BUFFERS) — plain EXPLAIN cannot show actual rows, loops, or buffer traffic. This prompt loads a real plan whose story is in the numbers: a Seq Scan with estimate 42 rows against actual 51,840, and nearly 8M rows removed by the filter — the statistics-staleness signature that invalidates every downstream planner choice. The forensic evidence mode requires every conclusion to cite its operator by name, and the platform guidance brings the PostgreSQL-specific checklist: autovacuum and bloat state, random_page_cost on SSDs, partial and expression indexes where evidence supports them, and whether the query shape blocks parallelism.
Capture the right plan
EXPLAIN (ANALYZE, BUFFERS) — actuals and buffer traffic, not estimates alone.
Read the gaps
Estimate 42 vs actual 51,840 is not noise — it is the planner working from a false map.
Fix causes, not symptoms
Stale statistics and bloat get fixed before index advice — the plan may fix itself.
Plain EXPLAIN can't show actual rows, loops, or buffer traffic — only estimates. The DATABASE CONTEXT demands EXPLAIN (ANALYZE, BUFFERS) so the analysis can read the estimate-42-vs-actual-51,840 gap that flags stale statistics. The sql-optimization-prompt generates this analysis prompt; you still run the EXPLAIN yourself and paste the verbatim plan for it to read.
It's built to refuse that. The QUERY CONTEXT says existing indexes are "not provided — do not assume any index exists," so under Forensic Optimization mode, where evidence is missing the output emits the exact command to gather it instead of a guess. Every recommendation must cite its operator, row count, or schema fact by name.
Not well — the platform is set to PostgreSQL, and the guidance is Postgres-specific: autovacuum and bloat, random_page_cost on SSDs, and partial, expression, or BRIN indexes. notFor states operator semantics and tooling differ by platform for MySQL or SQL Server. Regenerate in the sql-optimization-prompt with the right platform so the checklist matches your engine.
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.