簡體   English   中英

SAP HANA | 有條款履行

[英]SAP HANA | With Clause performance

我們正在使用SAP HANA 1.0 SPS12。

我們有如下的日表-

從表_1中選擇trans_date,article,measure1,measure2

表格數量〜500萬行

我們需要查看以下數據-

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'

如果您關心性能,那么將數據放在一行中應該會更好:

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM