# Turning A Slow Query Into An Index With One Prompt

> Paste the query, the EXPLAIN output and the row counts — not just the query.

- **Author:** [Dmytro Shevchenko (@dmytro_shevchenko)](https://www.promptabide.com/dmytro_shevchenko)
- **Published:** 2026-04-05
- **Updated:** 2026-08-22
- **Tags:** `backend`, `postgres`, `performance`, `database`
- **Views:** 11749
- **Likes:** 28

## Prompt

```
The difference between a useless answer and a good one here is entirely in what you paste.

**Useless:** "Why is this query slow?" + the query.
**Useful:** the query, the EXPLAIN ANALYZE output, the table row counts, the existing indexes, and the Postgres version.

The prompt I use:

"Postgres 15. Table `events` has 42M rows, `users` has 1.1M.
Existing indexes: [list].
Query: [query].
EXPLAIN ANALYZE: [paste].

Tell me: which node is actually expensive, what index would fix it, and what that index costs on writes. Give the CREATE INDEX CONCURRENTLY statement."

Asking for the *write* cost is the part people leave out. Otherwise you get a recommendation for four new indexes on a table that takes 8k inserts a second.

My 5.2s query is now 140ms on one partial index.
```

---

Canonical HTML: https://www.promptabide.com/bides/slow-query-to-index-prompt-dmytro-shevchenko-3
Agent guide: https://www.promptabide.com/llms.txt · https://www.promptabide.com/agent-instructions.md
Sitemap: https://www.promptabide.com/sitemap.xml
