Library › Coding AI › Software developers
Write one production-safe index from an EXPLAIN plan
Practice: Giving the right context. About 5 minutes.
About this exercise
The account activity page is slow. Your migration rules: production indexes are always CREATE INDEX CONCURRENTLY, and this one must be named idx_events_account_id_created_at. Write exactly one statement in 1 code block (at most 3 lines) that fits the query's filter and sort, as a partial index on rows that aren't deleted. Then exactly one line starting 'Why: ' under 20 words, and nothing else.
Common mistakes when you ask AI for this
- A locking index. A plain CREATE INDEX locks writes on a 38-million-row table while it builds. Try: Add: "Production rule: CREATE INDEX CONCURRENTLY."
- Letting the AI name it. The AI invents a name, and your migration checker rejects it. Try: Add: "Name it idx_events_account_id_created_at."
- Wrong columns or order. Indexing kind, or created_at first, doesn't help this filter and sort. Try: Add: "Fit the WHERE and ORDER BY: (account_id, created_at DESC)."
- Several options and a lecture. The AI offers three indexes and explains query plans. Try: Add: "Exactly one statement, then one 'Why:' line under 20 words."
What a strong prompt includes
- Says it's for production
- Gives the index name
- Asks for a partial index
- Asks for one statement
- Shapes the explanation
Related exercises
- Add a cron line that matches the others in a crontab (Coding AI · Product managers)
- Add dark mode by overriding your existing CSS variables (Coding AI · Software developers)
- Add input checks to your number guessing game (Coding AI · High school students)
- Add input validation to a Java method with exact messages (Coding AI · Software developers)
- Add labels to a signup form without rewriting the page (Coding AI · High school students)