query-optimization

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

Updated Sep 10, 2026
One-click install
npx skills add https://github.com/serpro-workshop-fortaleza/sifap-modernization-paula --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/sifap-modernization-paula/tree/main/.github/skills/query-optimization
Command: npx skills add https://github.com/serpro-workshop-fortaleza/sifap-modernization-paula --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 execution plans, and designing justified indexes, so changes are measured and validated instead of guessed. ## Core Features & Use Cases - Diagnostic workflow: Capture a baseline (p50/p95 latency, rows examined), obtain the plan with EXPLAIN (ANALYZE, BUFFERS), identify common causes like sequential scans, stale statistics, and nested loop misuse, then propose the smallest fix. - Index design heuristics: Apply the ESR rule (equality, sort, range), covering indexes with INCLUDE columns, and partial indexes, while justifying each index against its write cost. - Antipattern detection: Flag SELECT * in critical paths, functions on indexed columns in WHERE clauses, and ORM N+1 problems. - Use Case: A PostgreSQL endpoint is slow. You paste the EXPLAIN ANALYZE output, and the Skill identifies a sequential scan on a large table with a selective predicate, proposes a CREATE INDEX CONCURRENTLY statement, and provides a validation checklist. ## Quick Start Ask the assistant to analyze why this query is slow using the attached EXPLAIN ANALYZE output and propose the smallest fix.

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?

Start by capturing a baseline: p50/p95 latency, rows examined, and rows returned. Then obtain the execution plan with EXPLAIN (ANALYZE, BUFFERS) on PostgreSQL, EXPLAIN ANALYZE FORMAT=JSON on MySQL 8, or SET STATISTICS IO, TIME ON on SQL Server, and look for common causes like sequential scans or stale statistics.

How do I design a composite index for a query?

Follow the ESR rule: place equality columns first, then range columns, then sort columns. For read-heavy queries, consider a covering index with INCLUDE columns to avoid 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, outdated table statistics causing row estimate errors above 10x, or a predicate that is not selective enough. Run ANALYZE to refresh statistics or rewrite the predicate to match the index.

Does this work with PostgreSQL, MySQL, and SQL Server?

Yes. The workflow covers EXPLAIN (ANALYZE, BUFFERS) for PostgreSQL, EXPLAIN ANALYZE FORMAT=JSON for MySQL 8, and SET STATISTICS IO, TIME ON for SQL Server, with diagnostic heuristics that apply across all three engines.

When should I not add an index to fix a slow query?

Avoid adding indexes for ORM N+1 problems, which should be fixed with eager loading instead. Also avoid indexing every column, since each index adds write cost and storage overhead without guaranteeing read benefits.