客户反映某本报表运行效率极慢,需要进行优化,简单看了代码并且使用SAT分析后,定位了两个主要的问题,最主要的问题是合计数量的时候在LOOP循环中使用了SELECT SUM(*) FROM internal_table,而且还有很多个,导致当LOOP的本地内表条目比较大的时候,效率将会受到非常大的影响;其次是在循环中调用函数STATUS_TEXT_EDIT来获取订单文本,这个其实影响不算很大,但为了最大化提升效率,针对这点也做了优化。
优化前:time out dump。
优化后:10秒运行完成。
最大的元凶就是下面这段代码:
解决方式:
sort table + collect?
解决方式:?
table function + cds view?
1.首先定义table function:ZPRPP_ORDER_STATUS_TF
@ClientDependent: false
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Table function for get PP order status'
define table function ZPRPP_ORDER_STATUS_TF
returns
{
objnr :j_objnr;
status_profile :j_stsma;
@EndUserText.label: 'System status'
system_status :char60;
}
implemented by method
zprcl_st_text=>get_status_text;
2.然后创建实现类:?zprcl_st_text
CLASS zprcl_st_text DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES if_amdp_marker_hdb.
CLASS-METHODS get_status_text FOR TABLE FUNCTION zprpp_order_status_tf.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS zprcl_st_text IMPLEMENTATION.
METHOD get_status_text BY DATABASE FUNCTION
FOR HDB
LANGUAGE SQLSCRIPT
OPTIONS READ-ONLY
USING jest jsto tj02t tj02 tj04.
declare lv_99 "$ABAP.type( numc2 )";
lv_99 = 99;
gt_status = select distinct
a.objnr,
b.stsma as status_profile,
d.txt04 as system_status,
E.nodis as no_display,
CASE
when f.linep is not null or f.linep <> 00 then
f.linep
else
lv_99
end as position,
CASE
when f.statp is not null or f.statp <> 00 then
f.statp
else
lv_99
end as priority
from jest as a
inner join jsto as b
on b.objnr = a.objnr
left outer join tj02t as d
on d.istat = a.stat
inner join tj02 as e on e.istat = a.stat
left outer join tj04 as f
on f.obtyp = b.obtyp
and f.istat = a.stat
where a.inact <> 'X'
and e.nodis <> 'X'
and a.mandt = session_context('CLIENT')
and b.mandt = session_context('CLIENT')
and d.spras = session_context('LOCALE_SAP')
order by POSITION desc,
priority desc,
system_status desc;
RETURN SELECT objnr,
status_profile,
STRING_AGG( system_status, char( 32 ) ORDER BY position asc, priority asc, system_status asc ) as system_status
FROM :gt_status
GROUP BY objnr, status_profile;
ENDMETHOD.
ENDCLASS.
3.最后创建cds view:
@AbapCatalog.sqlViewName: 'ZPRPPORDSTTXT'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'CDS View for get PP order status text'
define view ZPRPP_ORDER_STATUS_V
as select from ZPRPP_ORDER_STATUS_TF
{
key objnr,
status_profile,
@EndUserText.label: 'System status'
cast(system_status as abap.char(60)) as system_status
}
?4.直接根据AFKO-OBJNR关联该cds view即可获取订单状态
AMDP用的并不是特别熟练,有几个小的知识点也总结一下,方便我后续查找吧。
变量声明:
????????declare lv_99 "$ABAP.type( numc2 )";
系统变量:
空格:
? ? ? ? char( 32 )? 代表空格
空格问题参考链接:The ABAPVARCHARMODE: Blanks and Empty Strings in ABAP and SQLScript (brandeis.de)
排序拼接:?
? ? ? ? 基于函数STATUS_TEXT_EDIT的输出规则,在行转列拼接时要按照指定的顺序拼接展示,在string_agg方法中使用order by即可指定拼接顺序,asc为升序,desc为降序。
尽可能的避免在循环中多次出现数据库操作语句,程序性能优化是一个慢慢积累的过程,能跑出来结果永远是最低标准。
以上。