SQL Optimization Patterns

Master SQL query optimization, indexing strategies, and EXPLAIN analysis to dramatically…

An EXPLAIN-driven toolkit for turning slow database queries into fast ones. It teaches you to read query plans, pick the right index type for each access pattern, kill N+1 queries, replace OFFSET pagination with cursors, and pre-compute heavy reports with materialized views, backed by real PostgreSQL examples and monitoring queries that surface the costliest 20% of your workload.

$15 one-time
Add to a kit →

Prices include 20% VAT. · Forged on real agency work · one-time, no lock-in

  • Type Skill
  • Category Data & Analytics
  • Delivery Email · instant
  • License One-time
Run preview
forgehouse, sql-optimization-patterns

Inside the run · no black box

See the actual work before you buy it.

Evidence before edits: roughly 20 percent of queries generate 80 percent of database load, so the loop starts at pg_stat_statements, reads the EXPLAIN plan, applies the matching index, and re-measures until the plan flips.

  1. Pull the top offenders from pg_stat_statements ordered by mean_time, because roughly 20 percent of queries generate 80 percent of database load; those are the only ones worth touching first.
  2. Run EXPLAIN (ANALYZE, BUFFERS) on each candidate and read the plan: a Seq Scan on a big table, a Nested Loop with exploding row counts, or estimated vs actual rows off by 10x each point to a different fix.
  3. Apply the matching index, not a generic one: composite B-Tree with the leftmost column rule in mind, partial index for hot subsets like active rows, GIN for JSONB, arrays and full-text, covering index to unlock index-only scans.
  4. Rewrite the query shape where indexes alone cannot help: correlated subqueries become JOINs with aggregation, OFFSET pagination becomes keyset (cursor) pagination, N+1 loops collapse into a single IN or JOIN batch.
  5. Push genuinely expensive aggregates into a materialized view refreshed CONCURRENTLY, with the refresh schedule matched to staleness tolerance: dashboard minutes, reporting hours.
  6. Re-run EXPLAIN ANALYZE to confirm the plan flipped to index scans, then run ANALYZE on the touched tables so planner statistics stay honest and the win does not silently regress.
Use cases · what happens when you plug it in

One power source. 6 lines out.

sql-optimization-patterns · core

core active · 6 lines

  1. Debug slow queries with EXPLAIN ANALYZE

    ✓ debug slow queries with
  2. Choose between B-Tree, GIN, GiST, and partial indexes

    ✓ choose between b-tree, gin
  3. Eliminate N+1 queries with joins or batch loads

    ✓ eliminate n+1 queries with
  4. Replace OFFSET pagination with keyset cursors

    ✓ replace offset pagination
  5. Pre-compute reports with materialized views

    ✓ pre-compute reports with
  6. Find missing and unused indexes from pg_stat

    ✓ find missing and unused
Benefits · what you walk away with

Yours to keep.

Drag time forward. Watch what stays.

Forever

That's what owning means.

The rented stack

ai writing tool: subscription

expired · access lost

analytics suite: subscription

expired · access lost

design platform: subscription

expired · access lost

(nothing left)

Your forge

  1. Make page loads faster by cutting query latency

    license: perpetual
  2. Lower database hosting and storage costs

    license: perpetual
  3. Find the costly 20% of queries causing 80% of load

    license: perpetual
  4. Scale to growing datasets without full table scans

    license: perpetual

subscriptions expire · deeds don't

What's included · the full manifest

Everything in the box.

Pick a piece up. Watch it work.

EXPLAIN plan reading guide (Seq Scan, Hash Join, costs)

part 01 of 06 · in the box

6 parts · one working system · ships instantly by email

Who it's for

This wasn't forged for everyone.

  • Not for you if you'd rather rent a tool than own one.
  • Not for you if you want someone else to run your stack.
  • Not for you if you're happy guessing.
Still here? Good.

Backend and data engineers fighting slow queries who want measurable performance gains and lower database costs at scale.

then this was forged for you.

Works with

Universal by design: these run in any AI. Delivered in the open Agent Skills + MCP format (native in Claude); ChatGPT, Gemini, Cursor and Copilot adapt the same files their own way.

  • Claude Native format
  • ChatGPT Adapts via open standards
  • Gemini Adapts via open standards
  • Cursor Adapts via open standards
  • Copilot Adapts via open standards
Questions · still in the air

Catch what's on your mind.

the air is clear. nothing between you and the forge.
catch a spark: the forge will answer

  1. Is this PostgreSQL-only, or can I use it with MySQL or SQL Server?

    The examples, index types (GIN, GiST, partial), and monitoring queries are PostgreSQL, built on pg_stat_statements and EXPLAIN ANALYZE. The reasoning transfers, N+1 elimination, keyset pagination, and materialized views exist everywhere, but the copy-paste tooling assumes Postgres.

  2. Why not just add indexes to every column that shows up in a WHERE clause?

    Because every index slows writes and consumes storage, and many never get used. The workflow is EXPLAIN-driven: read the plan first, match the index type to the actual access pattern, and use the pg_stat queries to find both missing and unused indexes, so you index the costly 20% of the workload instead of everything.

  3. Will it optimize my queries automatically?

    No. It teaches you to read query plans and gives you rewrite patterns for N+1, pagination, aggregates, and subqueries, but you run the EXPLAIN, interpret the plan, and apply the rewrite. There is no agent that patches SQL behind your back.

  4. How is it delivered?

    By email right after purchase: ready to run, downloaded instantly, no setup wait.

  5. One-time or subscription?

    A one-time purchase; no subscription or hidden fees. VAT (20%) is included.

  6. Can I get a refund?

    As a digital product, it can’t be refunded once downloaded. That’s why we show exactly what’s inside and who it’s for, right here.