Loop on Internal tables PDF Print E-mail
User Rating: / 2
PoorBest 
ABAP Tutorials - Performance tips and tricks
Written by Administrator   
Monday, 14 July 2008 15:27

Often loop on internal tables is expensive operation, specially when you have lots of entries in the internal table and if looping on internal tables is frequently required.

The loops on internal can be optimized to a great extent by declaring internal tables appropriately. Using SORTED tables in such cases is of great help. The following example explains the usage of SORTED tables.

Consider a internal table with 20 entries (of natural numbers) with following distribution. 4 entries of 1, 8 entries of 2, 5 entries of 3 and 3 entries of 4. Lets say you have to loop on this internal table and do some processing. Consider the following cases:

1.How many times the <Statement 1> is processed ? 20 times is the correct answer.

LOOP AT itab.

<Statement 1>

ENDLOOP.

2. How many times the <Statement 1> is processed in this case?

LOOP AT itab WHERE Key = 3

<Statement 1>

ENDLOOP.

The correct answer is again 20 times. Surprising.. is it ? Why 20 times even with a WHERE condition ? This is because the system does not know where is the entry '3' placed in the internal table. It will have to do a full table scan to check where the entry '3' is placed. Now, even if you sort the table using statement SORT itab BY key then also you can not avoid full table scan because the system does not know that the table is sorted.

3. Check the below logic to loop with a where condition. This method is called method of 'Parallel Cursors'. In this method we maitain a parallel cursor to track where the entry '3' starts and ends. Here the table is sorted using SORT itab BY key statement.

READ TABLE itab WITH KEY key = 3 BINARY SEARCH TRANSPORTING NO FIELDS.

LOOP AT itab FROM INDEX sy-tabix INTO <wa>.

lv_last = <wa>.

IF lv_last NE <wa>

EXIT.

ENDIF.

<Statement 1>. 

ENDLOOP.

Here you will see that the loop startes at index 14 and will continue till 18. Thus in this loop <Statement 1> is executed only 5 times (index 14-18). Since the table is sorted, we know that once a different entry (other that the key 3)is encountered, there can not be any more entries with the key '3'. So there is no point in processing the loop further. This method is very efficient especially when we have lot of entries in the table.

But, isn't it some extra lines of code every time we want to loop ? There is a solution. Declare the internal table ITAB as type SORTED table. Thus the ABAP kernel knows that the table has entries in sorted order. The ABAP kernel itself (internally) will take care that the loop starts where the key starts and ends where the key ends. So a simple statement LOOP AT itab WHERE key = '3' takes care of the parallel cursor logic.

Isn't that cool !

Comments
Add New Search
Anonymous  - Nice example..   |address125.17.147.68 |2008-07-19 08:04:17
I must say.. its a good example..
now I understand better how the sorted loops
can improve performance.
Anonymous   |address196.40.47.4 |2009-12-09 11:52:13
Write comment
Name:
Email:
 
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
 
Please input the anti-spam code that you can read in the image.

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Last Updated ( Monday, 14 July 2008 16:00 )
 

Advertisement

 

Google Search

Advertisement