mysql group 查询的替代_SQL 优化之用两表联查取代子查询

有两张表group_post帖子表,group_post_fav_idx_oid_0用户点赞帖子记录表,帖子id对10取模等于0的分表。

需求:取出点赞数>20,评论数>10的帖子,但是在group_post里面没有记录点赞数的字段,所以需要对点赞记录表做统计。

#50 rows in set (1 min 9.67 sec)

select id, name from group_post

where id>=144

and id<=21081

and id in ( select oid from group_post_fav_idx_oid_0 group by oid having count(*)>20 )

and id not in (90,210,263,950,964,1157)

and gid not in (0,90,165,166,179)

and cnum>10

order by last_ts limit 50

优化之后

#50 rows in set (0.09 sec)

select a.id, a.name from group_post a, group_post_fav_idx_oid_0 b

where a.id>=144

and a.id<21081

and a.cnum>100

and a.id=b.oid

and a.id not in (90,210,263,950,964,1157)

and a.gid not in (0,90,165,166,179)

group by b.oid

having count(b.oid)>20

order by a.last_ts limit 50

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值