query-optimization-specialist

Analyze PostgreSQL EXPLAIN plans to identify missing indexes and slow queries.

Updated Aug 27, 2026
One-click install
npx skills add https://github.com/Whaleylaw/llm-lawyer --skill query-optimization-specialist
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: query-optimization-specialist
Source: https://github.com/Whaleylaw/llm-lawyer/tree/main/.claude/skills/query-optimization-specialist
Command: npx skills add https://github.com/Whaleylaw/llm-lawyer --skill query-optimization-specialist

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Identifies and resolves slow or inefficient queries in PostgreSQL/Supabase by diagnosing execution plans, locating missing or suboptimal indexes, correcting query patterns, and recommending schema or query changes to reduce latency and resource usage.

Core Features & Use Cases

  • Plan Analysis: Interpret EXPLAIN and EXPLAIN ANALYZE output to find sequential scans, inaccurate row estimates, and expensive operations like large sorts or nested loops.
  • Indexing & Schema Guidance: Recommend single-column, composite, partial, or covering indexes and advise on appropriate data types to eliminate unnecessary casts.
  • Query & Application Fixes: Detect N+1 patterns, suggest JOIN/EXISTS rewrites, optimize aggregations with indexes or materialized views, and validate changes with re-run plans.
  • Use Case: When a Supabase-backed list view is slow, run plan analysis to add the proper composite index for the WHERE and ORDER BY columns and verify improved latency.

Quick Start

Run EXPLAIN ANALYZE on the slow query and provide the full plan, schema of involved tables, and recent statistics so the plan can be analyzed and index or query recommendations returned.

Frequently Asked Questions about query-optimization-specialist

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

FAQPage Schema
How do I diagnose slow PostgreSQL queries using EXPLAIN ANALYZE?

Diagnose slow PostgreSQL queries by running EXPLAIN ANALYZE to capture execution plans, then analyze the output to identify sequential scans, inaccurate row estimates, and expensive operations like large sorts or nested loops to determine the root cause of latency.

What is the best way to fix N+1 query patterns in Supabase?

Fix N+1 query patterns in Supabase by detecting the repetitive queries and rewriting them into JOIN or EXISTS statements, reducing the number of database round trips and significantly lowering execution latency for transactional operations.

When do I need to add a composite index in PostgreSQL?

Add a composite index in PostgreSQL when EXPLAIN ANALYZE reveals sequential scans on queries filtering or sorting by multiple columns, allowing the database to use an index scan instead of sorting large datasets for your WHERE and ORDER BY clauses.

How do I optimize expensive JOINs and aggregation operations in PostgreSQL?

Optimize expensive JOINs and aggregation operations by analyzing execution plans to identify bottlenecks, then implementing covering indexes or rewriting the query with materialized views to reduce resource usage and improve execution times.

Does this query optimization approach work for both transactional and analytical PostgreSQL workloads?

Yes, this query optimization approach works for both transactional and analytical PostgreSQL workloads, diagnosing high latency, sequential scans, and expensive JOINs or aggregation operations by analyzing execution plans and suggesting schema changes across both environments.