Optimize a slow SQL query
Find why a query is slow and get a faster version, index suggestions and a way to prove the improvement.
Paste a slow or suspicious SQL query and get correctness checks, performance fixes, and a cleaner rewrite you can verify.
Review this {{dialect}} query for correctness first, then performance and readability.
Query:
{{query}}
Context: {{context}}
What it is supposed to return: {{intent}}
Please do the following:
1. Correctness review: find logical bugs, such as join fan-out, wrong join type, filters on the outer table of a LEFT JOIN placed in WHERE, NULL traps in comparisons and NOT IN, double counting, time zone or date boundary mistakes, and aggregations at the wrong grain. For each issue, show the offending line and a concrete failure example.
2. Performance review: list the likely bottlenecks in order of impact (unindexed filters, functions on indexed columns, unnecessary DISTINCT, correlated subqueries, large sorts, selecting unneeded columns). Explain what you cannot judge without the execution plan or table statistics, and tell me what to look for in the plan.
3. Rewrite: a corrected, optimized, readable version using clear CTE names and consistent formatting. Keep the output columns identical unless a change is needed; flag any change.
4. Index suggestions: only the ones that clearly help, with the reason and the write-cost tradeoff.
5. Verification: how to prove the rewrite returns the same result as the original (row counts, checksum or EXCEPT comparison on a sample).
If you are unsure whether something is a bug without knowing the data, say so rather than asserting.Concrete failure examples for each suspected bug make the review checkable. Paste the EXPLAIN output as well for much more precise performance advice.
Licensed CC0 (public domain) Version 1 · history
Find why a query is slow and get a faster version, index suggestions and a way to prove the improvement.
Describe your tables and the question you need answered and get a correct, commented SQL query with assumptions spelled out.
Set up a proper cohort retention analysis, from defining cohorts and the activity event to producing the table and reading it correctly.
Get a prioritized code review that separates real defects from style nits, with a concrete fix for each issue.
Make tangled code easier to read and change without altering behavior, with every edit explained.
Turn feature requirements into a normalized schema with keys, constraints, indexes and the tradeoffs explained.
Comments (0)
Sign in to join the discussion.
No comments yet. Be the first to share how it worked.