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`.

14 models responded

high10.0

Judge: Thoroughly identifies the LEFT JOIN/WHERE interaction, OR index issues, missing index, name grouping, and redundant HAVING. Offers two intent-based rewrites (inner join vs preserving outer join with COUNT(p.id)), a composite index with ordering tradeoffs, and partitioning/pre-aggregation suggestions.

A few things in this query make it slow, and one of them also changes the semantics in a way that often prevents good plans. Why it’s slow 1. LEFT JOIN + WHERE on the right table defeats the outer j...
openai/gpt-5.4-mini · 2026-06-10
GPT-5.4OpenAI
high10.0

Judge: The response explicitly identifies the LEFT JOIN/WHERE interaction (the OR IS NULL pattern preserving an effective inner join), the COUNT(*) miscounting, the non-unique GROUP BY u.name, the missing created_at index, and the redundant HAVING, with query-plan implications. The rewrites (filtered join, pre-aggregation subquery) and the composite index strategy with tradeoff discussion genuinely solve the performance problem. Excellent, well-organized review.

It’s slow because the query structure makes the LEFT JOIN expensive and also prevents the database from using indexes as effectively as it could. What’s hurting performance 1. The LEFT JOIN + WHERE ...
openai/gpt-5.4 · 2026-06-11
Claude FableAnthropic
high10.0

Judge: Correctly diagnoses the OR/IS NULL index defeat, missing created_at index, aggregate-then-limit cost, GROUP BY name correctness/performance issues, the COUNT(*)-counts-the-NULL-row bug, and the always-true HAVING, with accurate LEFT JOIN semantics throughout. The aggregate-first rewrite with a (created_at, user_id) composite index is the right optimization, and the ON-clause LEFT JOIN variant with COUNT(p.id) covers the alternative intent. Includes EXPLAIN ANALYZE guidance.

Why It's Slow 1. The OR p.created_at IS NULL kills index usage. The OR condition forces the planner to evaluate the date filter against every joined row rather than using an index range scan on posts...
anthropic/claude-fable-5 · 2026-06-11
GPT-5.5OpenAI
high9.6

Judge: Identifies all five issues: the LEFT JOIN + WHERE interaction (with a precise explanation of which users are wrongly included/excluded), missing created_at index, COUNT(*) counting null-extended rows, the useless HAVING, and GROUP BY u.name merging same-named users. The rewrite (aggregate recent posts first, then join) with a composite index recommendation genuinely solves the performance problem, and it offers correct variants for both intended semantics plus EXPLAIN ANALYZE advice. Long but logically organized.

The query is slow mainly because it forces the database to examine/join far more posts rows than necessary, and the current indexes do not support the date filter. SELECT u.name, COUNT(*) as post_cou...
openai/gpt-5.5 · 2026-06-10
high9.4
Run 1 of 2

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 improvemen...
gemini/gemini-2.5-flash · 2026-03-02avg: 9.7
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
DeepSeek V4DeepSeek
high8.3

Judge: Clearly flags the LEFT JOIN + WHERE interaction, the index-defeating OR, the missing created_at index, the COUNT(*) null-row bug, and the no-op HAVING, but misses that GROUP BY u.name merges different users with the same name. The primary rewrite (filter and aggregate posts first, limit, then join) is correct and well-explained with sensible index advice, though the zero-posts alternatives section is muddled — it promises a UNION ALL approach, first shows a LEFT JOIN variant, and includes a shaky '50M users x posts' row-explosion claim. Good structure overall with a helpful summary table.

The query is slow because of several interacting issues: an inefficient OR condition that defeats indexing, a missing index on the filter column, an incorrect use of COUNT(*) in a LEFT JOIN, and unnec...
deepseek/deepseek-v4-pro · 2026-06-10
Claude OpusAnthropic
high8.2
Run 1 of 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 WHERE ...
anthropic/claude-opus-4-20250514 · 2026-03-01avg: 9.1
Claude HaikuAnthropic
high7.6
Run 1 of 2

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) LEFT JOIN posts p ON u.id = p.user_id WHERE p.created_at > NOW() - INTERVAL '30 days' OR p.created_at ...
anthropic/claude-haiku-4-5-20251001 · 2026-03-01avg: 7.6
Claude SonnetAnthropic
high7.2
Run 1 of 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 WHERE p.cr...
anthropic/claude-sonnet-4-20250514 · 2026-03-01avg: 7.9
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
Run 1 of 2

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: Potenti...
ollama/phi4 · 2026-03-01avg: 6.1
medium4.0
Run 1 of 2

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 cause perfor...
ollama/llama3.1:8b · 2026-03-01avg: 4.3
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