Parallel Cursor Looping - SAP ABAP - Performance Tuning
Nested Loops is one of the fear factors for all the ABAP developers as this consumes lot of program execution time. If the number of entries in the internal tables is huge, then the situation would be too worse. The solution for this is to use parallel cursor method whenever there is a need for Nested Loop.
Advantages Over Conventional Nested Loop Method:
- When nested loop becomes necessary than parallel cursor is really helpful in improving the performance.
- It will help us to avoid complete looping of the internal table.
- If there are two-three internal tables inside the loop it will be more efficient way of coding.
PROGRAM LINES:
Conventional Method Using Nested Loops:
loop at lt_vbpa into wa_vbpa.
loop at lt_kna1 into wa_kna1 where kunnr = wa_vbpa-kunnr.
****** Your Actual logic within inner loop ******
endloop.
endloop.
The below code is the replacement for above code with Parallel Cursor logic which is most preferred method.
Parallel Cursor Method (Preferred Method):
sort: lt_vbpa by kunnr, "Sorting by key is very important
lt_kna1 by kunnr. "Same key which is used for where condition is used here
loop at lt_vbpa into wa_vbpa.
read lt_kna1 into wa_kna1 " This sets the sy-tabix
with key kunnr = wa_vbpa-kunnr
binary search.
if sy-subrc = 0. "Does not enter the inner loop
v_kna1_index = sy-tabix.
loop at lt_kna1 into wa_kna1 from v_kna1_index. "Avoiding Where clause
if wa_kna1-kunnr <> wa_vbpa-kunnr. "This checks whether to exit out of loop
exit.
endif.
****** Your Actual logic within inner loop ******
endloop. "KNA1 Loop
endif.
endloop. " VBPA Loop