开发者

Query is slow while doing a NOT IN on a nested SELECT from another table

SELECT problems . * , users.id AS new_user_id, users.nick AS nick
FROM problems, users
WHERE problems.deleted =0
AND problems.topic_id =1
AND problems.user_id = users.id
AND problems.id NOT
IN (
  SELECT DISTINCT (problem_id)
  FROM problems_attempted
  WHERE user_id =1
  AND total_questions = ( attempted_right + attempted_wrong + s开发者_运维技巧kipped )
)
ORDER BY problems.updated DESC

Can this query be optimized for a better performance?


Nested queries are always a performance bottleneck. Try to use join instead

select p.*, u.* 
from problems p join users u 
on p.user_id = u.id 
join problems_attempted pa on pa.problem_id = p.id 
where not (pa.user_id = 1 
and total_questions = ( attempted_right + attempted_wrong + skipped ))
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜