Prompt Engineering SQL Performance

Slow Query Investigation — Degrading Month Over Month

Queries that were instant at launch and crawl a year later: scale-aware investigation of work that grows faster than the data, before it owns the server.

Overview

Some queries do not have a bug — they have a trajectory. This prompt uses the scaling goal on the classic case: dashboard queries degrading every month as data grows. The priorities target growth itself: reason at the scale every conclusion assumes (fine at 100K rows can own the server at 100M), identify work that grows faster than the data — join shapes that multiply, sorts that will spill, aggregates over ever-growing history — bound the working set with time-windowing and keyset pagination, and check growth asymmetry, because the right plan today may invert as table ratios change. The symptom framing matters: degradation correlated with data growth is itself evidence about the cause's shape.

How to use this resource

  1. Read the trajectory as evidence

    Month-over-month degradation points at growth-coupled work — the symptom narrows the suspects.

  2. Find the super-linear work

    What grows faster than the data: multiplying joins, spilling sorts, unbounded aggregates.

  3. Bound the working set

    Time windows, keyset pagination, pruning — queries that touch a bounded set stop degrading.

Why This Works

  • Trajectory-as-evidence thinking separates growth problems from regressions
  • The super-linear lens finds what will fail, not just what is slow today
  • Bounded working sets fix the class of problem, not the instance

Best for

  • Dashboards and feeds over growing tables
  • Queries whose slowness correlates with data volume
  • Teams who keep "fixing" the same query every quarter

Not for

  • A query that got slow suddenly overnight — check deployments first with the Debugging Prompt Generator
  • One-off reporting runs — that's the Reporting Query Optimization goal

Use cases

  • Investigating dashboards that slow a little every month
  • Finding the operation that grows faster than the data
  • Bounding queries that read ever-growing history

FAQ

Should I use this if my query got slow suddenly overnight?

That's the wrong fit. This prompt targets the large-dataset-scaling goal — queries that degrade month over month as data grows, reading the trajectory itself as evidence. A sudden overnight regression points at a deployment, which the resource routes to the Debugging Prompt Generator. Use this one when slowness correlates with volume, not with a recent change.

Why does the prompt keep telling me it can't conclude without the execution plan?

Its QUERY CONTEXT rules say the plan is "not provided. Do not invent one," so plan-dependent conclusions are marked pending until you capture it with your engine's actual-execution-plan facility. Same for row counts and existing indexes. When essential evidence is missing, the first recommendations become the commands to gather it rather than guesses standing in for it.

What does 'work that grows faster than the data' mean in this prompt?

It's the super-linear lens: the analysis hunts join shapes that multiply, sorts that will spill, and aggregates over ever-growing history — operations whose cost outpaces row growth. It also checks growth asymmetry (which tables grow, which stay constant) because the right plan today can invert as ratios change, and recommends bounding the set with keyset pagination instead of OFFSET.

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.