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.