开发者

MySQL sub-query optimization

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 ga开发者_JAVA百科mes.name ASC

Could someone point out my error here?


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜