Speed up a slow database query
Prompt that reads a slow SQL query with its execution plan, finds the most expensive steps and suggests improvements with benefit and risk. Paste it into an AI tool of your choice, which then speeds up the query.
- Topic
- Coding & agents
- Type
- Prompt
- Level
- Intermediate
- Time
- about 8 min
Results depend on the tool, the model and what you enter.
Result preview
Example: written by our editors, not real AI output.
The query returns a customer's open orders from the last 90 days. Most expensive step: full scan of orders (1.2 million rows) because there is no index on (customer_id, status). Suggestion 1: index on orders (customer_id, status, created_at). Expected: run time from about 900 ms to under 20 ms. Side effect: slightly slower writes, about 40 MB more storage. Measure: EXPLAIN ANALYZE before and after, run time in monitoring for one week. Assumption: status has only a few distinct values.
Fill in
What you type stays in this browser window. We store and send nothing.
- PostgreSQL
- MySQL
- MariaDB
- SQL Server
- SQLite
- on every page view
- several times a minute
- hourly in the background
- once a night
- only for reports
Your prompt
You have edited the prompt. Changes in the fields above no longer overwrite it.
This is an instruction for an AI, not the result itself.
Where this prompt works
These AI tools are a good fit for this prompt:
How to use it
- Fill in the fields or tap a suggestion. Leave out names, addresses and other personal data.
- Copy the finished prompt into an AI tool of your choice.
- Test every suggestion on a copy with a realistic amount of data first and compare run time and result before you deploy it.
Why does this work?
The execution plan shows where the time is really lost. Naming the cost for writes avoids indexes that slow things down elsewhere.
Tips
- An execution plan with real timings, for example from EXPLAIN ANALYZE, says much more than the estimated version.