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.
At 240M rows and growing: keyset pagination over OFFSET, partition pruning, and the PostgreSQL specifics — analysis aimed at the scale the table will reach, not the scale it passed.
Performance analysis at large scale is a different discipline: what matters is not today's timing but the curve it sits on. This prompt configures the scaling goal on PostgreSQL for an audit-log table heading to a billion rows: reason at target scale explicitly, identify the worse-than-linear work, replace OFFSET pagination — whose cost grows with page depth — with keyset pagination, and evaluate partition pruning where the platform offers it. The PostgreSQL guidance grounds it: EXPLAIN (ANALYZE, BUFFERS) for the evidence, BRIN indexes as the append-only table's cheap friend, bloat and autovacuum state at volumes where they dominate, and parallelism checked rather than assumed.
Name the target scale
Every conclusion states the row count it assumes — one billion is the design point, not today's 240M.
Kill OFFSET before it kills you
Deep-page OFFSET cost grows with depth; keyset pagination holds constant.
Prune instead of scan
Partitioning evaluated by what queries can actually prune — not by partitioning fashion.
It works without them but won't guess in their place. The QUERY CONTEXT states "Execution plan: not provided. Do not invent one," and marks every plan-dependent conclusion as pending EXPLAIN (ANALYZE, BUFFERS); row counts and indexes are likewise not assumed. Where evidence is missing, the first recommendations are the commands to gather it. The sql-optimization-prompt builds this; you run it in your assistant and supply the plan for firm conclusions.
Because the symptoms note that "pagination uses OFFSET and deep pages time out." Analysis priority 3 bounds the working set with "keyset pagination instead of OFFSET." Any rewrite it proposes "must return identical results," calling out NULL handling, duplicates, and ordering differences. It's an evidenced recommendation for you to review and test on production-shaped data, not a guaranteed fix.
Not really — this configures the Large Dataset Scaling goal, reasoning toward one billion rows for an append-only audit-log table already at 240M. Its priorities are worse-than-linear work, spill risk, and partition pruning. Per the notFor, a small table where scale isn't the issue fits the Query Speed goal better, and refactoring the application's data-access layer is the Refactor Prompt Builder's territory.
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.