Skip to Content
avatar image
Former Member

how to avoid the loop inside the loop? as it reduces the performance.

hi masters,

i have 2 internal tables having 1 same field. eg table itab having f1, f2, f3 fields and itab1 having f1, f4 and f5 fields. let us consider the data in the itab and itab1 is like below given

itab-f1 itab-f2 itab-f3 itab1-f1 itab1-f4 itab1-f5

10 aa 11 10 abc 456

10 bb 15 10 def 655

10 ff 13 10 ghi 456

11 dd 16 10 tre 455

11 zz 24 11 ftr 256

11 ii 54 11 kjh 556

12 hh 24 12 fjk 751

now i want the result in final table like below

f1 f2 f3 f4 f5

10 aa 11 abc 456

10 aa 11 def 655

10 aa 11 ghi 456

10 aa 11 tre 455

10 bb 15 abc 456

10 bb 15 def 655

10 bb 15 ghi 456

10 bb 15 tre 455

10 ff 13 abc 456

10 ff 13 def 655

10 ff 13 ghi 456

10 ff 13 tre 455

11 dd 16 ftr 256

11 dd 16 kjh 556

11 zz 24 ftr 256

11 zz 24 kjh 556

11 ii 54 ftr 256

11 ii 54 kjh 556

12 hh 24 fjk 751

i can get this result using the

Loop at itab.

loop at itab1 where f1 = itab-f1.

process....

endloop.

endloop.

but it is very slow. i want to avoid this loop inside the loop and i want to implement the same program using read table.. like below..

Loop at itab.

Read table itab1 with key ....

process...

endloop.

Is it possible? to get multiple records from itab2 using read statement or with READ statement that using CASE Statement or IF statemetnt.

Plz help me...

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

6 Answers

  • avatar image
    Former Member
    Mar 09, 2009 at 06:13 AM

    You can try by looping the 2nd internal table and reading the 1st internal table as i think you have some key firlds in the 1st table

    for exaple:

    1st itab.

    10 aa 11

    is formming some key field...

    so by looping the 2nd table and reading the 1st tablw with the key will reduce some loop processing..

    Add comment
    10|10000 characters needed characters exceeded

  • Mar 09, 2009 at 06:16 AM

    before trying to use the READ statement (which by the way is possible, but is weird whenever you want to retrieve multiple records), have you considered using sorted (hashed tables) with (NON)-unique keys.

    This will improve performance considerably.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Mar 09, 2009 at 06:25 AM

    Hello Vicky

    A MUST read is the blog

    [Performance of Nested Loops|/people/rob.burbank/blog/2006/02/07/performance-of-nested-loops]

    by Rob Burbank. Perhaps you find some useful tips & tricks in there.

    Regards

    Uwe

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Mar 09, 2009 at 06:42 AM

    hi,

    please try the following code

    TYPES:
      begin of ty_itab2,
       f1
       f2
       f3
       f4
       f5
      end of ty_itab2.
    
    DATA:
      lw_tab2tmp type ty_itab2,
      lt_tab2tmp type standard table of ty_itab2.
    
    sort it_itab by f1.
    loop at it_itab into lw_itab.
      at new f1.
        CLEAR lt_tab2tmp[].
        loop at it_itab1 into lw_itab1 where f1 = lw_itab-f1.
          CLEAR lw_tab2tmp.
          move-corresponding lw_itab1 to lw_tab2tmp.
          append lw_tab2tmp into lt_tab2tmp.
        end loop.
        "while there is no data with f1 in it_itab1
        if lt_tab2tmp is initial.
          lw_tab2tmp-f1 = lw_itab-f1.
          append lw_tab2tmp into lt_tab2tmp.
        endif.
      end at.
      CLEAR lw_tab2tmp.
      MOVE-CORRESPONDING lw_itab to lw_tab2tmp.
      modify lt_tab2tmp from lw_tab2tmp TRANSPORTING f2 f3 where not f1 is initial.
      append lines of lt_tab2tmp into lt_itab2.
    end loop.
    

    lt_itab2 is the final table.

    There may be some syntactically errors.

    I have not a system to test it now, please test it.

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      first have an internal table with 5 fields.(itab3)

      do move corresponding to move the contents of the internal table containing f1 f2 f3 to itab3.

      then loop this table ,

      and read the table with f4,f5 using binary search ( dont forget to sort this int table) and move the contents if sy-subrc is equal to 0.

  • avatar image
    Former Member
    Mar 09, 2009 at 06:49 AM

    Hi,

    try the following code to avoid nested loops.

    LOOP AT ITAB.

    READ TABLE ITAB1 WITH KEY f1 = ITAB-f1 BINARY SEARCH.

    IF SY-SUBRC = 0.

    *processing

    ENDIF.

    ENDLOOP.

    Hope this code bit helps you.

    Add comment
    10|10000 characters needed characters exceeded

  • Mar 09, 2009 at 08:06 AM

    Sometimes you can not avoid the loop in loop, but it is no problem, if there

    is where condition on the inner loop

    AND if you either take a sorted table or hashed table for the inner loop

    For standard tables, there is also an approach, see here section 3, it is based on a read binary search.

    Measurements on internal tables: Reads and Loops:

    /people/siegfried.boes/blog/2007/09/12/runtimes-of-reads-and-loops-on-internal-tables

    Siegfried

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Use a parallel cursor here rather than a nested loop. It will greatly improve your performance. The link above on nested loops mentioned by Uwe Schieferstein will suffice. There are other examples on the forum too that will help you understand the conepts. Its performance is close to read table using binary search. Read table will let to read a single record that matches your key. Using binary search in read table will improve your performance. Remember that for using binary search/parellel cursor your internal tables must be sorted on the key.

      Edited by: ZAFCO ABAP on Mar 9, 2009 4:15 PM