0

I've got a query that is running awfully slow:

SELECT * 
FROM games
WHERE games.platform =13
AND games.id NOT 
IN (SELECT game_id
FROM collections
WHERE collections.user_id =1)

I attempted to rewrite it as a left join but it's returned 0 results:

SELECT * 
FROM games 
LEFT JOIN collections ON collections.game_id = games.id 
WHERE collections.game_id IS NULL AND collections.user_id = 1 AND games.platform = 13
ORDER BY games.name ASC

Could someone point out my error here?

1
  • try that using #FULL OUTER JOIN because it will point to you everything, and what does not match will return as null. Commented Mar 17, 2011 at 19:59

1 Answer 1

3
SELECT games.* 
FROM   games 
       LEFT JOIN collections 
         ON collections.user_id = 1 
            AND collections.game_id = games.id 
WHERE  games.platform = 13 
       AND collections.game_id IS NULL 
ORDER  BY games.name ASC 

you need indexes on

  • (games.id,platform,name)
  • (collections.user_id,collections.game_id)
Sign up to request clarification or add additional context in comments.

2 Comments

I think the best indexes are games(id) and collections(user_id, game_id) select * makes the rest moot. Consider also changing select * to select games.*
You guys are rockstars! Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.