cancel
Showing results for 
Search instead for 
Did you mean: 

LOOP inside the LOOP

Alice_145
Discoverer
0 Kudos
283

I have a problem with performance in transformation, I found LOOP inside the LOOP, which I read can affect performance. I'm trying to change it, but I keep getting errors. Do you have an idea how I can eliminate one loop to improve the performance?

    LOOP AT it_dyn INTO wa_dyn WHERE chanm 'LCCTR'.
      SELECT *
      FROM edir
      INTO TABLE it_edir1
      WHERE iobjnm 'LCCTR' AND
      dateto '99991231' 
      hienm wa_dyn-low.
      IF sy-subrc 0.
        LOOP AT it_dir1 INTO ls_dir1.
          APPEND ls_dir1 TO it_dir.
        ENDLOOP.
      ENDIF.
    ENDLOOP.

Accepted Solutions (0)

Answers (1)

Answers (1)

donilrathod
Explorer

Firstly, it is best practice not to use select statement inside a loop. You can populate another internal table where CHANM = 'LCCTR'. Best way to do it is copy IT_DYN to IT_DYN_2 and then delete IT_DYN_2 where CHANM <> 'LCCTR' and use for all entries to get the records in the select statement based on entries in IT_DYN_2.

Secondly, if you are just copying records from one internal table to another with same structure, you can use IT_DIR[] = IT_DIR1[].

Using any of the above mentioned points, you can improve the performance drastically. I am in favor of using both if possible.

Alice_145
Discoverer
Thanks you for you're tips, as you mention I removed select statement outside the loop and it's reduce performance issue drastically..
donilrathod
Explorer
0 Kudos
Great! it is always best practice to use SELECT statement outside of loop and use READ TABLE to get the specific entry in the loop. You can sort the internal table and use BINARY SEARCH, that will help with performance as well.
Sandra_Rossi
Active Contributor
0 Kudos
BINARY SEARCH should be used only when standard tables cannot be avoided. Instead of standard tables and sort, use sorted and hashed internal tables (type sorted table, with sorted key, etc.) Refer to the official ABAP documentation as always.