我有一个表following
,其中一个用户可以关注另一个用户。
此表有200.000+条记录。我的选择需要一段时间。
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'
我有where
来users
user=1
跟随`following` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`user` INT(11) UNSIGNED NOT NULL REFERENCES cadastro (`id`),
`following` INT(11) UNSIGNED NOT NULL REFERENCES cadastro (`id`),
`block` tinyint(1) NOT NULL DEFAULT 0,
`feed` tinyint(1) NOT NULL DEFAULT 0,
`data` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY (`user`, `following`)
)
。这是需要更长时间的合作伙伴。
我的下表:
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 = ?
where (p.user in (select following from following where user =? and block=0) or p.user=?) and p.delete='0'
group by p.id
order by p.id desc limit ?
如何更快地获得关注并改进此表的任何想法?任何指数或任何想法?
我的完整sql:
KEY `share` (post_share, `delete`),
PRIMARY KEY (`id`)
帖子:
writer = pd.ExcelWriter(xlPath, engine='openpyxl')
df.to_excel(writer)
writer.close()
感谢。
答案 0 :(得分:1)
我不确定你是否只想提起following.block=0
。在following
表上,您看起来应该有(user, following, block)
的索引,这将使其成为仅索引扫描。
摆脱嵌套查询很重要。
这是一个猜测,但我相信你正在寻找以下内容:
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 = ?
left join following f on f.user = p.user
where
(p.user = ? and p.delete = '0')
or
(f.user = ? and f.block=0)
group by p.id
order by p.id desc limit ?