PromptFork

Diagnose and fix a slow SQL query using EXPLAIN ANALYZE like a DBA

Paste a slow query and get a root-cause diagnosis, targeted index/rewrite fixes, and a before/after query plan — with specific pattern detection for N+1s, implicit casts, and composite index gaps.

Open in Studio
Prompt
Act as a database performance engineer (Postgres unless I say otherwise). I'll give you a slow query, the table schemas, and optionally the EXPLAIN ANALYZE output.

Before proposing fixes, scan for these specific anti-patterns:
- N+1 queries disguised as correlated subqueries or lateral joins — suggest batch/CTE alternatives.
- Missing COMPOSITE indexes (queries filtering on A + ordering by B need a composite (A, B) index, not two single-column indexes).
- Function calls on indexed columns that prevent index usage (e.g., WHERE LOWER(email) = ... needs a functional index or generated column).
- Implicit type casting that silently kills index scans (e.g., comparing varchar to int).
- SELECT * pulling columns not needed by the query, bloating I/O.
- Large IN() lists that should be rewritten as joins against a VALUES list or temp table.
- ORDER BY + LIMIT without a supporting index (causes full sort then discard).

Then do this:
1. Read the plan (or infer one) and name the ACTUAL bottleneck — seq scan, nested loop on unindexed join, sort spill to disk, hash join with bad estimate, etc. Explain WHY it's slow in one sentence a junior could understand.
2. Propose the smallest set of changes — most impactful first. For each:
   - Exact CREATE INDEX statement (include CONCURRENTLY for production).
   - Query rewrite (if needed) with a one-line explanation of what changed.
   - Schema tweak (if needed) with migration safety note.
3. Show the fully rewritten query.
4. Draw the expected query plan BEFORE and AFTER as a simple text diagram:
   ```
   BEFORE: Seq Scan on orders (cost=0..45000)
     → Filter: status = 'active' (removes 95% of rows)
   
   AFTER:  Index Scan using idx_orders_status on orders (cost=0..150)
     → Index Cond: status = 'active'
   ```
5. State tradeoffs honestly: write amplification, index storage cost, lock duration for migrations.
6. Flag anything that needs real data distribution (selectivity, cardinality) to decide — don't guess.

Query: [PASTE]
Schema: [PASTE]
EXPLAIN ANALYZE: [PASTE OR OMIT]
Source
promptfork seed
License
CC-BY-4.0
Published
6/23/2026

More prompts you might like

Next.js 15 App Router page with streaming, caching, and server data

Scaffold a production App Router page: Server Component data fetching, Suspense streaming for instant TTFB, correct cache strategy (fetch cache vs unstable_cache vs revalidatePath), loading/error boundaries, and generateMetadata — with the non-obvious patterns most tutorials skip.

New

Tailwind analytics dashboard with animated stat cards, dark mode, and skeleton loading

Production-grade dashboard layout: KPI cards with counting animations and trend sparklines, a chart area, activity table — all with dark mode, skeleton loading states, and responsive breakpoints defined to the pixel.

New

Supabase RLS: owner-write, public-read policies for a table

Generate correct, non-recursive RLS policies so anyone reads published rows and only owners edit their own.

New

Stripe webhook handler with signature verify + idempotency

Production-ready Stripe webhook route that verifies the signature and processes events exactly once.

New

.cursorrules for a strict TypeScript + React codebase

A tuned .cursorrules file that keeps Cursor's agent on-convention: strict types, no dead code, match existing patterns.

New

Refactor a React component into clean custom hooks

Extract stateful logic from a bloated component into typed, testable custom hooks.

New