Skip to main content
UseCasePilot

ChatGPT Prompt for SQL Query Optimization

Use this ChatGPT prompt to optimize slow SQL queries. Copy the template, paste your query, and get a faster version with an explanation of the performance improvements.

Prompt Template

prompt.txt
You are a senior database engineer.

Analyze the following SQL query for performance issues.

Provide:
1. An explanation of what is causing slow performance.
2. An optimized version of the query.
3. Suggestions for indexes that would improve execution time.

Database type: [e.g. PostgreSQL, MySQL, SQL Server]

Query:
[PASTE QUERY HERE]

How to Use This Prompt

  1. 1Copy the prompt template above.
  2. 2Paste it into ChatGPT at chat.openai.com.
  3. 3Replace [e.g. PostgreSQL, MySQL, SQL Server] with your database type.
  4. 4Replace [PASTE QUERY HERE] with your slow SQL query.
  5. 5Optionally include table sizes or the EXPLAIN output for better results.

When to Use This Prompt

  • A query is taking longer than expected to execute.
  • Your EXPLAIN plan shows sequential scans on large tables.
  • A report or dashboard query is timing out.
  • You want to identify missing indexes before adding them to production.

Example Input

SELECT o.id, o.created_at, u.email, SUM(oi.price * oi.quantity) as total
FROM orders o
JOIN users u ON o.user_id = u.id
JOIN order_items oi ON oi.order_id = o.id
WHERE u.country = 'US'
  AND o.created_at > '2024-01-01'
GROUP BY o.id, o.created_at, u.email
ORDER BY o.created_at DESC;

Expected Output

The AI will identify missing indexes on users.country and orders.created_at, suggest composite index strategies, and return an optimized query with notes on each improvement.

Recommended AI Tools

Recommended Tool

Free plan

CursorAI-native code editor built for pair programming with LLMs.

Try Cursor

Recommended Tool

Free trial

GitHub CopilotAI pair programmer that suggests code completions in real time.

Try GitHub Copilot

Recommended Tool

Free plan

TabninePrivacy-focused AI code assistant for teams.

Try Tabnine

Related Prompt Templates

Frequently Asked Questions

Can ChatGPT optimize SQL queries?

Yes. ChatGPT can analyze SQL queries, identify performance bottlenecks such as missing indexes or inefficient joins, and suggest optimized rewrites. For complex queries, include your EXPLAIN output for more accurate advice.

What SQL performance issues can AI identify?

AI can spot missing indexes, N+1 patterns in ORM-generated queries, inefficient subqueries that can be rewritten as JOINs, unnecessary full table scans, and poorly structured GROUP BY or ORDER BY clauses.

Does ChatGPT work with all SQL databases?

ChatGPT understands most SQL dialects including PostgreSQL, MySQL, SQL Server, and SQLite. Specifying your database type in the prompt ensures the AI suggests syntax and index types that are compatible.

Should I apply AI-suggested SQL changes to production directly?

No. Always test AI-suggested query changes and index additions in a staging environment first. Measure actual execution time improvement before applying changes to a production database.