A slow SQL query explained and sped up with ChatGPT
The struggle: A report query that used to take seconds now takes minutes, and you cannot read the query plan to see why.
What you end up with: A plain-words reading of the query plan and a tested change, with its trade-offs written down.
Paste the query, the table definitions and the query plan into ChatGPT, and it explains where the time goes and proposes changes. You test each on a copy and compare timings.
Tools: ChatGPT. Level: Free to start. Time: 45 min.
Use it if
- A report or page got slow over time
- You have the plan from EXPLAIN but cannot read it
- You can test on a copy of the data
Skip it if
- You can only test on production
- The query was never correct to begin with
What you need first
- A ChatGPT account (Free works)
- The slow query, table definitions and its query plan
- A copy of the data to test on
The method
Collect the evidence (ChatGPT: Chat)
Gather the query, the table definitions with indexes, row counts, and the plan from your database. Get the plan on a copy or read replica: EXPLAIN ANALYZE runs the query, so never run it on production without review. Replace real values in the query with placeholders.
Prompt to copy:
Write me a short checklist for gathering what a person needs to diagnose a slow [database] query: the query, table definitions and indexes, row counts, the plan, and the version. Include how to get the plan safely on a copy.
You get: One text block with everything.
Tip: Ask for the plan on a copy. Running EXPLAIN ANALYZE executes the query.
Attach or paste the evidence (ChatGPT: File uploads)
Paste it into the chat, or save it as a text file and attach it if it is long. Tell ChatGPT the database and version.
You get: The evidence in the chat.
Tip: Strip customer data and credentials. Table names are fine, row contents are not.
Read the plan with you (ChatGPT: Chat)
Ask ChatGPT to explain the plan in plain words, step by step, and say which step takes the time.
Prompt to copy:
I use [database and version]. Below are a slow query, the table definitions with indexes, approximate row counts and the query plan. Explain the plan step by step in plain words. Name the step that costs the most and why. List what you cannot tell from this evidence. Do not suggest changes yet. [paste everything]
You get: A plain reading with the costly step named.
Tip: Ask which numbers it is using so you can check them.
Propose options with costs (ChatGPT: Chat)
Ask for two or three changes, such as a rewrite or an index, each with the expected effect and the downside.
Prompt to copy:
Propose up to three changes ranked by expected benefit: query rewrite, new index, or schema change. For each give the exact SQL, the expected effect on the plan, the downside (write cost, disk, locking), and how I verify it. Say if a rewrite could change the results.
You get: Options ranked by effort and risk.
Tip: Every new index slows writes and uses space. Ask for the cost.
Test on a copy and compare (ChatGPT: Chat)
On a copy of the data, run the original and the changed query, check the results are identical, then compare timings and the new plan.
Prompt to copy:
Write a test script for me: run the old and new query on the copy, compare result sets row for row, run each three times, and record timings and the new plan. Give me a table for the results.
You get: Before and after timings with matching results.
Tip: Run each twice to avoid cache effects, and check a case with many rows.
Where this goes wrong
- Run EXPLAIN, and especially EXPLAIN ANALYZE (it runs the query), on a copy or read replica. Never run it on production without review by someone who owns the database.
- Test everything on a copy. New indexes slow writes, and index creation can lock a table in some engines. Compare results row for row. A faster query that returns different data is wrong.
- Do not paste real customer values or credentials into the chat.
- ChatGPT reads the plan you give it and cannot see your database. Treat its advice as a hypothesis.
Sources
More like this
- Agent pull requests checked before a human merges
- An incident timeline rebuilt from your git history
- Acceptance criteria written for vague tickets
Updated . Written with AI help and checked by an independent review step, as described in our editorial standards.