Application Development 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: 

Performance tuning LOOP within a LOOP

Former Member
0 Kudos

Hi experts,

Good Day!

I need your help about a program that i will apply performance tuning because the program is very slow. there was one internal table that will retrieve 23,000 data entries that will be use in the loop. can you help me how the program's performance will be fast.

SELECT aufnr

auart

autyp

werks

astnr

objnr

INTO TABLE i_aufk FROM aufk

WHERE aufnr IN s_aufnr

AND auart IN s_auart

AND werks IN s_werks.

LOOP AT i_aufk INTO x_aufk.

LOOP AT i_akpo INTO x_akpo WHERE aufnr = x_aufk-aufnr.

ENLOOP.

ENLOOP.

20 REPLIES 20

Former Member
0 Kudos

did you run SE30 and found the potential issues?

Former Member
0 Kudos

Hi,

Try this.

Please avoid using Loop inside Loop.

Use Read.

LOOP AT i_aufk INTO x_aufk.

Read table i_akpo INTO x_akpo with key aufnr = x_aufk-aufnr..

ENLOOP

regards

Avi

Former Member
0 Kudos

Hi, when you LOOP AT i_aufk, you can found only one record in AFKO and AFPO, you can sort the two internal table by production order number(AUFNR), and use read table i_akpo INTO x_akpo with key aufnr = x_aufk-aufnr. If you don't need to use i_akpo in other places, you can delete the record from i_akpo when you got it with the statement 'read tabe'.

former_member598013
Active Contributor
0 Kudos

Hi Jrvillacortz ,

Please see the updated code for the perfomance of your code.




SELECT aufnr
auart
autyp
werks
astnr
objnr
INTO TABLE i_aufk FROM aufk
WHERE aufnr IN s_aufnr
AND auart IN s_auart
AND werks IN s_werks.

LOOP AT i_aufk INTO x_aufk.


Read table i_akpo intox_akpo with key aufnr = x_aufk-aufnr.
if sy-subrc = 0.
endif.
ENLOOP.

This will enhance your performance.

Thanks,

Chidanand

Former Member
0 Kudos

Hi experts,

I think we cannot LOOP and READ inside the LOOP because it will only get one line of the data entries. i am experementing what to do in this kind of scenario. thanks for the help.

~thanks

0 Kudos

Hi ,

I have already mention you that parallel Cursor ,used that to improve performance

Regards,

Tarak

Former Member
0 Kudos

Hi ,

This is correct way to use loop in loop.:

Sort i_aufk by aufnr.

Sort i_akpo by aufnr.

LOOP AT i_aufk INTO x_aufk.

read table i_akpo with key aufnr = x_aufk-aufnr..

if sy-subrc = 0.

LOOP AT i_akpo INTO x_akpo from sy-index.

if x_akpo-aufnr ne x_aufk-aufnr.

exit.

endif.

ENLOOP.

endif.

ENLOOP.

Former Member
0 Kudos

Hi,

Try this:

SORT i_aufk by aufnr.

SORT i_akpo by aufnr.

SELECT aufnr

auart

autyp

werks

astnr

objnr

INTO TABLE i_aufk FROM aufk

WHERE aufnr IN s_aufnr

AND auart IN s_auart

AND werks IN s_werks.

LOOP AT i_aufk INTO wa_aufk.

READ TABLE i_akpo into wa_akpo with key aufnr = wa_aufk-aufnr BINARY SEARCH.

if sy-subrc = 0.

" do something

endif.

ENLOOP.

Regards,

Saba

Former Member
0 Kudos

Hi,

if you have already data available in i_aupo...then read that table in the loop of i_auko.

suppose if you want to loop the i_aupo table...

then plz check the give modified code.

data:w_index LIKE sy-index.

SELECT aufnr

auart

autyp

werks

astnr

objnr

INTO TABLE i_aufk FROM aufk

WHERE aufnr IN s_aufnr

AND auart IN s_auart

AND werks IN s_werks.

sort i_auko by aufnr.

sort i_aupo by aufnr.

LOOP AT i_auko.

LOOP AT i_aupo FROM w_index.

if i_auko-aufnr ne i_aupo-aufnr.

w_index = sy-tabix.

exit.

endif.

endloop.

endloop.

********************

**for using read statement..

sort i_auko by aufnr.

sort i_aupo by aufnr.

loop at i_auko.

read table i_aupo with key aufnr = i_auko-aufnr binary search.

if sy-subrc = 0.

.........

endif.

endloop.

Regards,

venkat

Former Member
0 Kudos

Hi jrvillacortz ,

To improve the performance that use the nested loop , used parallel cursor .

Used that code

sort: t_likp, t_lips

LOOP AT t_likp INTO likp.

LOOP AT t_lips INTO lips FROM w_index.

IF likp-vbeln NE lips-vbeln.

w_index = sy-tabix.

EXIT.

ENDIF.

ENDLOOP.

ENDLOOP.

Regards,

Tarak

Former Member
0 Kudos

Hi,

First so sort on two internal tables.

eg: vbak(Sales order header details) and vbap(Sales order line items)

now select all the entries in ur internal tables v1 and v2.

sort v1.

sort v2.

loop at v2.

read table v1 with key vbeln = v2-vbeln.

sy-subrc = 0.

endloop.

Regards

Martin.

Former Member
0 Kudos

Hi experts,

I have tried already what you have suggested but the performance is negligible to the program in other words the program is still slow. I'm trying to find another work around for this.

~thanks

0 Kudos

Hi friend,

This is correct only way to use loop in loop :

Sort i_aufk by aufnr.

Sort i_akpo by aufnr.

LOOP AT i_aufk INTO x_aufk.

read table i_akpo with key aufnr = x_aufk-aufnr..

if sy-subrc = 0.

LOOP AT i_akpo INTO x_akpo from sy-index.

if x_akpo-aufnr ne x_aufk-aufnr.

exit.

endif.

ENLOOP.

endif.

ENLOOP.

Please let me know u r views

Kiran

matt
Active Contributor
0 Kudos

>

> Hi friend,

>

> This is correct only way to use loop in loop :

>

>

> Sort i_aufk by aufnr.

> Sort i_akpo by aufnr.

>

> LOOP AT i_aufk INTO x_aufk.

> read table i_akpo with key aufnr = x_aufk-aufnr..

> if sy-subrc = 0.

> LOOP AT i_akpo INTO x_akpo from sy-index.

> if x_akpo-aufnr ne x_aufk-aufnr.

> exit.

> endif.

> ENLOOP.

> endif.

> ENLOOP.

>

>

> Please let me know u r views

>

> Kiran

It can be improved further by making i_akpo a SORTED table with key aufnr.

0 Kudos

Hi Matt,

Thanks for u r modification and i think it will improve the performence.

Kiran

ThomasZloch
Active Contributor
0 Kudos

Combining MxG and Matt's anwer plus my own few cents:

- find out via SE30 what really is the problem

- if S_AUFNR is empty, then that select will be a problem as well

- loop inside loop is not a performance issue, if the inner loop is done via key fields of a sorted table (defined as such!)

- maybe you can use an inner join select from AUFK and whatever other table you queried to fill I_AKPO and avoid the nested loop altogether

Thomas

Former Member
0 Kudos

hi,

Sort i_aufk by aufnr.

Sort i_akpo by aufnr.

LOOP AT i_aufk INTO x_aufk.

read table i_akpo with key aufnr = x_aufk-aufnr..

if sy-subrc = 0.

LOOP AT i_akpo INTO x_akpo from sy-index.

if x_akpo-aufnr ne x_aufk-aufnr. <---after the execution of the program it will retrieve nothing

exit.

endif.

ENLOOP.

endif.

ENLOOP.

i have replace this if x_akpo-aufnr ne x_aufk-aufnr with if x_akpo-aufnr eq x_aufk-aufnr so that it will compare the aufnr entries and it will work. but still the performance is slow.

0 Kudos

hi,

Sort i_aufk by aufnr.

Sort i_akpo by aufnr.

LOOP AT i_aufk INTO x_aufk.

read table i_akpo with key aufnr = x_aufk-aufnr..

if sy-subrc = 0.

LOOP AT i_akpo INTO x_akpo from sy-index.

if x_akpo-aufnr ne x_aufk-aufnr. <---after the execution of the program it will retrieve nothing

exit.

endif.

ENLOOP.

endif.

ENLOOP.

Program is checking x_akpo-aufnr ne x_aufk-aufnr condition and AKPO AUFNR and AUFK-AUFNR are not same the it is exiting i_akpo loop.

that means this is terminating i_akpo loop and continuing with i_aufk loop.

Former Member
0 Kudos

Hi Expert's,

A lot of Thanks! for the suggestion's & help.

Former Member
0 Kudos

Hi Experts,

Thanks!

Usinf field symbol and parallel cursor it will improve the programs performance.