DEV Community

Antonio Marasco
Antonio Marasco

Posted on

The Claude prompt I use to optimize slow SQL queries

Here's the single Claude prompt I reach for most when a query is dragging.

Optimize a slow SQL query

Analyze and optimize this slow SQL query:
[paste query]

Context:
- Table size: [rows]
- Query frequency: [per second/minute]
- Current performance: [ms]  ->  Target: [ms]

Suggest indexes, rewrites, and explain the trade-offs (read speed vs. write cost).
Show EXPLAIN reasoning: what scans it eliminates.
Enter fullscreen mode Exit fullscreen mode

Use it when a query is slow and you are not sure whether the fix is an index, a rewrite, or a schema change. Asking for the EXPLAIN reasoning and the read-vs-write trade-off is what turns a generic answer into one you can actually ship.

This is one of 55 prompts I use in production. 7 are free, no signup: https://gist.github.com/razorphish/5469109bbe6f20a49280c5b5002d1a5a

The full set (each with a worked example input and a real output) is here: https://2797536849428.gumroad.com/l/vxetvk

What prompt do you reach for most? I am curious whether the "worked example + output" format beats a bare prompt list.

Top comments (0)