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?

4
  • 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. Commented Oct 13, 2021 at 7: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. Commented Oct 13, 2021 at 9: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. Commented Oct 13, 2021 at 11:00
  • 1
    Does this answer your question? Optimize LOOP AT with conditions =, >=, <= in WHERE Commented Oct 18, 2021 at 6:35

2 Answers 2

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.
Sign up to request clarification or add additional context in comments.

2 Comments

Got it . Thanks man .
@sumedhpatil When this answer answered your question, please accept it by clicking the checkmark-icon next to it. That way the question appears as answered in the question list.
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.

2 Comments

why do you sort lt_header?
Actually it doesn't mandatory for header. Just I feel comfortable when I was debugging.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.