Skip to Content
-1

Doubt performance LOOP FROM sy-tabix

Aug 17, 2017 at 07:30 AM

72

avatar image

Hi all,

I have a LOOP like this:

 LOOP AT tab1 ASSIGNING <tab>.
  LOOP AT tab1 FROM sy-tabix ASSIGNING <tab_aux>
       WHERE field1 = tab1-field1
        AND field2 = tab1-field2
        AND field3 = '3'.
   EXIT.
  ENDLOOP.
  IF sy-subrc EQ 0.
    "Do something
  ENDIF.
 ENDLOOP.

I need to check if in the same table i'm looping exits a line with same key fields and other field with other value...

This internal table has a lot of records (BSET, BKPF....) so the performance is very poor.

I've changed the code by this one and the now the execution is super fast.


LOOP AT tab1 ASSIGNING <tab>.
  lv_tabix = sy-tabix.
  CLEAR: lv_found.
  DO.
    ADD 1 TO lv_tabix.
    READ TABLE tab1 ASSIGNING <tab_aux> INDEX lv_tabix.
    IF sy-subrc NE 0.
      EXIT.
    ELSEIF <tab_aux>-field1 NE <tab>-field1
        OR <tab_aux>-field2 NE <tab>-field2.
      EXIT.
    ELSEIF <tab_aux>-field3 = '3'.
      lv_found = abap_true.
      EXIT.
    ENDIF.
  ENDDO.
  IF lv_found EQ abap_true.
   "Do something
  ENDIF.
ENDLOOP. 

Do you know if there is something wrong in the first code, or why is better to use the READs instead the LOOP FROM.

Regards.

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Best Answer
Bertrand DELVALLEE Aug 17, 2017 at 08:11 AM
2

Hello,

Indeed your 2 codes are very different.

First one will compare each entry from "there" to end table for (field1,field2,field3) values you want. If this triple doesn't exist you it will read all table for nothing.

Second one just stop if the next entry don't correspond to values you want.

My complexity calculations are far away but, for n entries, your first code is in "n²" order (around n * n / 2 access each run) and the second one in "n" order (2n access each run). So yes, your second code is far better ONLY IF your table is sorted by (field1,field2,field3). And sort a table need ressources too.

Best regards

Bertrand

Show 6 Share
10 |10000 characters needed characters left characters exceeded

mmm.

ok, I thought the second LOOP would end if the next entry doesn't match with the key fields...

the table is sorted so i only need to read the next entries

0

the table is sorted only by key fields (field1 and field2)

0

So your itab entries can be sorted like that :

field1; field2; field3

foo; bar; 3

foo; bar; 1

You should try the opposite : search first for field3 = 3 and then search both ways (previous and next entries) if they have same (field1;field2) ?

Something like :

LOOP AT tab1 ASSIGNING <tab> where field3 = '3'.
lv_found EQ abap_false.
w_index = sy-tabix.
w_previous_index = w_index - 1.
w_next_index = w_index + 1.
if w_previous_index > 0.
  READ TABLE tab1 ASSIGNING <tab_aux> INDEX w_previous_index.
  if sy-subrc is initial.
   if <tab_aux>-field1 = <tab>-field1 AND <tab_aux>-field2 = <tab>-field2.
    lv_found EQ abap_true.
   endif.
  endif.
endif.

if lv_found NE abap_true.
 READ TABLE tab1 ASSIGNING <tab_aux> INDEX w_next_index.
  if sy-subrc is initial.
   if <tab_aux>-field1 = <tab>-field1 AND <tab_aux>-field2 = <tab>-field2.
    lv_found EQ abap_true.
   endif.
  endif.
endif.

if lv_found EQ abap_true.
" do something with <tab_aux> 
endif.
ENDLOOP.
1

well... the field3 = 3 was only an example, its actually a bit more complicated. I need to read several times to the internal table, reading diferent values. And could be more than 2 lines with same key... but thanks for the effort.

0

So your problem is solved as you now know why 2nd code is faster and have all in your hands ?

0

Yes, I just wondered why the second code was faster ... I misunderstood how the LOOP FROM was working.

0