Prompt Engineering SQL Scaling

SQL Performance Analysis — Surviving the Billion-Row Table

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.

Overview

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.

How to use this resource

  1. Name the target scale

    Every conclusion states the row count it assumes — one billion is the design point, not today's 240M.

  2. Kill OFFSET before it kills you

    Deep-page OFFSET cost grows with depth; keyset pagination holds constant.

  3. Prune instead of scan

    Partitioning evaluated by what queries can actually prune — not by partitioning fashion.

Why This Works

  • Target-scale reasoning catches what passes today and fails next year
  • Keyset-over-OFFSET is the highest-value pagination fix at scale
  • Pruning-based evaluation keeps partitioning honest about its wins

Best for

  • Append-heavy tables: audit logs, events, time series
  • Systems whose data outgrew their original query patterns
  • Capacity planning grounded in query mechanics

Not for

  • Small-table tuning where scale is not the issue — that's the Query Speed goal
  • Refactoring the application's data access layer — that's the Refactor Prompt Builder

Use cases

  • Preparing audit and event tables for the next order of magnitude
  • Replacing OFFSET pagination before deep pages time out
  • Evaluating partitioning with honest pruning analysis

FAQ

Does this prompt need my execution plan and row counts before it recommends anything?

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.

Why does it push keyset pagination over OFFSET for a large table?

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.

Is this the right prompt for tuning a small, slow query?

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.

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.