简体   繁体   English

SAP HANA | 有条款履行

[英]SAP HANA | With Clause performance

We are using SAP HANA 1.0 SPS12. 我们正在使用SAP HANA 1.0 SPS12。

We have daywise table like below - 我们有如下的日表-

select trans_date,article,measure1,measure2 from table_1 从表_1中选择trans_date,article,measure1,measure2

Volume of table ~ 5 millions rows 表格数量〜500万行

we need to see data like - 我们需要查看以下数据-

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'

so on and so forth for MTD,MTD-1,MTD-2,YTD. MTD,MTD-1,MTD-2,YTD等等。

Performance wise is it better to use WITH CLAUSE and hold data for one year and then split according to timeframe? 在性能方面,最好与WITH CLAUSE一起使用并将数据保存一年,然后根据时间范围进行拆分? OR Is it better to use separate aggregation for each timeframe as shown above. 或最好对每个时间范围使用单独的汇总,如上所示。

As far as I understand, in RDBMSs like Oracle, WITH CLAUSE materializes results and use it from the memory. 据我了解,在Oracle等RDBMS中,WITH CLAUSE实现结果并从内存中使用它。 SAP HANA is In Memory database itself. SAP HANA是内存数据库本身。 Does using WITH CLAUSE in SAP HANA gives distinctive performance edge? 在SAP HANA中使用WITH CLAUSE是否会带来独特的性能优势?

Query using 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'

If you care about performance, putting the data in a single row should be much better: 如果您关心性能,那么将数据放在一行中应该会更好:

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);

You can unpivot the results afterwards, if you really need the values in separate rows. 如果确实需要在单独的行中输入值,则可以在以后取消显示结果。

Alternatively, you can do: 或者,您可以执行以下操作:

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