--访客数 select count(1) from ( select v.idvisitor from ods.piwik_log_visit v where v.idsite = 18 AND TO_NUMBER(TO_CHAR(v.visit_last_action_time+8/24,'YYYYMMDD'))>=20170501 AND TO_NUMBER(TO_CHAR(v.visit_last_action_time+8/24,'YYYYMMDD'))<=20170531 group by v.idvisitor) --优化后的访客数查询 select count(distinct v.idvisitor) from ods.piwik_log_visit v where v.idsite = 18 AND TO_NUMBER(TO_CHAR(v.visit_last_action_time+8/24,'YYYYMMDD'))>=20170501 AND TO_NUMBER(TO_CHAR(v.visit_last_action_time+8/24,'YYYYMMDD'))<=20170531
发现查询太慢了,优化前查询要9秒,优化后只要1秒。
sql是根据访客ID进行分组,然后才进行统计数量。
说明记录中访客ID是有重复出现的情况,要合并成一条,去除重复数,所以可以考虑用去重关键字DISTINCT,然后再用统计函数COUNT进行计算。