SAP HANA |使用子句性能

时间:2018-05-18 06:23:43

标签: sql sap hana

我们正在使用SAP HANA 1.0 SPS12。

我们有如下的日常表 -

从table_1中选择trans_date,article,measure1,measure2

表的数量~5百万行

我们需要看到像

这样的数据
select 'day-1',sum(measure1),sum(meaure2) from table1 where trans_date=add_days(current_date,-1) group by 'day-1'
union all
select 'day-2',sum(measure1),sum(meaure2) from table1 where trans_date>=add_days(current_date,-2) group by 'day-2' 
union all
select 'WTD',sum(measure1),sum(meaure2) from table1 where trans_date>=add_days(current_date,-7) group by 'WTD'
union all
select 'WTD-1',sum(measure1),sum(meaure2) from table1 where trans_date>=add_days(current_date,-15) and trans_Date <= add_days(current_date,-7) group by 'WTD-1'

等等MTD,MTD-1,MTD-2,YTD。

性能方面是否最好使用WITH CLAUSE并保存数据一年,然后根据时间范围进行拆分?或者如上所示,为每个时间帧使用单独的聚合更好。

据我所知,在像Oracle这样的RDBMS中,WITH CLAUSE实现了结果并从内存中使用它。 SAP HANA是内存数据库本身。在SAP HANA中使用WITH CLAUSE是否具有独特的性能优势?

使用WITH CLAUSE查询 -

WITH t1 as
(
select trans_date,sum(measure1),sum(meaure2) from table1 where trans_date>=add_days(current_date,-365)
)
select 'day-1',sum(measure1),sum(meaure2) from t1 where trans_date=add_days(current_date,-1) group by 'day-1'
union all
select 'day-2',sum(measure1),sum(meaure2) from t1 where trans_date>=add_days(current_date,-2) group by 'day-2' 
union all
select 'WTD',sum(measure1),sum(meaure2) from t1 where trans_date>=add_days(current_date,-7) group by 'WTD'
union all
select 'WTD-1',sum(measure1),sum(meaure2) from t1 where trans_date>=add_days(current_date,-15) 
                                                  and trans_Date <= add_days(current_date,-7) 
                                                  group by 'WTD-1'

1 个答案:

答案 0 :(得分:1)

如果您关心性能,那么将数据放在一行应该会好得多:

select sum(case when trans_date = add_days(current_date, -1) then measure1 end) as measure1_day1,
       sum(case when trans_date = add_days(current_date, -1) then measure2 end) as measure2_day1,
       sum(case when trans_date = add_days(current_date, -2) then measure1 end) as measure1_day2,
       sum(case when trans_date = add_days(current_date, -2) then measure2 end) as measure2_day2,
       . . .       
from table1
where trans_date >= add_days(current_date, -15);

如果您确实需要单独行中的值,则可以在之后取消结果。

或者,您可以这样做:

select days, sum(measure1), sum(measure2)
from (select 1 as days from dummy union all
      select 2 from dummy union all
      select 7 from dummy union all
      select 15 from dummy
     ) d left join
     table1 t
     on t.trans_date = add_days(current_date, - d.days)
group by days
order by days;