# Query Optimizer

> Diagnose and fix slow Postgres queries using EXPLAIN ANALYZE, index suggestions, and N+1 detection. Use when a query is slow, a page is slow because of the DB, or before shipping a query that touches a large table.

- **Type:** Skill
- **Install:** `agentstack add skill-m-binimran-dev-pack-query-optimizer`
- **Verified:** Yes — security-reviewed for prompt injection and unsafe behavior
- **Seller:** [m-binimran](https://agentstack.voostack.com/s/m-binimran)
- **Installs:** 0
- **Category:** [Databases](https://agentstack.voostack.com/c/databases)
- **Latest version:** 0.1.0
- **License:** MIT
- **Upstream author:** [m-binimran](https://github.com/m-binimran)
- **Source:** https://github.com/m-binimran/dev-pack/tree/main/skills/query-optimizer

## Install

```sh
agentstack add skill-m-binimran-dev-pack-query-optimizer
```

Requires the [AgentStack CLI](https://agentstack.voostack.com/docs/cli). Works with Claude Code, Cursor, and any MCP-compatible agent.

## About

# query-optimizer

Make the query fast based on evidence, not guesses.

## Process
1. **Get the plan:** run `EXPLAIN (ANALYZE, BUFFERS) `. Never optimize without it.
2. **Read the plan top-down:** find the costliest node. Look for:
   - `Seq Scan` on a big table where an index could be used.
   - `Rows Removed by Filter` high → missing/!selective index.
   - Estimated vs actual rows far apart → stale stats (`ANALYZE`).
   - Nested loop over many rows → maybe a hash/merge join is better.
3. **Propose the smallest fix:** an index, a rewritten predicate, a `LIMIT`, or pagination.
4. **Re-measure** with `EXPLAIN ANALYZE` and report before/after timing. Don't claim a win without it.

## Common fixes
- Composite index ordered by selectivity for multi-column filters.
- Partial index (`where active`) for queries that always filter the same way.
- Replace `OFFSET` pagination with keyset (`where id > $last`) for deep pages.
- Kill **N+1**: one query per row in app code → use a join or `IN (...)`/`= ANY`.

## Output
- The plan's bottleneck in one sentence.
- The fix (SQL/DDL).
- Before/after timing from real `EXPLAIN ANALYZE` runs.

## Guardrails
- An index isn't free — it slows writes. Only add what the plan justifies.
- Don't add an index that duplicates an existing one's prefix.

## Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

- **Author:** [m-binimran](https://github.com/m-binimran)
- **Source:** [m-binimran/dev-pack](https://github.com/m-binimran/dev-pack)
- **License:** MIT

Install and usage instructions live in the source repository linked above.

## Pricing

- **Free** — Free

## Security capabilities

Automated source analysis of v0.1.0 — what this tool can access:

- **Network access:** no
- **Filesystem access:** no
- **Shell / process execution:** no
- **Environment & secrets:** no
- **Dynamic code execution:** no

*"Yes" means the capability is present in the source — more access means more to trust, not that it is unsafe.*


## Versions

- **0.1.0** — security scan: passed — Imported from the upstream source.

## Links

- Listing page: https://agentstack.voostack.com/l/skill-m-binimran-dev-pack-query-optimizer
- Seller: https://agentstack.voostack.com/s/m-binimran
- Browse the marketplace: https://agentstack.voostack.com/browse

---
Listed on AgentStack — the marketplace for AI agent skills and MCP servers. Every listing is security-reviewed. Creators keep 70%.
