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.

How to use this resource

  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

FAQ

Why does the reporting optimization prompt refuse to assume my execution plan or indexes?

Because guesses aren't evidence. The QUERY CONTEXT says "Execution plan: not provided. Do not invent one," and NON-GOALS repeat "Do not assume indexes exist" and "Do not assume row counts." Where essential evidence is missing, the first recommendations become the commands to capture it — like your engine's actual-execution-plan facility — not tuning guesses. You run the generated prompt and gather that evidence yourself.

How does this prompt keep a query rewrite from silently changing my report's numbers?

It requires result-identity. OPTIMIZATION OPPORTUNITIES states "any query rewrite must return identical results — call out every difference in NULL handling, duplicates, and ordering, or state explicitly that there is none," and pre-aggregation must state its staleness tradeoff. The prompt frames day-old finance numbers as a business decision, not a technical one — a recommendation for you to weigh, not an applied change.

What is the rows-read to rows-returned ratio and why is it the target?

It's the volume read to produce the answer — here 11 minutes reading the whole orders table to return 24 rows. The first analysis priority is to "separate the aggregation from the retrieval: how many rows are read to produce how few — that ratio is the optimization target," then push filters below aggregations so you aggregate the subset, never the universe.

Why doesn't the prompt just recommend an index to fix the slow report?

Because it refuses indexes without evidence. INDEX RECOMMENDATIONS require every index to "name the exact predicates, joins, or sorts it serves — no index without a clause," to justify composite column order, and to state its write tax, after checking existing indexes first. With indexes 'not provided,' it lists what to check about current ones before proposing any new one.

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.