Best Techniques in ABAP Programming - Internal table appending


Joining Two Internal Tables - Best Technique


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 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. 

Normal Technique used by ABAPers  

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

Parallel Cursor Technique

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.


For more Tutorials, visit ABAP Tutorials, Tips & Tricks and Certification Questions

No comments:

Post a Comment