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: 

How to make this code go faster?

Former Member
0 Kudos
222

Hi,

I have this code and its really slow. My question is how to make it faster since its taking about 10 hours.

Code:

  loop at ti_equi.
    select matnr sernr lief_nr kunde datum uzeit bwart posnr
 appending corresponding fields of table ti_ser01
      from z_objk_ser01
     where sernr eq ti_equi-sernr
       and matnr eq ti_equi-matnr.
  endloop.

  delete ti_ser01 where not ( kunde in s_kunnr and
                              bwart = '631' ).
  sort ti_ser01 by matnr sernr datum descending uzeit descending.
  delete adjacent duplicates from ti_ser01 comparing matnr sernr.

What this code does is fetch all these fields and then just keeps the one that's been modified at last.

I have created an index by sernr and matnr in table objk which makes it just a little bit faster.

Any helpful answer will be appreciated.

Regards,

Roberto

Edited by: Julius Bussche on Jan 27, 2009 4:29 PM

Code tags added. Please use more meaningfull subject titles

1 ACCEPTED SOLUTION

Former Member
0 Kudos
190

Hi Robetro,

Generally avoid SELECT inside LOOPs. Get the data from database tables into internal tables and use either LOOP in a LOOP (or) READ statement inside a LOOP.

I suppose in your casae you can use JOIN between EQUI and Z_OBJK_SER01 to get the data.

Thanks,

Vinay

Edited by: Vinaykumar G on Jan 27, 2009 9:00 PM

8 REPLIES 8

Former Member
0 Kudos
191

Hi Robetro,

Generally avoid SELECT inside LOOPs. Get the data from database tables into internal tables and use either LOOP in a LOOP (or) READ statement inside a LOOP.

I suppose in your casae you can use JOIN between EQUI and Z_OBJK_SER01 to get the data.

Thanks,

Vinay

Edited by: Vinaykumar G on Jan 27, 2009 9:00 PM

Former Member
0 Kudos
190

First of all, have you used ST05 to make sure that the SELECT is actually using your index?

Rob

0 Kudos
190

Yes i have. It is using it.

Isnt there a way just to fetch the last modified register from the table SER01?

Regards,

Roberto

Former Member
0 Kudos
190

Hi roberto

please avoid select statment inside loop.

use code

-


data: index type sy-tabix.

data : ti-ser011 type standard table of zobjk-ser01 with header line.

select matnr sernr lief_nr kunde datum uzeit bwart posnr

from z_objk_ser01 into table ti_ser01.

sort ti_ser01 by sernr matnr.

sort ti_equi by sernr matnr.

index = 1.

loop at ti_equi.

loop at ti_ser01 from index

if ti_ser01-sernr = ti_equi-sernr

and ti_ser01-matnr = ti_equi-matnr.

move-corresponding ti_ser01 to ti_ser011.

append ti_ser011.

index = sy-tabix.

exit.

endif.

endloop.

endloop.

here i have used one more internal table ti_ser011.

you can delete specific record from ti_ser01 internal table on condition. at that time u don't need to create any other internal table like ti-ser011.

in place of loop select statement---endloop.

you can use this code. I think it will help you.

please find if it is udeful.

Former Member
0 Kudos
190

1) Avoid select query within the Loop -


instead fetch the records of the select query into an internal table and read them within main Loop.

2) Avoid appending corresponding field's statement within the select query

It will be of much help.

Regards,

CK

Former Member
0 Kudos
190

1.dont use select within loop.

2. dont use correspondig .

3.use where condition rather than delete outside the loop.

former_member194613
Active Contributor
0 Kudos
190

there are actually quite a lots of bugs in such a short pice of coding


     from z_objk_ser01
     where sernr eq ti_equi-sernr
     and matnr eq ti_equi-matnr.
    endloop.
 
    delete ti_ser01 where not ( kunde in s_kunnr and
                            bwart = '631' ).

How is the table z_objk_ser01 defined, it must be different from ser01.

Why don't you check kunde and bwart in WHERE-condition, it is not a good idea to select the data

and delete it later, it is actually the worst you can do.


   delete adjacent duplicates from ti_ser01 comparing matnr sernr.

It is much better to do that on ti_equi, save again records to be selected.

If there are still duplicates, then do it again, the operations on the internal tables are fast.

Siegfried

Former Member
0 Kudos
190

Thanks for all your answers!!

Regards,

Roberto