Tuesday, July 12, 2011

parallel cursor technique in ABAP

To improve the performance of the program where nested loops are used.

Technique 1:

SORT it_likp BY vbeln.
SORT it_lips BY vbeln.

LOOP AT it_likp INTO wa_likp.

  READ TABLE it_lips WITH KEY vbeln = wa_likp-vbeln TRANSPORTING NO FIELDS BINARY SEARCH.
  IF sy-subrc = 0.
    lv_indx = sy-tabix.
    LOOP AT it_lips INTO wa_lips FROM lv_indx.
      IF wa_lips-vbeln NE wa_likp-vbeln.
        EXIT.
      ENDIF.
      counter = counter + 1.
    ENDLOOP.
  ENDIF.
ENDLOOP.


Technique 2:

  SORT: it_likp BY vbeln,
       it_lips BY vbeln.
    lv_tabix = 1" Set the starting index 1
    CLEAR counter.
    LOOP AT it_likp ASSIGNING <lfs_likp>.
     " Start the nested LOOP from the index
      LOOP AT it_lips FROM lv_tabix
      ASSIGNING <lfs_lips>.
      " Save index & Exit the loop, if the keys are not same
        IF <lfs_likp>-vbeln <> <lfs_lips>-vbeln.
          lv_tabix = sy-tabix.
          EXIT.
        ELSE.
      " Rest of the logic would go from here...
      " counter to check how many times the actual processing was made.
        counter = counter + 1.
        ENDIF.
      ENDLOOP.
    ENDLOOP.


Technique 2 will not work if the it_likp table doesn't contain the primary key of the it_lips table.