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-559899/,如需转载,请注明出处,否则将追究法律责任。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值