每个ABAP在写代码的时候,我们都不可避免的要使用到嵌套循环,虽然我们有时候能避免掉,也有无可避免的时候,这个时候就体现我们开发人员的经验和水平的时候。
如何更快的提升ABAP程序的性能,我们开始分析一下如下的代码:
REPORT ZABAPHOME.
TYPES: STR_VBAK TYPE STANDARD TABLE OFVBAK.
DATA: IT_VBAK TYPE STR_VBAK .
DATA: WA_VBAK LIKE LINE OF IT_VBAK.
TYPES: STR_VBAP TYPE STANDARD TABLE OFVBAP.
DATA: IT_VBAP TYPE STR_VBAP.
DATA: WA_VBAP LIKE LINE OF IT_VBAP.
SELECT* FROM VBAK
INTO TABLE IT_VBAK
UP TO 5000 ROWS.
CHECK IT_VBAK IS NOT INITIAL.
SELECT * FROM VBAP
INTO TABLE IT_VBAP
FOR ALL ENTRIES IN IT_VBAK
WHERE VBELN = IT_VBAK-VBELN.
*
DATA: LV_START_TIME TYPE TIMESTAMPL,
LV_END_TIME TYPE TIMESTAMPL,
LV_DIFF TYPE TIMESTAMPL.
DATA: LV_TABIX TYPE I.
GET TIME STAMP FIELD LV_START_TIME.
LOOP AT IT_VBAK INTO WA_VBAK.
LOOP AT IT_VBAP INTO WA_VBAP
WHERE VBELN = WA_VBAK-VBELN.
ENDLOOP.
ENDLOOP.
GET TIME STAMP FIELD LV_END_TIME.
LV_DIFF = LV_END_TIME - LV_START_TIME.
WRITE: /(50) '普通循环时间:', LV_DIFF.
**--重点代码来了
CLEAR: LV_START_TIME, LV_END_TIME,LV_DIFF.
GET TIME STAMP FIELD LV_START_TIME.
**首先要排序
SORT: IT_VBAK BY VBELN,
IT_VBAP BY VBELN.
LOOP AT IT_VBAK INTO WA_VBAK.
**--Read到对应的TABIX
READ TABLE IT_VBAP TRANSPORTING NO FIELDS
WITH KEY VBELN = WA_VBAK-VBELN
BINARY SEARCH.
LV_TABIX = SY-TABIX.
**然后根据TABIX来循环
LOOP AT IT_VBAP FROM LV_TABIX INTO WA_VBAP.
IF WA_VBAP-VBELN <> WA_VBAK-VBELN.
EXIT.
ENDIF.
ENDLOOP.
ENDLOOP.
**--重点代码
GET TIME STAMP FIELD LV_END_TIME.
LV_DIFF = LV_END_TIME - LV_START_TIME.
WRITE: /(50)'read循环后时间分析', LV_DIFF.
执行100条数据:
SELECT * FROM VBAK INTO TABLE IT_VBAK UP TO 100 ROWS.
结果:
执行1000条数据:
SELECT * FROM VBAK INTO TABLE IT_VBAK UP TO 1000 ROWS.
结果:
执行5000条数据:
SELECT * FROM VBAK INTO TABLE IT_VBAK UP TO 5000 ROWS.
结果:
执行10000条数据:
SELECT * FROM VBAK INTO TABLE IT_VBAK UP TO 10000 ROWS.
结果:
来点极限数据看看
SELECT * FROM VBAK INTO TABLE IT_VBAK UP TO 100000 ROWS.
结果:
你们想想是怎样吧(前面代码被我注销了,我不想搞死服务器)