ChatGPT Coding

Write a SQL Query Optimizer

Prompt
Analyze this SQL query for performance issues: [paste query]. Explain what indexes are missing, suggest query rewrites, and estimate the performance gain. The table has approximately [row count] rows.
Why it works

Query optimization without context on data size is guesswork; this prompt grounds suggestions in reality.

If you're struggling with slow SQL queries and don't know where to start, this ChatGPT prompt gives you an instant performance analysis without hiring a database consultant. Whether you're building a web application, managing reports, or working with legacy code, slow queries drain resources and frustrate users. This prompt works by feeding ChatGPT your actual SQL code along with realistic context about your data, so it can identify genuine bottlenecks rather than making generic suggestions. It's designed for developers of any experience level who want to understand why their queries are slow and how to fix them.

To use this prompt effectively, paste your actual SQL query where it says [paste query] and fill in the row count field with your real table size. For example, if you have a query joining users and orders tables, you'd write something like: "Analyze this SQL query for performance issues: SELECT u.id, u.name, COUNT(o.id) FROM users u LEFT JOIN orders o ON u.id = o.user_id WHERE u.created_at > '2023-01-01' GROUP BY u.id, u.name. Explain what indexes are missing, suggest query rewrites, and estimate the performance gain. The users table has approximately 500000 rows and orders has 5000000 rows."

ChatGPT will return a detailed analysis identifying missing indexes on specific columns, suggesting query rewrites with explanations, and providing rough performance estimates like "adding an index on orders.user_id could reduce query time from 8 seconds to 500ms." It explains the reasoning behind each suggestion so you understand what's actually happening.

For better results, include your table schema if the query is complex or involves multiple joins. Also mention any current indexes you already have and whether you're experiencing specific symptoms like high CPU or memory usage. This extra context helps ChatGPT avoid suggesting indexes you already created and tailor recommendations to your actual pain points.