ABAP Code for Parallel Cursor - Loop Processing

The most common performance problem that occurs in ABAP programs is because of huge number of records in the internal tables. The problem complexities if a program has huge nested internal tables. How much ever efficient data select routines are, data processing routines would be contributing significantly for the bad performance. When analyzed it would be revealed that the where condition that is used in inner loops expend a significant amount of processing time. The idea is to avoid where conditions in the inner loops by maintaining the loop indexes manually.



Conventional Code for 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.

Code sample: Parallel Cursor method


SORTlt_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





Observation: One can observe that as the data increases, the time taken for the nested loop increases drastically, at the same time, the Parallel cursor method did not suffer any considerable time impact.
Verdict: Use the parallel cursor method whenever there is a need to process data in a nested loop.

Share this

Related Posts