query-optimization

Diagnose slow SQL queries by interpreting EXPLAIN ANALYZE execution plans.

1|Updated May 6, 2026
One-click install
npx skills add https://github.com/jacob-balslev/skill-graph --skill query-optimization-jacob-balslev
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: query-optimization
Source: https://github.com/jacob-balslev/skill-graph/tree/main/marketplace/skills/query-optimization
Command: npx skills add https://github.com/jacob-balslev/skill-graph --skill query-optimization-jacob-balslev

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps you diagnose why a specific relational database query is slow and choose the right corrective action instead of guessing, by interpreting the query planner’s chosen execution plan.

Core Features & Use Cases

  • Plan-first diagnosis with EXPLAIN/EXPLAIN ANALYZE: Identifies the most expensive plan node and checks estimated vs actual cardinalities to locate the root cause.
  • Query-planner mental model and plan-node taxonomy: Interprets common plan-node types (Seq Scan, Index Scan, Bitmap Heap Scan, Nested Loop, Hash Join, Sort, Hash Aggregate, Materialize, etc.) in terms of cost and behavior.
  • Response catalog tied to diagnosis: Selects the appropriate fix among query rewriting, refreshed/raised statistics (ANALYZE, statistics targets, extended statistics), index-related changes, and other operational options.

Quick Start

Use the query-optimization skill to read the output of EXPLAIN ANALYZE for your slow SQL statement and determine which plan node and diagnosis explain the runtime, then apply the matching response (rewrite, statistics, or index-related tuning).

Frequently Asked Questions about query-optimization

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I diagnose a slow SQL query using its execution plan?

Diagnose a slow SQL query by running EXPLAIN ANALYZE to inspect the execution plan, identifying the most expensive plan node, and comparing estimated versus actual cardinalities to locate the root cause of the performance issue.

What does a Seq Scan plan node mean in EXPLAIN ANALYZE output?

A Seq Scan plan node in EXPLAIN ANALYZE output indicates the database performs a sequential scan reading the entire table, which often causes high runtime costs compared to an Index Scan or Bitmap Heap Scan when filtering large datasets.

How do I fix bad cardinality estimation when the query planner chooses slow plans?

Fix bad cardinality estimation by refreshing or raising statistics with ANALYZE, adjusting statistics targets, or creating extended statistics to provide the query planner accurate data distribution for generating faster execution plans.

When should I rewrite SQL versus adding an index to improve query performance?

Decide between rewriting SQL or adding an index by examining the execution plan node: if the planner picks the correct access path but the query structure is inefficient, rewrite it; if it uses a Seq Scan where an Index Scan fits, add an index.

What's the best way to verify that SQL query optimization changes actually improved performance?

Verify SQL query optimization improvements by re-running EXPLAIN ANALYZE after applying your corrective action, comparing the new execution plan runtime statistics and costs against the original baseline to confirm the performance gain.

How do Nested Loop and Hash Join plan nodes affect SQL performance differently?

Nested Loop plan nodes execute efficiently for small datasets by iterating rows, while Hash Join nodes build a hash table for larger inputs, and misestimation by the planner between them causes severe SQL performance degradation.