SkillVaultskills Browse all 500 skills

Data · Version 2.0.0 · Reviewed 2026-08-02

Database Query Optimizer

Make data systems more correct and operable for EXPLAIN plan analysis and index design with evidence, explicit trade-offs, and a verification plan.

4 method steps 7 documented failure modes 5 diagnostic checks 7 quality gates

Explains query plans and recommends index, join, pagination, and schema changes with workload-aware trade-offs.

₹99 one-time

Get this skill archive

What this skill helps you do

  • EXPLAIN plan analysis
  • Index design
  • Lock and contention diagnosis

How Database Query Optimizer works

You provide

EXPLAIN ANALYZE output, schema, and the real workload

It inspects

Estimate-versus-actual divergence, sorts, and buffer reads

It decides

Index or access-path change with its write-cost trade-off

You verify

Re-measure the plan and compare total execution time

What it checks first

Database Query Optimizer explains query plans and recommends index, join, pagination, and schema changes with workload-aware trade-offs. Use it when the work involves EXPLAIN plan analysis, Index design, Lock and contention diagnosis.

  1. Estimated versus actual row counts in the plan: a divergence above roughly 100x means statistics or correlation assumptions are wrong, and every downstream join choice is suspect.
  2. The presence of a Sort node whose input could have been provided in order by an index — this is the usual cause of pagination collapse.
  3. `Rows Removed by Filter` on an index scan, which shows the index found the rows but the predicate could not be pushed down.
  4. Buffers: `shared read` versus `shared hit` separates a cache-miss problem from a genuine plan problem.
  5. Whether the query is latency-bound on one execution or throughput-bound across concurrency — they have opposite fixes.

Failure modes it recognizes

  • A composite index that satisfies the filter but not the ORDER BY, forcing the planner to fetch and sort the entire matching set for a LIMIT query.
  • OFFSET pagination degrading linearly, because the database still materializes and discards every skipped row.
  • A function applied to an indexed column (`WHERE lower(email) = ...`) silently disabling the index in favor of a sequential scan.
  • Implicit type coercion between a `bigint` column and a numeric literal preventing index use.
  • Correlated subqueries executing per row after the planner loses the ability to flatten them.
  • Stale statistics after a bulk load, where the planner still believes the table is small.
  • A partial index whose predicate does not provably cover the query predicate, so it is never chosen.

Answers it will reject

  • Adding one index per slow query, which grows write amplification and lock contention until inserts become the new bottleneck.
  • Reading only `EXPLAIN` output: without `ANALYZE, BUFFERS` the row counts are estimates and prove nothing about real cost.
  • Concluding "the index is not used" when the table is small enough that a sequential scan is genuinely cheaper.
  • Recommending `CREATE INDEX` on a production table without `CONCURRENTLY`, which takes a write-blocking lock.

Decision rules it applies

  • Order composite index columns by equality predicates first, then the range or sort column — the sort column must come last to be usable for ordering.
  • Replace OFFSET with keyset pagination (`WHERE (sort_key, id) < (:last_key, :last_id)`) whenever the offset can exceed a few thousand rows.
  • Only add an index if it serves a class of queries; a single-query index is a maintenance liability.
  • If estimates are accurate but the plan is still slow, the problem is data volume or access pattern, not the planner — redesign the access path.

Evidence it asks for

  • `EXPLAIN (ANALYZE, BUFFERS, VERBOSE)` to get actual rows, loop counts, and I/O attribution.
  • `pg_stat_statements` ordered by `total_exec_time` to find the query that dominates load rather than the query that feels slow.
  • `pg_stat_user_indexes.idx_scan` to identify indexes that cost writes but serve no reads.
  • `ANALYZE <table>` after bulk changes, and check `last_autoanalyze` in `pg_stat_user_tables`.

The method inside

  1. Define the measured baseline and user-visible target for EXPLAIN plan analysis.
  2. Attribute the dominant cost or latency mechanism affecting index design.
  3. Rank lock and contention diagnosis changes by expected impact, confidence, effort, and regression risk.
  4. Validate under representative load and retain guardrail metrics that detect a shifted bottleneck.

Deliverables

  • EXPLAIN plan analysis assessment
  • Index design decision and action plan
  • Lock and contention diagnosis verification checklist

Evidence requirements

  • Schema, access patterns, query plans, or event contracts
  • Volume, cardinality, retention, and freshness
  • Consistency, latency, and migration constraints

Quality gates

  • Every material claim traces to supplied evidence or is labeled as a hypothesis.
  • The response follows the declared deliverable contract.
  • No execution, access, measurement, or verification is invented.
  • Secrets and personal data are redacted rather than repeated.
  • The user receives a concrete independent verification step.
  • The relevant failure modes in this domain were considered rather than only the reported symptom.
  • No listed anti-pattern was recommended as a solution.

Example task

Input

This PostgreSQL query scans 40 million orders to return the newest 50 for one customer. Review the plan.

Expected output

The planner cannot satisfy both the customer filter and created_at order from the current indexes. Add a composite index on (customer_id, created_at DESC)...

Boundaries and compatibility

Ideal for

  • EXPLAIN plan analysis: produce a decision or artifact grounded in supplied evidence.
  • Index design: produce a decision or artifact grounded in supplied evidence.
  • Lock and contention diagnosis: produce a decision or artifact grounded in supplied evidence.

Out of scope

  • Recommending indexes without a workload
  • Treating eventual consistency as universally acceptable

Agent compatibility

  • GitHub Copilot custom agents
  • Claude Agent Skills / SKILL.md
  • Any instruction-following chat model

Tool policy: Advisory by default. No tools are assumed. If the host provides tools, use read-only evidence gathering unless the user explicitly approves a scoped write or execution action.