Application Development and Automation 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: 
Read only

Read table binary search vs indexed loop inside loop

Former Member
0 Likes
1,839

Hello All,

it1 entries 70k (internal table)

it2 entries 150k (it1 to it2, 1 to n )

it3 entries 70k (it1 to it3, 1 to 1 )

it4 entries 70k (it1 to it4, 1 to 1 )

it5 entries 150k (it2 to it5, 1 to 1 )

1. is it better to loop through it1 and then loop through it2 using indexed loop. then further read stmt with binary search on 1 to 1 relationship table inside indexed loop.

or

2. it is better to loop through it2 and then use read using binari search on each table.

which one is better performance wise.

I appreciate any reply on tyhe same.

Mani

Edited by: Mani on Jan 26, 2009 7:21 PM

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,017

+ Use sorted tables!

With standard tables it is possible but more complicated,

read the blog

Measurements on internal tables: Reads and Loops:

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

+ get a structure like this one:


LOOP AT itab1
   LOOP   AT   itab2  WHERE key1 = wa1-key
   ....
   ENDLOOP.

   READ TABLE itab3 WITH key = wa1-key.

   LOOP   AT   itab4  WHERE key1 = wa1-key
   ....
   ENDLOOP.
ENDLOOP.

Not more than two loops should be open.

Siegfried

4 REPLIES 4
Read only

Former Member
0 Likes
1,017

in my point of view first one is good.

also sort the table before binary search other wise it wiil not increas performance

Read only

Former Member
0 Likes
1,018

+ Use sorted tables!

With standard tables it is possible but more complicated,

read the blog

Measurements on internal tables: Reads and Loops:

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

+ get a structure like this one:


LOOP AT itab1
   LOOP   AT   itab2  WHERE key1 = wa1-key
   ....
   ENDLOOP.

   READ TABLE itab3 WITH key = wa1-key.

   LOOP   AT   itab4  WHERE key1 = wa1-key
   ....
   ENDLOOP.
ENDLOOP.

Not more than two loops should be open.

Siegfried

Read only

0 Likes
1,017

I might be in need of this but i am using like this

LOOP AT itab1

read table <it3> with key <field> binary search

read table <it4> with key <field> binary search.

read table <it2> with key <field> binary search.

indexofit = sy-tabix.

while exit is true.

read table <it2> with index indexofit.

check if conditions met or exit from this while loop.

read table <it5> with key <field> binary search.

create final table from all entries in it1, it2, it3, it4,it5.

endwhile.

i think ur looping as below is called nested loop but above one is called indexed loop.

LOOP AT itab2 WHERE key1 = wa1-key

....

ENDLOOP.

READ TABLE itab3 WITH key = wa1-key.

LOOP AT itab4 WHERE key1 = wa1-key

....

ENDLOOP.

ENDLOOP.

actually i want to avoid loop(or indexed loop) inside loop. So according to you nested loop or Indexed loop are better than to loop on big table then read with binary right???

Mani

Read only

Former Member
0 Likes
1,017

I does not really matter how you call it,


LOOP AT itab1
  LOOP AT sort2 WHERE 
  ENDLOOP.
ENDLOOP.

is the simpelst solution for your task, and it is as good in performance as any other

solution, see blog


LOOP AT itab1
   READ TABLE itab2 .... BINARY SEARCH.
   idx2 = sy-tabix.

   LOOP AT itab2 FROM idx2.
      IF ( condition .... not fullfilled).
        EXIT.
      ENDIF.
   ENDLOOP.
ENDLOOP.

You can always read recommendations such as 'Avoid nested loop', but they are as useful

as recommendation 'Avoid eating, the food can be poisoned'. Either you must compare the tables are you don't have to compare them.

There is no fundatmental difference between a READ inside a LOOP and a LOOP with a condition inside a LOOP.

Siegfried