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: 
Read only

Time consuming in Loop ...

Former Member
0 Likes
967

Hi Experts,

Pl. have a look in the below program and explain why it takes long time in loop.

START-OF-SELECTION.

select * from mara client specified

into corresponding fields of table itab

where mandt = sy-mandt and matnr in matnr1

and matkl in ptype and mtart = 'ZFIN'.

sort itab by matnr.

select * from makt into corresponding fields of table it_makt

for all entries in itab where matnr = itab-matnr.

sort it_makt by matnr.

select * from a304 client specified

into corresponding fields of table it_a304

where mandt = sy-mandt and kschl = 'PR00'.

sort it_a304 by matnr.

select * from konp client specified

into corresponding fields of table it_konp

where mandt = sy-mandt and KSCHL = 'PR00'.

sort it_konp by knumh.

loop at itab.

it_out-matnr = itab-matnr.

it_out-matkl = itab-matkl.

read table it_makt with key matnr = itab-matnr.

it_out-maktx = it_makt-maktx.

read table it_a304 with key matnr = itab-matnr.

it_out-kschl = it_a304-kschl.

it_out-vkorg = it_a304-vkorg.

it_out-vtweg = it_a304-vtweg.

it_out-knumh = it_a304-knumh.

read table it_konp with key knumh = it_a304-knumh.

it_out-kbetr = it_konp-kbetr.

IF IT_OUT-KBETR <= '5.00'.

IT_OUT-FACTOR = IT_OUT-KBETR.

ELSE.

IT_OUT-FACTOR = ( IT_OUT-KBETR ) * '0.55'.

ENDIF.

APPEND IT_out.

endloop.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
930

**using binary search in READ statement makes it very fast, but shud be sorted

sort it_makt by matnr.

sort it_a304 by matnr.

sort it_konp by knumh.

loop at itab.

it_out-matnr = itab-matnr.

it_out-matkl = itab-matkl.

read table it_makt with key matnr = itab-matnr bimary search.

it_out-maktx = it_makt-maktx.

read table it_a304 with key matnr = itab-matnr binary search.

it_out-kschl = it_a304-kschl.

it_out-vkorg = it_a304-vkorg.

it_out-vtweg = it_a304-vtweg.

it_out-knumh = it_a304-knumh.

read table it_konp with key knumh = it_a304-knumh binary search.

it_out-kbetr = it_konp-kbetr.

IF IT_OUT-KBETR <= '5.00'.

IT_OUT-FACTOR = IT_OUT-KBETR.

ELSE.

IT_OUT-FACTOR = ( IT_OUT-KBETR ) * '0.55'.

ENDIF.

APPEND IT_out.

endloop.

8 REPLIES 8
Read only

Former Member
0 Likes
931

**using binary search in READ statement makes it very fast, but shud be sorted

sort it_makt by matnr.

sort it_a304 by matnr.

sort it_konp by knumh.

loop at itab.

it_out-matnr = itab-matnr.

it_out-matkl = itab-matkl.

read table it_makt with key matnr = itab-matnr bimary search.

it_out-maktx = it_makt-maktx.

read table it_a304 with key matnr = itab-matnr binary search.

it_out-kschl = it_a304-kschl.

it_out-vkorg = it_a304-vkorg.

it_out-vtweg = it_a304-vtweg.

it_out-knumh = it_a304-knumh.

read table it_konp with key knumh = it_a304-knumh binary search.

it_out-kbetr = it_konp-kbetr.

IF IT_OUT-KBETR <= '5.00'.

IT_OUT-FACTOR = IT_OUT-KBETR.

ELSE.

IT_OUT-FACTOR = ( IT_OUT-KBETR ) * '0.55'.

ENDIF.

APPEND IT_out.

endloop.

Read only

0 Likes
930

THanks Tripath,

YAB

Read only

Former Member
0 Likes
930

also use BINARY SEARCH option

loop at itab.

it_out-matnr = itab-matnr.

it_out-matkl = itab-matkl.

read table it_makt with key matnr = itab-matnr <b>BINARY SEARCH</b> .

<b>if sy-subrc eq 0.</b>

it_out-maktx = it_makt-maktx.

<b>endif.</b>

read table it_a304 with key matnr = itab-matnr <b>BINARY SEARCH</b> .

<b>if sy-subrc eq 0.</b>

it_out-kschl = it_a304-kschl.

it_out-vkorg = it_a304-vkorg.

it_out-vtweg = it_a304-vtweg.

it_out-knumh = it_a304-knumh.

<b>endif.</b>

read table it_konp with key knumh = it_a304-knumh <b>BINARY SEARCH</b> .

<b>if sy-subrc eq 0.</b>

it_out-kbetr = it_konp-kbetr.

<b>endif.</b>

IF IT_OUT-KBETR <= '5.00'.

IT_OUT-FACTOR = IT_OUT-KBETR.

ELSE.

IT_OUT-FACTOR = ( IT_OUT-KBETR ) * '0.55'.

ENDIF.

APPEND IT_out.

endloop.

Read only

rodrigo_paisante3
Active Contributor
0 Likes
930

Hi,

change your select, put only the fields you really need. select * is bad for performance.

START-OF-SELECTION.

select <b>field1... field n </b> from mara client specified

into corresponding fields of table itab

where mandt = sy-mandt and matnr in matnr1

and matkl in ptype and mtart = 'ZFIN'.

sort itab by matnr.

<b>if not itab[] is initial.</b>

select <b>field1... field n </b> from makt into corresponding fields of table it_makt

for all entries in itab where matnr = itab-matnr.

sort it_makt by matnr.

<b>endif.</b>

select <b>field1... field n </b> from a304 client specified

into corresponding fields of table it_a304

where mandt = sy-mandt and kschl = 'PR00'.

sort it_a304 by matnr.

select <b>field1... field n </b> from konp client specified

into corresponding fields of table it_konp

where mandt = sy-mandt and KSCHL = 'PR00'.

sort it_konp by knumh.

loop at itab.

it_out-matnr = itab-matnr.

it_out-matkl = itab-matkl.

read table it_makt with key matnr = itab-matnr.

it_out-maktx = it_makt-maktx.

read table it_a304 with key matnr = itab-matnr.

it_out-kschl = it_a304-kschl.

it_out-vkorg = it_a304-vkorg.

it_out-vtweg = it_a304-vtweg.

it_out-knumh = it_a304-knumh.

read table it_konp with key knumh = it_a304-knumh.

it_out-kbetr = it_konp-kbetr.

IF IT_OUT-KBETR <= '5.00'.

IT_OUT-FACTOR = IT_OUT-KBETR.

ELSE.

IT_OUT-FACTOR = ( IT_OUT-KBETR ) * '0.55'.

ENDIF.

APPEND IT_out.

endloop.

Read only

Former Member
0 Likes
930

<Deleted>

Sorry, the two first already given answers are right. I got something wrong.

Timo.

Read only

Clemenss
Active Contributor
0 Likes
930

Hi YUSUF BHORI,

small addition/change:


sort:
  it_makt by matnr,  
  it_a304 by matnr ,
  it_konp by knumh.
field-symbols:
  <itab> like itab,
  <makt> like it_makt,
  <konp> like it_konp.
loop at itab assigning <itab>.

it_out-matnr = <itab>-matnr.
it_out-matkl = <itab>-matkl.

read table it_makt assigning <makt>
  with key matnr = <itab>-matnr
  binary search.
it_out-maktx = <makt>-maktx.

read table it_a304  assigning <a304> 
  with key matnr = <itab>-matnr
  binary search.
  it_out-kschl = <a304>-kschl.
  it_out-vkorg = <a304>-vkorg.
  it_out-vtweg = <a304>-vtweg.
  it_out-knumh = <a304>-knumh.

read table it_konp   assigning <konp>
  with key knumh = <a304>-knumh
  binary search.
 it_out-kbetr = <konp>-kbetr.

IF IT_OUT-KBETR <= '5.00'.
IT_OUT-FACTOR = IT_OUT-KBETR.
ELSE.
IT_OUT-FACTOR = ( IT_OUT-KBETR ) * '0.55'.
ENDIF.
APPEND IT_out.

endloop

This is just´a quick shot, but I'm conviced this will do factor 8 to 20 for the loop, depenmds on data amount.

Loop assigning will not waste the time copying each table line to the header line in the loop.

READ ... BINARY SEARCH will reduce search time logarithmically.

BTW: Replace SELECT * bay select into corresponding fields. Remove CLIENT SPECIFIED - it's nonsense.

Regards,

Clemens

Read only

Clemenss
Active Contributor
0 Likes
930

sorry I was too slow but my solution is the fastest

Read only

S0025444845
Active Participant
0 Likes
930

Hi,

1)donot use select * use select< particular fields only>.

2) declare structures with types with the particular fields needed and then decclre internal tables of those structrues. like.

types: begin of ty_makt.

matnr typr makt-matnr,

maktx type makt-maktx,

end of ty_makt.

data : it_makt type standard table of ty_makt.

if itab[] is not initial.

select matnr makt into table it_makt from makt

for all entries in itab where matnr = itab-matnr.

3) use binary search before that sort the internal tables with the keys by which u r reading.like

sort it_makt by matnr.

loop at itab.

read table it_makt with key matnr = itab-matnr

binary search.

4) check sy-subrc.

if sy-subr4c is initial.

it_out-maktx = it_makt-maktx.

endif.

5) clear all header lines like

clear: it_makt.

then append it-out.

endloop.

regards,

sudha