另类的循环内查询

某php里面,sql如下

select count(*) from a, (select sum(b) as b from c where c.id=a.id ) as d whee a.id= b.aid  and a.xxxx = ??? and a.yyyy=?????

用来做分页的满足条件记录数量的统计,看上去不错。

但我们考虑如下场景,当 a里面满足 a.xxxx = ??? and a.yyyy=????? 的记录达到万条,那么 (select sum(b) as b from c where c.id=a.id )

不也要计算万次?

优化方法如下:

删除(select sum(b) as b from c where c.id=a.id )

而在每个分页展示的时候,发出 rowcountperpage 个统计。

将大大减轻查询负载。

原文地址:https://www.cnblogs.com/stevenlaz/p/3984982.html