query-optimization

Diagnose slow SQL queries and design indexes using execution plan analysis.

Updated Sep 10, 2026
One-click install
npx skills add https://github.com/serpro-workshop-fortaleza/datacorp-sifap-modernization-team-kit --skill query-optimization-serpro-workshop-fortaleza
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: query-optimization
Source: https://github.com/serpro-workshop-fortaleza/datacorp-sifap-modernization-team-kit/tree/main/.github/skills/query-optimization
Command: npx skills add https://github.com/serpro-workshop-fortaleza/datacorp-sifap-modernization-team-kit --skill query-optimization-serpro-workshop-fortaleza

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Slow database queries are hard to fix without a systematic method. This Skill provides a structured diagnostic workflow for investigating slow queries, reading EXPLAIN plans, and designing justified indexes instead of guessing. ## Core Features & Use Cases - Diagnostic Workflow: Capture a baseline (p50/p95 latency, rows examined), obtain the execution plan, identify common causes like sequential scans, stale statistics, and nested loop issues, then propose the smallest fix. - Index Design Heuristics: Apply the ESR rule (equality, sort, range), covering indexes with INCLUDE columns, and partial indexes for skewed data, while justifying each index against its write cost. - Antipattern Detection: Flag SELECT * in hot paths, functions on indexed columns in WHERE clauses, and ORM N+1 problems. - Use Case: A PostgreSQL query scans a 10-million-row table. Use this Skill to capture EXPLAIN (ANALYZE, BUFFERS), identify the missing index, create it with CREATE INDEX CONCURRENTLY, and validate the plan change. ## Quick Start Ask the AI to review this slow query and its EXPLAIN ANALYZE output and propose the smallest optimization.

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?▼

Capture a baseline first: p50/p95 latency, rows examined, and rows returned. Then get the execution plan with EXPLAIN (ANALYZE, BUFFERS) on PostgreSQL, EXPLAIN ANALYZE FORMAT=JSON on MySQL 8, or SET STATISTICS IO, TIME ON in SQL Server, and look for common causes like sequential scans or bad row estimates.

How to design a composite index for a SQL query?▼

Follow the ESR rule: place equality columns first, then range columns, then sort columns. For read-heavy queries, add INCLUDE columns to create a covering index that avoids heap lookups, and justify every index against its write cost.

Why is my query not using the index?▼

Common causes include applying a function to the indexed column in the WHERE clause, stale table statistics causing row estimate errors above 10x, or a predicate that is not selective enough. Run ANALYZE to refresh statistics or use an expression index for computed predicates.

Does adding an index fix ORM N+1 query problems?▼

No. N+1 problems come from the ORM issuing one query per related record, so they must be fixed in the ORM with eager loading, not with an index. Indexes only help when a single query itself is slow.

When should I use a partial index instead of a full index?▼

Use a partial index for highly selective filters on skewed data, such as WHERE status = 'pending' when only a small fraction of rows match. It keeps the index small and cheap to maintain while still serving the hot query path.