Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Alternative for nested loops(Performance Issue)

Former Member
0 Kudos

Hi,

The following code has performance issues:

   LOOP AT itab_mara.
    LOOP AT itab_store.
      MOVE itab_mara-matnr TO imara-matnr.
      MOVE itab_store-werks TO imara-werks.
      APPEND imara.
    ENDLOOP.

  ENDLOOP.

I have two internal tables one is Itab_mara having one field called matnr and other internal table itab_store having werks field and the data from both the tables

need to be added in the final internal table imara.

Please guide me how to achieve the above code without using nested loop so that i dont get the performance issue.

Thanks.

5 REPLIES 5

ThomasZloch
Active Contributor
0 Kudos

You need a common key field in both tables (otherwise this doesn't seem to make sense), and the inner table must be declared as sorted with a key that matches the where-conditions for the inner loop.

Please tell more about the context of this issue, there might be a better solution altogether. For example, often complicated internal table handling can be avoided if the required data is selected at once using properly constructed JOIN selects, if technically possible.

Thomas

former_member189779
Active Contributor
0 Kudos

from which tables you are selecting the data in your internal tables? You should select MATNR and WERKS into your itab_store as well.

Then

SORT both tables by MATNR.

LOOP AT ITAB_MARA.

READ TABLE ITAB_STORE WITH KEY MATNR = ITAB_MARA-MATNR BINARY SEARCH.

IF SY-SUBRC = 0.

MOVE itab_store-werks TO imara-werks.

  MOVE itab_mara-matnr TO imara-matnr.

ENDIF.

append Final table

ENDLOOP.

Former Member
0 Kudos

Hi Anuradha,

U need to use parallel cursor processing technique here to processing a loop within a loop to avoid any performance issue. In this technique, we call the records of the 2nd internal table by the matched indexes using sy-tabix.

Below is a sample parallel cursor technique code for your reference.

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

Plz let me know if this helps and revert in case of issues.

regards

Vivek

raymond_giuseppi
Active Contributor
0 Kudos

Does the itab_store comes from a table like MARC where MATNR is present, if yes try something like

SORT itab_store BY matnr.

SORT itab_mara BY matnr.

LOOP AT itab_store INTO wa_store.

  AT NEW matnr.

    CLEAR wa_final.

    READ TABLE itab_mara INTO wa_mara WITH KEY matnr = wa_store-matnr BINARY SEARCH.

    MOVE-CORRESPONDING wa_mara TO wa_final.

  ENDAT.

  MOVE-CORRESPONDING wa_store TO wa_final.

  APPEND wa_final TO itab_final.

ENDLOOP.

(Did you hide a WHERE clause in the posted code of your question ?)

Regards,

Raymond

former_member491621
Contributor
0 Kudos

Hi Anirudha,

You can try using the code given by Vivek. There is 1 more alternative way of doing it.

SORT itab_mara ASCENDING BY matnr.

SORT itab_store ASCENDING BY matnr.

lv_tabix = 1.          (this variable is of type 'i' or sytabix)

   LOOP AT itab_mara.
    LOOP AT itab_store FROM lv_tabix.

     IF itab_mara-matnr NE itab_store-matnr.

          lv_tabix = sy-tabix.

          EXIT.

     ENDIF.

      MOVE itab_mara-matnr TO imara-matnr.
      MOVE itab_store-werks TO imara-werks.
      APPEND imara.
    ENDLOOP.

  ENDLOOP.

You can also use internal table of type SORTED TABLE for the inner internal table specifying the key and using where in the inner loop.

Hope this helps