您好,欢迎访问一九零五行业门户网

HSQL语句,说说HSQL union all的用法

今天,在写hive的hsql语句,又是重复性的计算pv、uv(不爽),而且还是,算完分类算总类,就比如:算pc端的pv、uv,移动端的pv、uv,然后又要计算总的pv、uv,总的pv还好说,pc+移动端就ok了,但uv就得重新排重了,每次遇到这样的事情就非常不爽,因为不能快
今天,在写hive 的hsql语句,又是重复性的计算pv、uv(不爽),而且还是,算完分类算总类,就比如:算pc端的pv、uv,移动端的pv、uv,然后又要计算总的pv、uv,总的pv还好说,pc+移动端就ok了,但uv就得重新排重了,每次遇到这样的事情就非常不爽,因为不能快速在一个hsql中处理(可能自己有点强迫症吧),于是自己挤出上班时间测试了几种不同的写法,对比效率
好了废话不多说,上代码了 1、以前统计总量pv,uv和各分类的pv,uv都这么写也就是select a.type,a.pv,a.uv from ( select type,count(1) as pv,count(distinct(uid))as uv from t1 where dt='201410129' and req_url like 'mbloglist?domain=100808&ajwvr=6%' group by type union all select 'all' as type,count(1) as pv,count(distinct(uid))as uv from t1 where dt='201410129' and req_url like 'mbloglist?domain=100808&ajwvr=6%' ) a说明:distinct虽然写起来挺方便的,但是效率真的太差,建议永远不要用distinct2、然后我们的语句就可以改为:select a.type,sum(pv),count(uid) from ( select type,count(1) as pv,uid from t1 where dt='201410129' and req_url like 'mbloglist?domain=100808&ajwvr=6%' group by uid,type union all select 'all' as type,count(1) as pv,uid from t1 where dt='201410129' and req_url like 'mbloglist?domain=100808&ajwvr=6%' group by uid) agroup by type这样虽然效率提高了些,而且我也一直这么用了,有段时间,但总感觉还是很不爽,总觉得没有发挥union all的功能3、今天才发现,这group by 不能写在里面,真的严重影响效率,而且按照上面写job数量还多,果断需改:select type,sum(pv),count(uid) from (select a.type,sum(pv),uid from ( select type,1 as pv,uid from t1 where dt='201410129' and req_url like 'mbloglist?domain=100808&ajwvr=6%' union all select 'all' as type,1 as pv,uid from t1 where dt='201410129' and req_url like 'mbloglist?domain=100808&ajwvr=6%' ) agroup by uid,type) b group by type经测试,效率果然杠杠的
其它类似信息

推荐信息