ABAP--两个提高ABAP代码效率的优化方法

一、利用内表缓冲减少数据库访问次数

REPORT  zmmi003.

data: it_vbap type table of vbap,
wa_vbap type vbap,
it_makt type table of makt,
wa_makt type makt.
perform process.
perform process_using_cursor.
*&---------------------------------------------------------------------*
*& Form process
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM process .
  DATA: t1 TYPE i,
  t2 TYPE i,
  tmin TYPE i.
  get run time field t1.
  select * into wa_vbap
  up to 10000 rows
  from vbap .
    select single *  into wa_makt
    from makt
    where matnr = wa_vbap-matnr.
*    write:/ wa_makt-MAKTX.
  endselect.
  get run time field t2.
  tmin = t2 - t1.
  write:/ '------------------------------------------------------------------'.
  write:/ 'Time(ms):' ,tmin.
ENDFORM. " process
*&---------------------------------------------------------------------*
*& Form process_using_cursor
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM process_using_cursor .
  DATA: t1 TYPE i,
  t2 TYPE i,
  tmin TYPE i.
  get run time field t1.
  select * into wa_vbap
  up to 10000 rows
  from vbap .
    read table it_makt into wa_makt with key matnr = wa_vbap-matnr binary search.
    if sy-subrc = 0.
*      write:/ wa_makt-MAKTX.
    else.
      select single *  into wa_makt
      from makt
      where matnr = wa_vbap-matnr.
*      write:/ wa_makt-MAKTX.
      APPEND wa_makt to it_makt SORTED BY matnr.
      endif.
  endselect.
  get run time field t2.
  tmin = t2 - t1.
  write:/ '------------------------------------------------------------------'.
  write:/ 'Time(ms):' ,tmin.
ENDFORM. " process_using_cursor 

 

Time(ms):  4.735.194

Time(ms):  3.411.177

 

二、利用一条read table来提高loop 访问速度

 data: it_mara type table of mara,
wa_mara type mara,
it_makt type table of makt,
wa_makt type makt.
select *
up to 100 rows
from mara
into table it_mara.
select *
up to 100 rows
from makt
into table it_makt.
perform process.
perform process_using_cursor.
*&---------------------------------------------------------------------*
*& Form process
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM process .
  DATA: t1 TYPE i,
  t2 TYPE i,
  tmin TYPE i.
  get run time field t1.
*  sort it_mara by matnr.
  sort it_makt by matnr.
  loop at it_mara into wa_mara.
    loop at it_makt into wa_makt where matnr = wa_mara-matnr.
    endloop.
  endloop.
  get run time field t2.
  tmin = t2 - t1.
  write:/ 'Time(ms):' ,tmin.
ENDFORM. " process
*&---------------------------------------------------------------------*
*& Form process_using_cursor
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM process_using_cursor .
  DATA: t1 TYPE i,
  t2 TYPE i,
  tmin TYPE i,
  tabix type sy-tabix.
  get run time field t1.
*  sort it_mara by matnr.
  sort it_makt by matnr.
  loop at it_mara into wa_mara.
    read table it_makt transporting no fields with key matnr = wa_mara-matnr.
    if sy-subrc = 0.
      tabix = sy-tabix.
      loop at it_makt into wa_makt from tabix.
        if wa_makt-matnr <> wa_mara-matnr.
          exit.
        endif.
      endloop.
    endif.
  endloop.
  get run time field t2.
  tmin = t2 - t1.
  write:/ 'Time(ms):' ,tmin.
ENDFORM. " process_using_cursor

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/15232446/viewspace-541598/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/15232446/viewspace-541598/

你可能感兴趣的:(ABAP--两个提高ABAP代码效率的优化方法)