Joining internal tables.

Naive join: loop ITAB1, read ITAB2 w/key
Runtime: 732 microseconds

* Entries: 1000 (ITAB1), 300 (ITAB2)
* Line width: 100
* Both tables sorted by unique key K ascending

LOOP AT ITAB1 INTO WA1.
  READ TABLE ITAB2 INTO WA2
             WITH KEY K = WA1-K BINARY SEARCH.
  IF SY-SUBRC = 0.
    " ...
  ENDIF.
ENDLOOP.

 

 

More sophisticated: use parallel cursors
Runtime: 360 microseconds

* Entries: 1000 (ITAB1), 300 (ITAB2)
* Line width: 100
* Both tables sorted by unique key K ascending
DATA: I TYPE I.

I = 1.
LOOP AT ITAB1 INTO WA1.
  do.
    READ TABLE ITAB2 INTO WA2 INDEX I.
    IF SY-SUBRC <> 0. EXIT. ENDIF.
    IF WA2-K < WA1-K.
      ADD 1 TO I.
    ELSEIF WA2-K = WA1-K.
      " ...
      ADD 1 TO I.
      EXIT.
    ELSE.
      EXIT.
    endif.
  enddo.
  if sy-subrc <> 0. exit. endif.
ENDLOOP.


Documentation
If ITAB1 has n1 entries and ITAB2 has n2 entries, the time needed for
joining ITAB1 and ITAB2 with the straightforward algorithm is
O( n1 * log2( n2 ) ), whereas the parallel cursor approach takes only
O( n1 + n2 ) time.
The above parallel cursor algorithm assumes that ITAB2 is a secondary
table containing only entries also contained in primary table ITAB1.
If this assumption does not hold, the parallel cursor algorithm
gets slightly more complicated, but its performance characteristics
remain the same.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值