我有这个选择:
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 (从下面的 user =1 和 block=0 和 feed=0 中选择以下内容) 或 p.user=1) ,
它将需要 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/