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

Code optimization - how can I optimize ?

Former Member
0 Likes
1,437

Basically i_mara has 19,000 records and it loops for all recors as coded.

The program is taking much time when i_mch1 has 300,000 records(approximately 40 minutes).

But in case i_mch1 had 18,000 records, it is fast.

Below is the code:

-


FORM output_itab4_normal .

FIELD-SYMBOLS : <lwa_makt_maktx> TYPE tt_makt_maktx,

<lwa_prfrq> TYPE tt_prfrq,

<lwa_mch1> TYPE tt_mch1,

<lwa_mvke> TYPE tt_mvke.

LOOP AT i_mara INTO wa_mara.

READ TABLE i_mch1 WITH KEY matnr = wa_mara-matnr

ASSIGNING <lwa_mch1>.

IF sy-subrc = 0.

*-----For Recurring Inspection, go to marc table.

READ TABLE i_prfrq WITH KEY matnr = wa_mara-matnr

ASSIGNING <lwa_prfrq>.

IF sy-subrc = 0.

wa_final_itab-prfrq = <lwa_prfrq>-prfrq.

READ TABLE i_mvke WITH KEY matnr = wa_mara-matnr

ASSIGNING <lwa_mvke>.

IF sy-subrc = 0.

wa_final_itab-mvgr1 = <lwa_mvke>-mvgr1.

ENDIF.

*-------For material description, go to makt table.

READ TABLE i_makt_maktx WITH KEY matnr = wa_mara-matnr

ASSIGNING <lwa_makt_maktx>.

IF sy-subrc = 0.

wa_final_itab-maktx = <lwa_makt_maktx>-maktx.

ENDIF.

wa_final_itab-matnr = wa_mara-matnr.

wa_final_itab-matkl = wa_mara-matkl.

wa_final_itab-bismt = wa_mara-bismt.

wa_final_itab-zzbismt = wa_mara-zzbismt.

wa_final_itab-mtart = wa_mara-mtart.

wa_final_itab-mhdhb = wa_mara-mhdhb.

wa_final_itab-zzshtdaysdom = wa_mara-zzshtdaysdom.

APPEND wa_final_itab TO i_final_itab.

CLEAR : wa_final_itab, wa_mara.

ENDIF. " IF sy-subrc = 0.

ENDIF.

ENDLOOP.

ENDFORM. " OUTPUT_ITAB4_NORMAL

Please help me . Thanks in advance.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,389

Dear SAM,

You just use BINARY SEARCH keyword with READ TABLE. Use all key field in READ TABLE statement if possible or

what max can be possible.

Before READ TABLE statement sort the table by its key fields.

You can find then your program is running absolutely fine and quick.

Thanks

SANKU.

13 REPLIES 13
Read only

guilherme_frisoni
Contributor
0 Likes
1,389

Hi Sam,

you could start by sorting your internal table by matnr field and then use READ TABLE with BINARY SEARCH.

You could also get maktx for each material in your SELECT, using LEFT JOIN in MAKT table.

Regards,

Frisoni

Read only

Former Member
0 Likes
1,389

Sam,

Just like Frisoni said, use binary search. That will give you the most improvement in performance. Do that to all the read statements in your program. Remember to sort before hand.

Also, if you are not using <lwa_mch1> for anything within the loop, just don't assign. Use TRANSPORTING NO FIELDS option of the READ statement.

Read only

HermannGahm
Product and Topic Expert
Product and Topic Expert
0 Likes
1,389

Hi,

as already posted by the others: BINARY SEARCH might be a good idea. Make sure you sort the table only ONCE (don't sort in the loop). Alternatively you can use a sorted table for i_mch1.

Fort the other tables i_prfrq, i_mvke, i_makt_maktx the same approach might be a good idea.

Kind regards,

Hermann

Read only

Former Member
0 Likes
1,389

You can define i_mch1 as hash table - provided you will have unique records and define key fields for the internal table. This way the performance will be consistent irrespective of number of entries.

Read only

Former Member
0 Likes
1,389

So out of two below piece of codes, the first one is effective? AND IS ALWAYS BETTER TO USE THE first piece of code?

1). DATA : wa_mch1 TYPE tt_mch1.

READ TABLE i_mch1 INTO wa_mch1

WITH KEY matnr = wa_mara-matnr

BINARY SEARCH TRANSPORTING NO FIELDS.

OR

2). FIELD-SYMBOLS : <lwa_mch1> TYPE tt_mch1.

READ TABLE i_mch1 WITH KEY matnr = wa_mara-matnr

ASSIGNING <lwa_mch1>.

thanks for your help.

Read only

0 Likes
1,389

Yes definately first one, as you are using binary search and used 'transporting no field' statement. But keep in mind before using binary search, you have to sort the table accordingly. If possible and if the performace is very bad, then I will suggest to use Hash table.

Kuntal

Read only

0 Likes
1,389

You mean I need code HASHED TABLE like below ? and BELOW coding is okay ?

DATA : wa_mch1 TYPE HASHED TABLE OF tt_mch1.

READ TABLE i_mch1 INTO wa_mch1

WITH KEY matnr = wa_mara-matnr

BINARY SEARCH TRANSPORTING NO FIELDS.

Read only

0 Likes
1,389

There are some condition before using Hash table. This is kind of Key driven internal table. Your internal table should have unique key and should hold unique data. Check in forum for use of Hash table.

Kuntal

Read only

HermannGahm
Product and Topic Expert
Product and Topic Expert
0 Likes
1,389

Hi,

we have to distinguish between access cost or search cost and output or copy cost.

In oyur first example the binary search influences the access/serach cost. The transporting no fields

influences the output cost.

In the second example you are not changing the access/search cost (a linear search is used) but the

ouput cost is as well influenced with asigning.

The access/search costs are more important than the output costs.

Furthermore since you need the fields after the search: don't use transporting no fields. You should

use assigning or into (depending on th lenght of the line).

Use binary search with into or assigning in your example.

Kind regards,

Hermann

Read only

Former Member
0 Likes
1,389

Please get better informaed about internal tables:

Use the right search:

+ binary search

+ but better sorted or hashed tables

see

Measurements on internal tables: Reads and Loops:

/people/siegfried.boes/blog/2007/09/12/runtimes-of-reads-and-loops-on-internal-tables

The assigning etc fpr READ is then the last 1% optimization:

often the INTO is better

and please read TRANSPORTING NO FIELDS means no fields, i.e. there is nothing

transported this is no alternative, it is only useful, if you need no field no into, but

only the sy-subrc or sy-tab.

READ TABLE i_mch1 INTO wa_mch1

WITH KEY matnr = wa_mara-matnr

BINARY SEARCH TRANSPORTING NO FIELDS.

OR

2). FIELD-SYMBOLS : <lwa_mch1> TYPE tt_mch1.

READ TABLE i_mch1 WITH KEY matnr = wa_mara-matnr

ASSIGNING <lwa_mch1>.

but the search is much much more important

Siegfried

Read only

0 Likes
1,389

Hi,

better you try look and see at your internal table... suggest you sort it 1st and declare your internal table as Hashed Table.

[SAP Notes & Tips|http://abap4beginner.blogspot.com/]

Read only

Former Member
0 Likes
1,389

Hi,

You can refer to the following likns:

or

you can try this coding too As this avoids any looping and hence improve performance

Create another temporary internal table ie. i_final_temp.

then do the following:

referesh i_final_temp.

append lines of i_final to i_fianl_temp.

delete i_final_temp where v_box ne 'X'

describe table i_final_temp lines v_counter.

Thank You.

Regards,

Dhanalakshmi L

Read only

Former Member
0 Likes
1,390

Dear SAM,

You just use BINARY SEARCH keyword with READ TABLE. Use all key field in READ TABLE statement if possible or

what max can be possible.

Before READ TABLE statement sort the table by its key fields.

You can find then your program is running absolutely fine and quick.

Thanks

SANKU.