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: 

Perfomance Optimization

Former Member
0 Kudos
392

Hi Experts,

I need to done a performance optimization for a program. when i saw the program the following performance issues found.

1) there r lot of nesting LOOPs. Below is the appoximate loop structure. How much performanance impact with below type of Nesting LOOP.

Ex:

LOOP AT tb_seg.

Loop at tb_seg1.

endloop.

Loop at tb_send.

Select single

Loop at tb_rec.

loop at tb_rec1.

almost 6 select single statements was there on different conditions.

endloop.

endloop.

endloop.

endloop.

2) Select Single... Statements were in LOOP..ENDLOOP. How much performanance impact .

3) like above specified LOOPs there are 4 Subroutines in that program.

4) In some areas WHILE loop also there in LOOP......ENDLOOP. MOVE-CORRESPONDING also used in LOOP..ENDLOOP.

Please tell me how to improve the performance of program.

Thanks in Advance.

1 ACCEPTED SOLUTION

Former Member
0 Kudos
160

Hello Siva,

For Q:4

You need to twist code as per logic. If WHILE statement is having proper conditions and used appropriately then should be OK.

For move-corresponding if select f1, f2 is matching with internal table then should not be used as it is a additional task for ABAP to decide where the field data should go in internal table. It is expensive statement than the normal select f1, f2 into itab.

In some situation you can not avoid corresponding as those internal table are accessed with some sequence of fields.

Using select inside loop in normal conditions is not acceptable. It should be outside the loop.

Thank You,

Nishikant.

13 REPLIES 13

Former Member
0 Kudos
160

Please give some performance standards to improve performance

0 Kudos
160

Hi Siva,

I hope the following information would help.

>Avoid selecting from DB within LOOP, instead use FOR ALL ENTRIES clause.

>Wherever possible avoid using INTO CORRESPONDING FIELDS.

>Avoid usage of aggregate functions

>Check if proper index is being used and also whether primary key fields are used in the WHERE clause.

You can compare the performance improvement, by accessing the report RSHOWTIM, path from SE38 menu -> Environment -> Examples -> Performance Examples. You can enter the existing SQL statement on one of the windows and the tuned one on the other and compare the runtime. There are lot of examples available.

Regards,

Rahul

0 Kudos
160

1. First avoid Select single inside the loop it would improve the performance a lot, instead of the first pick up all the entries out side the loop by using for all entries and use the read statement with binary search inside the loop.

2. Nested loop, If its very much required than try to use do-while inside the loop

Regards

Sasi

Former Member
0 Kudos
160

This message was moderated.

Former Member
0 Kudos
160

Hi Siva,

-->database hitting with in the loop will leads to the bad performace. instead fetch all the required data at a time and while processing you can filter the things.

--> if it is compulsory that you need use the nestead loops. so try to avoid that and instead use the "Parallel Cursor method" . it will increase your performance.

Hope these tips useful to you.

Former Member
0 Kudos
160

>

> Hi Experts,

> I need to done a performance optimization for a program. when i saw the program the following performance issues found.

>

> 1) there r lot of nesting LOOPs. Below is the appoximate loop structure. How much performanance impact with below type of Nesting LOOP.

> Ex:

> LOOP AT tb_seg.

> Loop at tb_seg1.

>

> endloop.

>

> Loop at tb_send.

> Select single

> Loop at tb_rec.

> loop at tb_rec1.

> almost 6 select single statements was there on different conditions.

> endloop.

>

> endloop.

> endloop.

> endloop.

>

> 2) Select Single... Statements were in LOOP..ENDLOOP. How much performanance impact .

> 3) like above specified LOOPs there are 4 Subroutines in that program.

> 4) In some areas WHILE loop also there in LOOP......ENDLOOP. MOVE-CORRESPONDING also used in LOOP..ENDLOOP.

>

> Please tell me how to improve the performance of program.

>

> Thanks in Advance.

Hi Siva,

First, you have to make sure that your nested loop is justifiable. Since we have very limited information here, I can not say much on whether your program need to have 4 nested loop but in my experience, its a very rare condition that we need to have 4 nested loop in the program logic.

Try to look at the production data that will be populated in your internal table.

Performance tuning does not have generic solution for every problem. Data is one important thing that we need to consider while making the changes. Here is the main idea:

1. Always start performance tuning with running trace tools such as ST12. The logic that you wrote here seems like the cause of the poor performance of your program but you still need to do measurement to ensure that you fix the right block of code. Sometimes in a badly written program, one select statement can consume 90% of the running time of the program.

2. If your internal tables indeed have a lots of data, check the key used for your select single. I would not say that select single is a bad programming technique. Here is one simple example where I prefer using select single than select for all entries before loop.

I have itab_report and need to populate account description from SKAT table to itab_report.

itab_report itself has 15000 records but after I do data analysis I found out that most of the time I will only have 5 different account. Should I use select for all entries using that 15000 records?

It's very obvious that it will cause bottleneck to the program if we use select for all entries.

My solution will be to have itab_account and read binary search to this itab using account as the key. If no record with the same account can be found, I will select single from SKAT using account as the key and populate it into itab_account. Since we only have at most 5 different account, most of the time the program will successfully read binary search for account text and at most only do 5 select single. Isn't that a better program performance will be achieved?

3. MOVE-CORRESPONDING definitely not a good way of programming but sometimes it is justifiable if we need to move to internal table with different structure in which we can not change the structure of the internal table due to the sequence of the fields is meant for other purpose. So, once again, there is no generic solution for move-corresponding. You still need to look at the data and other related logic in the program.

As a conclusion, you need to do performance trace first as initial step before making any changes to the program. After you discover the cause, analyse the data in general. Try to analyse production data instead of development data. Hope my information can help. Thanks.

Regards,

Abraham

Former Member
0 Kudos
160

Hi Siva,

The nesting of loop affects the performance badly as for each record in one of the outer loop it leads to scan of the whole internal table in the inner loop.Moreover select statement inside the loop leads to database table access for each loop pass. All these can be avoided by using proper nesting of loop (if nesting of loop is only way to attain the functionality) using following few tips:

1) Get the data into an internal table from database outside the loop using the conditions and using for all entries, sort the internal table with appropriate key with which the internal table will be read with that key and read it using binary search inside the loop. This will improve the performance reducing the load from database server as there will be only one database fetch for the entire processing.

2) Some times loop...

loop at <itab> where <condition>

.....

.....

endloop.

endloop.

is used.

The loop at where is used to have all the records that are satisfying the where condition.In this case just using the read statement will give only the first record satisfying the condition.For performance improvement This case can be addressed in better way like following..

loop at it_item.

read table it_vbep with key vbeln = it_item-vbeln

posnr = it_item-posnr

binary search

Transporting no fields .

if sy-subrc = 0.

loop at it_vbep from sy-tabix .

if it_vbep-vbeln ne it_item-vbeln or

it_vbep-posnr ne it_item-posnr.

exit .

endif .

.......

.......

Endloop.

Endloop.

Former Member
0 Kudos
160

Hi.

Refer this link.

Regards.

Jay

former_member194613
Active Contributor
0 Kudos
160

There are nested loops and there are other nested loops ... and lots of confusion.

I don't believe, that it is necessary to have


loop at itab1.
  loop at itab2.
...
  endloop.
endloop.

without where. This might appear for rather small and not growing tables. But then it is no performance problem.

It only become an issue if itab1 and itab2 can become large. Then it must be of that kind


loop at itab1 into wa.
  loop at itab2 where x = wa-x .
...
  endloop.
endloop.

BUT YOU STILL MUST TAKE CARE THAT itab2 IS A SORTED TABLE!

Otherwise it is still slow. There is a solution for standard tables which is cumbersome, see

here,

Measurements on internal tables: Reads and Loops:

Forget parallel index, it is even more cumbersome! Don't forget, only itab2 must be sorted,

with parallel cursor also itab1 must be sorted ... with that you will loose the whole advantage.

So: Nested loops can not always be avoided, if you take care, then they are no problem.

Siegfried

Former Member
0 Kudos
160

Hello Siva,

For Q:1

Loop inside loop will definitely make huge impact based on data and the way loops are used.

Pls take out all the selects from loop.

Have select data in internal table outside first loop with for all entries. Its always advisable to use Joins instead of for all entries.

Sort internal table and read that table using binary search at the 'select' place inside loop or use internal table as sorted table if you are not doing any modification in those internal table.

After read make sure that you have sy-subrc check. In else part clear the values.

Since loops inside Loops are required then inside loops should have where condition.

If you do not need duplicate data from the loop then delete duplicate entries and sort all the internal tables.

e.g. DELETE ADJACENT DUPLICATE ENTRIES FROM <itab> .

http://help.sap.com/saphelp_nw04/helpdata/EN/06/aafd54fc4011d195280000e8353423/content.htm

See if you could make use of joins for 'select' to reduce internal tables and intern loops.

Thank You,

Nishikant.

Former Member
0 Kudos
160

Hello Siva,

For Q:2

for each select database connection should be opened, retrieve data from table, & close required. Hence you should retrieve all the data before starting the loop to avoid n number of attempt of database open and close.

For that select data in internal table outside first loop with for all entries. Its always advisable to use Joins instead of for all entries.

Sort internal table and read that table using binary search at the 'select' place inside loop or use internal table as sorted table if you are not doing any modification in those internal table.

After read make sure that you have sy-subrc check. In else part clear the values.

Do have sy-subrc check after read statement not to carry the previous read value in case of read fail.

In sap scripts you can see more select single inside the loop because we don't expect too many hits I.e. line items are usually less.

Thank You,

Nishikant.

Former Member
0 Kudos
160

Hello Siva,

For Q:3

having subroutines inside the loop is not an issue as some times we have same logic to be performed for each record.

It matters when you have above things like select data in internal table inside subroutine, sort internal tables etc... Definitely this kind of statements will make code to put unwanted load on processing.

Thank You,

Nishikant.

Former Member
0 Kudos
161

Hello Siva,

For Q:4

You need to twist code as per logic. If WHILE statement is having proper conditions and used appropriately then should be OK.

For move-corresponding if select f1, f2 is matching with internal table then should not be used as it is a additional task for ABAP to decide where the field data should go in internal table. It is expensive statement than the normal select f1, f2 into itab.

In some situation you can not avoid corresponding as those internal table are accessed with some sequence of fields.

Using select inside loop in normal conditions is not acceptable. It should be outside the loop.

Thank You,

Nishikant.