1

Both tables sorted by key KNO

    LOOP AT lt_header INTO lwa_header.

         LOOP AT lt_items INTO lwa_item

                 WHERE key = lwa_header-KNO

                “……….

          ENDLOOP.

     ENDLOOP.

This would take more time to execute if number of entries in the table is huge. How should i modify code to improve the performance?

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
sumedh patil
  • 59
  • 3
  • 8
  • 2
    lt_items has to be declareed as a SORTED table with the field "key" as key (another option would be the declare a secondary non-unique sorted key - this depends on how the table will be read in the whole program). Add to the question, how the internal tables are declared now. – József Szikszai Oct 13 '21 at 07:13
  • 1
    If you say "sorted" to mean that you use the statement `SORT` because the internal table is of type "standard" (declared with `TYPE TABLE` or `TYPE STANDARD TABLE` or `OCCURS 0`), then it's not efficient at all, and you should use an internal table of type "sorted", as József said. – Sandra Rossi Oct 13 '21 at 09:36
  • 1
    Do you get the data from those two internal tables via two SELECTs on two different database tables? In that case consider doing a database JOIN. that way the database can take care of pairing the entries between those two tables. It will then give you a result set where you have each entry of the item table together with the corresponding fields of the header table. – Philipp Oct 13 '21 at 11:00
  • 1
    Does this answer your question? [Optimize LOOP AT with conditions =, >=, <= in WHERE](https://stackoverflow.com/questions/36965601/optimize-loop-at-with-conditions-in-where) – Suncatcher Oct 18 '21 at 06:35

2 Answers2

5

You should be able to improve the lookup in the second table by using a secondary table key. Such a key needs to be declared when you declare the table variable:

DATA lt_items TYPE TABLE OF whatever WITH NON-UNIQUE SORTED KEY k1 COMPONENTS key.

You can then use this key to accelerate lookups in that table from O(n) or O(log n) time complexity:

LOOP AT lt_header INTO lwa_header.
  LOOP AT lt_items INTO lwa_item
          USING KEY k1
          WHERE key = lwa_header-KNO.
    "...
  ENDLOOP.
ENDLOOP.

When you can guarantee that the values in lt_items-key are always unique (there are no two lines with the same value for "key"), then you can even use a hashed key, which is even faster (constant time):

DATA lt_items TYPE TABLE OF whatever WITH UNIQUE HASHED KEY k1 COMPONENTS key.

LOOP AT lt_header INTO lwa_header.
  READ TABLE lt_items INTO lwa_item
          WITH TABLE KEY k1
          COMPONENTS key = lwa_header-KNO.
  IF sy-subrc = 0.
     "...
  ENDIF.
ENDLOOP.
Philipp
  • 67,764
  • 9
  • 118
  • 153
4

You can use parallel cursor. It's a good technique for performance improvements in nested loops. For more information check this link.

Also field symbols are better for performance.

DATA lv_tabix TYPE sy-tabix.

SORT: lt_header BY kno,
      lt_items BY kno.

LOOP AT lt_header ASSIGNING FIELD-SYMBOL(<lfs_header>).
 READ TABLE lt_items TRANSPORTING NO FIELDS
                     WITH KEY kno = <lfs_header>-kno
                     BINARY SEARCH.
 lv_tabix = sy-tabix.
 LOOP AT lt_items FROM lv_tabix ASSIGNING FIELD-SYMBOL(<lfs_item>).
   IF <lfs_header>-kno <>  <lfs_item>-kno.
     EXIT.
  ENDIF.
  "Your logic should be here..
 ENDLOOP.
ENDLOOP.
Eray
  • 157
  • 1
  • 8