Prompt Engineering SQL Reporting

Optimize Reporting Queries — Read Millions, Return Rows

An 11-minute report that reads two years of orders to return 24 rows: the rows-read to rows-returned ratio is the target — filters pushed down, pre-aggregation costed honestly.

Overview

Reporting queries have their own physics: the question is rarely the join, it is the sheer volume read to produce a small answer. This prompt uses the reporting goal: separate the aggregation from the retrieval and target the rows-read to rows-returned ratio; push filters below aggregations wherever semantics allow — aggregate the subset, never the universe; evaluate pre-aggregation honestly (materialized or indexed views, summary tables, incremental aggregation) with the staleness tradeoff stated, because finance deciding on day-old numbers is a business question, not a technical one; and check the grouping mechanics — whether an index can feed the groups already in order.

Workflow

  1. Measure the ratio

    Rows read versus rows returned — an 11-minute, 24-row report states its own problem.

  2. Aggregate the subset

    Filters pushed below the aggregation wherever semantics allow — never aggregate the universe.

  3. Cost the staleness

    Pre-aggregation buys speed with freshness — the tradeoff is stated, and the business decides.

Why This Works

  • The ratio framing locates reporting cost where it actually lives
  • Pushdown discipline is the highest-leverage safe rewrite for aggregates
  • Honest staleness costing keeps pre-aggregation from being a silent correctness change

Best for

  • Finance, analytics, and BI query workloads
  • Reports run daily that aggregate years of history
  • Aggregation queries on OLTP databases

Not for

  • Interactive dashboard queries degrading with growth — that's the scaling investigation
  • Documenting the report's business logic — that's the Code Explanation Prompt's territory

Use cases

  • Speeding up the monthly report that reads everything
  • Deciding between summary tables and materialized views
  • Pushing filters below aggregations safely

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

Explore all resources