gpt4 book ai didi

mysql - 查询需要 0.0005 秒,但是当用作子查询时,它会将选择增加到 2.+

转载 作者:行者123 更新时间:2023-11-29 02:11:49 24 4
gpt4 key购买 nike

我有这个选择:

select c.nome, p.foto, c.user, p.user, p.id, p.data, p.titulo, p.youtube, pp.foto, count(DISTINCT likes.user) as likes_count, count(distinct comentarios.id) as comentarios_count, count(DISTINCT l2.user) as count2

from posts p

join cadastro c on p.user=c.id
left join profile_picture pp on p.user = pp.user
left join likes on likes.post = p.id
left join comentarios on comentarios.foto = p.id and comentarios.delete = 0
left join likes l2 on l2.post = p.id and l2.user = 1

where (p.user in (select following from following where user =1 and block=0 and feed=0) or p.user=1) and p.delete='0'
group by p.id
order by p.id desc limit 15

通常需要 2.2705 秒。如果我删除 (p.user in (select following from following where user =1 and block=0 and feed=0) or p.user=1) and 它将需要 0.0018

如果我使用下面的选择,它只需要 0.0005 秒:

select following from following where user =1 and block=0 and feed=0

我的问题是,我做错了什么?第一个查询在 0.0018 中运行,第二个查询在 0.0005 = 0.0023 中运行。为什么我的全选是2.2705?这是什么逻辑?

最佳答案

时间没有问题。 IN 运算符将必须验证每个 p.user 是否存在于选择返回的 N 元素中,这自然会增加总时间。

关于mysql - 查询需要 0.0005 秒,但是当用作子查询时,它会将选择增加到 2.+,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50262137/

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