Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Avoiding nested Loops

Former Member
0 Kudos
6,506

Hi ,

My loop condition is taking much time for execution. The tables used are BKPF & BSEG. Please find the condition given below.

LOOP AT T_BKPF WHERE SUPER = T_SUPER-NAME.

IF USNAM NE T_BKPF-USNAM.

MOVE T_BKPF-UNAM TO H-USNAM.

NEW-PAGE.

ENDIF.

PERFORM OUT.

LOOP AT T_BSEG WHERE BELNR = T_BKPF-BELNR

                              AND     GJAHR = T_BKPF-GJAHE.

CASE T_BSEG-KOART.

WHEN 'L'.

PERFOM....

...................

ENDCASE.

ENDLOOP.

ENDLOOP.

Please help me to over come this issue.

7 REPLIES 7

rajkumarnarasimman
Active Contributor
0 Kudos
2,354

Hi Barani,

Can you share the full code.

Regards

Rajkumar Narasimman

Former Member
0 Kudos
2,354

use field-symbols, do not use internal tables with header lines. That should be your 1st step.

vinodkumar_thangavel
Participant
0 Kudos
2,354

Hi Barani,

1) Sort the Internal table before using where condition in loop,

2) Try to avoid using perform inside loop.

Regards,

Vinodkumar.

Former Member
0 Kudos
2,354

Hi Barani,

SORT T_BSEG BY belnr gjahr.

LOOP AT T_BKPF WHERE SUPER = T_SUPER-NAME.

IF USNAM NE T_BKPF-USNAM.

MOVE T_BKPF-UNAM TO H-USNAM.

NEW-PAGE.

ENDIF.

PERFORM OUT.

READ TABLE T_BSEG WITH KEY BELNR = T_BKPF-BELNR GJAHR = T_BKPF-GJAHE

                           BINARY SEARCH.

IF SY-SUBRC EQ 0.

LOOP AT T_BSEG  FROM INDEX sy-tabix.

IF T_BSEG-BELNR EQ T_BKPF-BELNR AND T_BSEG-GJAHR = T_BKPF-GJAHE.

CASE T_BSEG-KOART.

WHEN 'L'.

PERFOM....

...................

ENDCASE.

ELSE.

     EXIT.

ENDIF.

ENDIF.

ENDLOOP.

ENDLOOP.

regards,

Archer

franz_reitmayer2
Explorer
0 Kudos
2,354

Hi,

Really hard to say without the "surrounding code". As far as I can see you've a nested loop because you're performing a join at the abap side. In general I'd delegate that to the database, but for your case it would be interessting how and why t_bkpf and t_bseg are filled. As BESG is a cluster table it's also a special case.

But to answer the question how to avoid nested loops: I'd recommend to join at database.

Regards,

Franz

rajkumarnarasimman
Active Contributor
0 Kudos
2,354

Hi Barani,

There are two concerns here.

  1. Using Nested loop which decreases the performance. Instead of that we can use parallel cursor method, which fetches the data using index method.
    1. ABAP Code for Parallel Cursor - Loop Processing - Code Gallery - SCN Wiki
  2. Second one is instead of fetching the data from BSEG cluster table, we can fetch from some index table


INDEX TABLES:

BSID- Accounting: Secondary Index for Customers (Open Items)

BSAD- Accounting: Secondary Index for Customers (Cleared Items)

BSIK- Accounting: Secondary Index for Vendors (Open Items)

BSAK- Accounting: Secondary Index for Vendors (Cleared Items)

BSIS- Accounting: Secondary Index for G/L Accounts (Open Items)

BSAS- Accounting: Secondary Index for G/L Accounts (Cleared Item)


Please find the link below.


How SAP addressed the performance issues with cluster table BSEG? - ABAP Development - SCN Wiki

Regards

Rajkumar Narasimman

0 Kudos
2,354

Hi Barani,


If nested loop cannot be able to avoid, please try with below sample if it helps.

it's a litter bit difference with the code from Dengyong Zhang.


T_BKPF_BACKUP = T_BKPF.

* Remove all unneeded entry to improve performance

DELETE T_BKPF_BACKUP WHERE SUPER <> T_SUPER-NAME.

SORT: T_BKPF_BACKUP BY BUKRS BELNR GJAHR,

            T_BSEG                 BY BUKRS BELNR GJAHR.

lv_tabix = 1.  "Set the starting index 1

LOOP AT T_BKPF_BACKUP.

IF USNAM NE T_BKPF-USNAM.

MOVE T_BKPF-UNAM TO H-USNAM.

NEW-PAGE.

ENDIF.

PERFORM OUT.

  LOOP AT T_BSEG FROM lv_tabix.

*   Save index & Exit the loop, if the keys are not same

    IF T_BKPF_BACKUP-BUKRS <> T_BSEG-BUKRS

      OR T_BKPF_BACKUP-BELNR <> T_BSEG-BELNR

      OR T_BKPF_BACKUP-GJAHR <> T_BSEG-GJAHR.


      lv_tabix = sy-tabix.

      EXIT.

    ENDIF.

CASE T_BSEG-KOART.

WHEN 'L'.

PERFOM....

...................

ENDCASE.

ENDLOOP.

ENDLOOP.