gpt4 book ai didi

sql - 在另一个表的嵌套 SELECT 上执行 NOT IN 时查询速度很慢

转载 作者:行者123 更新时间:2023-11-29 05:46:14 26 4
gpt4 key购买 nike

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 + skipped )
)
ORDER BY problems.updated DESC

是否可以优化此查询以获得更好的性能?

最佳答案

嵌套查询始终是性能瓶颈。尝试改用 join

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

关于sql - 在另一个表的嵌套 SELECT 上执行 NOT IN 时查询速度很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1623531/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com