关于数据统计时的效率

近两天时间一直在更改项目中的一个统计部分,对统计效率的一些看法写在这里,作为一个记录

1、统计有时遇到的是同一张表,不同的查询条件,如此一来不得不写很多条统计语句,以此来得到结果。首先:要保证数据的正确性,不能为了效率而牺牲了准确性。我的办法是把需要查询的内容一块一块的查出来,把这一块一块的内容作为一张临时表,然后再用链式查询来进行整理

2、尽可能减少统计模块内的链接查询,经验证模块内的链接查询减少一些查询的效率可以调高很多

如:
select t.sid,t.rymc,t1.dl_count,t2.ry_count from
(
select sid,rymc from ryxx 
where (ISDELETED= 0 or ISDELETEDis null)
and ID = '000100090002'
) t
left join
(
select userid,count(1) dl_count         --单一的SQL语句,及其查询条件
from sys_login l
where l.logindate >= to_date('20140501', 'yyyyMMdd')
and l.logindate <= to_date('20140731', 'yyyyMMdd')
group by userid
) t1
on t.sid=t1.userid
left join
(
select qyid,count(1) ry_count            --单一的SQL语句,及其查询条件    
from ybyhxx ybyh 
where (ISDELETEDis null or ISDELETED= 0)
and ybyh.cjsj >= to_date('20140501', 'yyyyMMdd') and ybyh.cjsj <= to_date('20140731', 'yyyyMMdd') group by ryid ) t2 on t.sid = t2.ryid
天地何其大,人生何其短。 不困于一时,不困于一世。 且恒且坚,且苦且乐,且行且看。
原文地址:https://www.cnblogs.com/mozizhu/p/3945576.html