Code Review
Code Review #3

This SQL query is slow on a table with 50M rows. Explain why and suggest improvements: ```sql SELECT u.name, COUNT(*) as post_count FROM users u LEFT JOIN posts p ON u.id = p.user_id WHERE p.created_at > NOW() - INTERVAL '30 days' OR p.created_at IS NULL GROUP BY u.name HAVING COUNT(*) > 0 ORDER BY post_count DESC LIMIT 20; ``` The table has indexes on `users.id` and `posts.user_id`.

9 models responded

high9.4

Judge: Excellent analysis. Identifies the LEFT JOIN + WHERE interaction (the core issue), incorrect COUNT(*) semantics, missing index on created_at, and filtering behavior for users with only old posts. The fix correctly moves the time filter to the ON clause, uses COUNT(p.id) instead of COUNT(*), and adds GROUP BY u.id. Clear explanation of why each change helps.

This SQL query is slow for several reasons, primarily due to how the `WHERE` clause interacts with the `LEFT JOIN` and `COUNT(*)` on a large table. Let's break down the problems and then suggest impr...
gemini/gemini-2.5-flash · 2026-03-02
o4-miniOpenAI
high8.4

Judge: Correctly identifies the LEFT JOIN + WHERE interaction that effectively nullifies the LEFT JOIN. Spots the missing index on created_at, the GROUP BY on text field (u.name), and provides two concrete rewrite options including a CTE approach. Suggests the right composite index (posts(user_id, created_at)). Clear explanation of query plan implications. Solid fix quality.

Let’s unpack what’s happening and then look at a few ways to rewrite and re-index so that the engine only scans a small fraction of your 50 million posts. 1. Why it’s slow - The WHERE clause ...
openai/o4-mini · 2026-03-02
Claude OpusAnthropic
high8.2

Judge: Correctly identifies the LEFT JOIN + WHERE contradiction, missing index on created_at, OR preventing index usage, grouping by name vs id, and the redundant HAVING clause. Provides two rewrite options (A and B) which is insightful, showing the query's ambiguity. Suggests a composite index on (user_id, created_at). Clear structure and explanations. Hits the hard constraint about LEFT JOIN/WHERE interaction.

This query has several performance issues when dealing with 50M rows. Let me break down the problems and suggest improvements. ## Why it's slow: 1. **LEFT JOIN with WHERE clause contradiction**: The...
anthropic/claude-opus-4-20250514 · 2026-03-01
Claude HaikuAnthropic
high7.6

Judge: Correctly identifies the LEFT JOIN + WHERE interaction as the primary issue, missing created_at index, GROUP BY name vs id problem, and redundant HAVING clause. The improved query with subquery approach is a valid optimization. Suggests composite index. Truncated at the end but core analysis is complete.

# Performance Issues & Solutions ## Why It's Slow ### 1. **LEFT JOIN + WHERE Logic Problem** (Primary Issue) ```sql LEFT JOIN posts p ON u.id = p.user_id WHERE p.created_at > NOW() - INTERVAL '30 da...
anthropic/claude-haiku-4-5-20251001 · 2026-03-01
Claude SonnetAnthropic
high7.2

Judge: Correctly identifies the LEFT JOIN + WHERE interaction (the OR clause effectively negates the LEFT JOIN's purpose), missing created_at index, the redundant HAVING clause, and the OR performance problem. The restructured query using a subquery is a valid improvement. Clear explanation with code examples. Misses the GROUP BY u.name issue (should be u.id for correctness).

This query has several performance issues that make it slow on large datasets. Let me break down the problems and provide solutions. ## Why This Query is Slow ### 1. **Complex WHERE Clause with OR**...
anthropic/claude-sonnet-4-20250514 · 2026-03-01
GPT-4oOpenAI
high6.7

Judge: Correctly identifies the LEFT JOIN + WHERE clause interaction that turns the LEFT JOIN into an INNER JOIN, suggests a composite index on (user_id, created_at), and provides a reasonable rewrite. Analysis is solid if not exhaustive on all query plan implications.

The SQL query you provided is likely slow due to several reasons, particularly when dealing with a large dataset, such as a table with 50 million rows. Let's break down the query and identify potentia...
openai/gpt-4o · 2026-03-01
Phi-4Microsoft
high6.4

Judge: Correctly identifies the LEFT JOIN + WHERE interaction (hard constraint met), the full table scan issue, and grouping inefficiency. Provides two alternative query rewrites (CTE and EXISTS approaches) which are practical improvements. The analysis of missing index on created_at is implied. However, the response is truncated before the EXISTS query completes, and it doesn't explicitly discuss the HAVING COUNT(*) > 0 redundancy with the WHERE clause or suggest specific composite indexes.

The SQL query you provided is likely slow due to several factors, especially given the large size of the dataset (50 million rows). Let's break down potential issues and suggest improvements: ### Po...
ollama/phi4 · 2026-03-01
medium4.0

Judge: Identifies the LEFT JOIN + WHERE interaction issue (the OR condition effectively turns it into an INNER JOIN for non-NULL cases), suggests indexes and subqueries. However, the explanations are somewhat generic ('JOIN Order' isn't the real issue), and the alternative queries have logic errors (the EXISTS version changes semantics). Correctly suggests an index on created_at.

### Slow Query Explanation This SQL query is slow for several reasons: 1. **JOIN Order**: The LEFT JOIN operation is performed first, which can lead to a large intermediate result set. This can caus...
ollama/llama3.1:8b · 2026-03-01
DeepSeek R1DeepSeek
high0.0

Judge: Empty response. No SQL review or performance analysis provided.

Hard constraint failed: Empty response fails the hard constraint -- does not identify the LEFT JOIN/WHERE clause interaction.
deepseek/deepseek-reasoner · 2026-03-02