Search code examples
performanceabapinternal-tables

how to improve the performance in nested loops with 2 tables having huge number of entries in abap?


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?


Solution

  • 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.