Skip to main content

Claude Prompt for SQL Query Performance Tuning

Optimize slow SQL queries step by step: EXPLAIN analysis, index strategies, query rewriting, and caching for dramatic performance gains.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt

                                

What this prompt does

This prompt takes one slow [query], your [table_info], and its [current_time], then walks through a disciplined optimization pass. It interprets the EXPLAIN output, recommends specific indexes (which columns, what type, partial or covering), and proposes query rewrites such as turning a subquery into a join or choosing EXISTS over IN. It also hunts for full table scans, cardinality issues, and lock contention, then ties everything to a concrete [target_time].

The structure works because it makes the model justify every change instead of guessing. Reading EXPLAIN and rewriting the query usually beats reaching for caching first, so the prompt only treats [cache_approach] and denormalization as later levers. By demanding the optimized query with a per-change explanation, plus an index migration and rollback plan, it keeps the optimization safe and reviewable rather than a black-box rewrite. It also pushes into the parts people skip under deadline pressure: statistics and cardinality issues that skew the planner, lock contention that only shows up under load, and a monitoring setup so you can track this query's performance over time rather than re-discovering the regression months later.

When to use it

  • A dashboard or API endpoint is slow and you have traced it to one specific [query].
  • You can read EXPLAIN output but want a second opinion on index and rewrite choices.
  • You want to exhaust query-level fixes before adding [cache_approach] complexity.
  • You need an index migration with a matching rollback plan, not just ad-hoc CREATE INDEX commands.
  • You suspect full table scans or stale statistics but want them confirmed and explained.
  • You need to hit a hard [target_time] and want each change justified for review.

Example output

Expect a step-by-step breakdown: an EXPLAIN interpretation, a prioritized list of index recommendations with reasoning, one or more rewritten versions of your [query], notes on table scans and cardinality, and a caching discussion scoped to [cache_approach]. It closes with the final optimized query annotated change-by-change, plus a migration to add the new indexes and a rollback to remove them.

Pro tips

  • Paste real EXPLAIN output and accurate [table_info] (row counts, existing indexes); the quality of the advice tracks directly with the detail you provide.
  • Set a concrete [current_time] and [target_time] so the model can reason about how aggressive the changes need to be.
  • Resist applying [cache_approach] first; let the prompt fix the query and indexes, since caching a slow query just hides the problem.
  • Review every proposed index before adding it, because redundant indexes slow writes and bloat storage.
  • Always keep the rollback migration; an index that helps this [query] can regress others, and you want a clean exit.
  • Iterate by feeding back the new EXPLAIN after applying changes and asking it to push closer to [target_time].

Frequently Asked Questions

Do I need to paste the EXPLAIN output myself?
It helps enormously. The prompt can reason from `[table_info]` and the `[query]` alone, but real EXPLAIN output lets it spot actual full table scans, bad row estimates, and join order issues. Without it, the recommendations are more generic and require more verification on your end.
Will it just tell me to add Redis caching?
No, caching is deliberately a later step. The prompt prioritizes reading EXPLAIN, adding the right indexes, and rewriting the `[query]` first. It only discusses `[cache_approach]` and denormalization as trade-offs once query-level fixes are exhausted, since caching a slow query hides rather than solves the problem.
Are the suggested indexes safe to add in production?
Treat them as recommendations to review, not commands to run blindly. Each new index speeds reads but slows writes and consumes storage, and adding one on a large table can lock or take time. Apply them during a maintenance window and keep the provided rollback migration handy.
Can it optimize queries for any database engine?
It works best when you state your engine in `[table_info]`, since index types, EXPLAIN format, and rewrite options differ between PostgreSQL, MySQL, and others. The general approach transfers across engines, but engine-specific syntax and features should be confirmed against your database's documentation.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in Data & SQL Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

Claude Code Expert · Online

👋

Hey there!

Quick Actions

WhatsApp Instant reply

Chat on WhatsApp

+880 1723 741224 · Instant reply

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

[email protected]

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support