2015 Mar 04 6:00 AM
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.
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.
2015 Mar 04 6:11 AM
Hi Barani,
Can you share the full code.
Regards
Rajkumar Narasimman
2015 Mar 04 6:16 AM
use field-symbols, do not use internal tables with header lines. That should be your 1st step.
2015 Mar 04 7:17 AM
Hi Barani,
1) Sort the Internal table before using where condition in loop,
2) Try to avoid using perform inside loop.
Regards,
Vinodkumar.
2015 Mar 04 7:57 AM
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
2015 Mar 04 8:05 AM
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
2015 Mar 04 8:10 AM
Hi Barani,
There are two concerns here.
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
2015 Mar 04 8:57 AM
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.
| User | Count |
|---|---|
| 6 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |