Skip to Content
-1

Doubt performance LOOP FROM sy-tabix

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.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Best Answer
    Aug 17, 2017 at 08:11 AM

    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

    Add comment
    10|10000 characters needed characters exceeded