Speed — does it scale?
Brief 87

Query Performance & N+1 Audit

The database access patterns that fall over as data and traffic grow — N+1s, missing indexes, unbounded reads, and lock contention.

QUERIES.md4 phases7 lenses~2.6k chars

Free & open · no signup · read-only — it ends by asking · nothing leaves your machine

What it does

You are working inside this repo. Mission: find the data-access patterns that are fine on a laptop and fatal in production — the queries whose cost grows with rows, traffic, or both.

Read-only pass. Read the data-access code and schema; run read-only EXPLAIN/query logs if available; change nothing. Your only write is the report file.

What it looks for

7 lenses it looks through

Phase 2 sweeps the codebase through every one of these, citing file and line for each finding.

1

N+1 queries

a query per row; ORM lazy-loads inside a loop, serializer, or render

2

Missing indexes

filters, joins, and sorts on unindexed columns; full-table scans

3

Unbounded results

queries with no limit or pagination that grow with the table

4

Over-fetching

SELECT *, loading columns and relations never used, chatty round-trips

5

Hot-path cost

the queries on the busiest routes and the ones issued per request

6

Transactions & locking

long transactions, lock contention, N+1 writes

7

Caching gaps

identical reads repeated every request that never memoize or cache

The deliverable

What lands in your repo

One structured report at the repo root — or in reports/, if you keep one — the same shape every time, ready for a teammate — or the optional Studio — to act on.

QUERIES.md
1
Worst offenders
ranked by cost × frequency, with the query and call site
2
Findings
each: pattern · location · why it scales badly · the fix · effort
3
Index plan
the indexes to add, and the queries each serves
4
How to confirm
the measurement that proves each fix worked
One file. Evidence-backed. It ends by asking before touching anything.
How it works

What it makes the agent do

Every Goal Prompt follows the same four steps, so results are consistent and repeatable — no matter which one you run.

1
Phase 1

Find the hot paths

Identify the highest-traffic and most expensive endpoints and jobs.

2
Phase 2

Audit through 7 lenses

Give the query and its call site for every finding.

3
Phase 3

Curate

Rank by cost × frequency.

4
Phase 4

Report

Create QUERIES.md at repo root.

Get started

Three ways to run this Goal Prompt

01 · COPY

Paste it in

Copy it and paste it into your agent inside the repo you want checked.

02 · INSTALL

As a slash command

Install the goal plugin once — two commands — then just type /goal:query-performance.

1/plugin marketplace add GhostlyGawd/goal-prompts
2/plugin install goal@goal-prompts

Or install only this Goal Prompt as /goal-query-performance:

curl -fsSL https://goal-prompts.vercel.app/install | BRIEF=87 sh
03 · AGENT

From an agent (MCP)

Let an agent fetch it mid-conversation, or pull the raw Goal Prompt by URL.

https://goal-prompts.vercel.app/raw/87.md
Transparency

The exact prompt

Nothing hidden — this is the whole Goal Prompt, verbatim. Read it in a minute, edit it, or copy it as-is.

Read the full brief (2,569 characters)
# Goal: Query Performance & N+1 Audit

You are working inside this repo. Mission: find the data-access patterns that are fine on a laptop and fatal in production — the queries whose cost grows with rows, traffic, or both.

Read-only pass. Read the data-access code and schema; run read-only `EXPLAIN`/query logs if available; change nothing. Your only write is the report file.

## Phase 1 — Find the hot paths
- Identify the highest-traffic and most expensive endpoints and jobs.
- Trace how each reads data: raw queries, ORM calls, what is loaded eagerly vs lazily.
- Note the tables that grow without bound; queries against them age worst.

## Phase 2 — Audit through 7 lenses
Give the query and its call site for every finding.
1. **N+1 queries** — a query per row; ORM lazy-loads inside a loop, serializer, or render
2. **Missing indexes** — filters, joins, and sorts on unindexed columns; full-table scans
3. **Unbounded results** — queries with no limit or pagination that grow with the table
4. **Over-fetching** — `SELECT *`, loading columns and relations never used, chatty round-trips
5. **Hot-path cost** — the queries on the busiest routes and the ones issued per request
6. **Transactions & locking** — long transactions, lock contention, N+1 writes
7. **Caching gaps** — identical reads repeated every request that never memoize or cache

## Phase 3 — Curate
- Rank by cost × frequency: a cheap query on the hottest path can outweigh a slow rare one.
- For each, name the fix: an index, an eager-load, a batch, a bound, or a cache.
- Distinguish a code fix from a schema change; size each.

## Phase 4 — Report
Create `QUERIES.md` at repo root:
1. **Worst offenders** — ranked by cost × frequency, with the query and call site
2. **Findings** — each: pattern · location · why it scales badly · the fix · effort
3. **Index plan** — the indexes to add, and the queries each serves
4. **How to confirm** — the measurement that proves each fix worked

Start the report with today's date. If `QUERIES.md` already exists from a previous run, read it first and lead with what changed since.

## Rules
- Rank by cost × frequency, not raw query time
- An index is not free; justify each by the queries it serves
- No database queries in this repo? Say so in a one-paragraph null report and stop — a null result is a valid finding.
- If a `reports/` directory exists at the repo root, write the report there instead of the root.
- Before asking, present the top findings as a ranked list in plain words
- Report only — end by asking which queries to optimize first

House rules for this Goal Prompt

  • Rank by cost × frequency, not raw query time
  • An index is not free; justify each by the queries it serves
  • No database queries in this repo? Say so in a one-paragraph null report and stop — a null result is a valid finding.
  • If a reports/ directory exists at the repo root, write the report there instead of the root.
  • Before asking, present the top findings as a ranked list in plain words
  • Report only — end by asking which queries to optimize first
Keep exploring

Pairs well with

Curated neighbors — briefs that answer the adjacent question, worth running in the same session.

More Speed briefs

Runs inside these playbooks — curated sequences you can launch with one paste: