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: 

Sorting internal table -- will it improve performance during loop

Former Member
0 Kudos

Consider that there are 2 internal tables itab1 and itab2

  1. of records in itabl1 = 100000

  2. of records in itabl2 = 200000

loop at itab1.

loop at itab2 where field f1 = itab1-f1.

--- Do some processing -


endloop.

endloop.

By any chance will the performance will be improved if i sort the tables itab1 and itab2 by field f1

9 REPLIES 9

Former Member
0 Kudos

Hi Karthikeyan,

In any case the iternal tables will be looped depending on the number of records be it sorted or not.So in this case it will hamper the performance if you sort the internal table itself cos you will be sorting an internal table that has so many records.

regards,

Sudhi

andreas_mann3
Active Contributor
0 Kudos

Hi,

you should use a sorted table for itab2

DATA: itab2 TYPE sorted TABLE OF ztab with non-unique

key f1.

data wa2 type ztab.

...

loop at itab1.

loop at itab2 into wa2 where field f1 = itab1-f1.

--- Do some processing -


endloop.

endloop.

...

-> performance - win will be enormous !

Grx Andreas

0 Kudos

Hi Andreas,

Thanks for your explanation . I found the following piece of documentation for sorted tables.

-


Sorted tables

This is the most appropriate type if you need a table which is sorted as you fill it. You fill sorted tables using the INSERT statement. Entries are inserted according to the sort sequence defined through the table key. Any illegal entries are recognized as soon as you try to add them to the table. The response time for key access is logarithmically proportional to the number of table entries, since the system always uses a binary search. Sorted tables are particularly useful for partially sequential processing in a LOOP if you specify the beginning of the table key in the WHERE condition.

-


If i understood this correctly ,if i use sorted table, when there is a loop with where clause it uses the binary search internally to identify the records that needs to be processed and once it identifies the last record, it just skips the rest of the records and hence improving the performance.

is this the reason why sorted tables improve the performcance in this case??

0 Kudos

Hi Karthik,

Your understanding is true and that is reason why the performance improves.

Because when you use LOOP .. WHERE. It necessarily goes through all the rows, with the exception that only if the condition is met, it will execute the code between LOOP and ENDLOOP.

Incase you dont want to use a sorted table.

You might follow this approach.

Table1

Table2

Sort table2 by key.

loop at table1.k

read table table2 with key binary search.

if sy-subrc ne 0.

continue.

endif.

loop at table2 from sy-tabix.

if table2-key ne table1-key.

exit.

endif.

do your required processing here.

endloop. "table2

endloop. "table1

*****

Another approach incase you can sort both the tables is as follows:

data i1 type sy-index value 1.

sort t1 by key.

sort t2 by key.

loop at t1.

if t1-key < t2-key.

continue.

endif.

loop at t2 from idx.

if t2-key = t1-key.

      • do the processing.

elseif t2-key > t1-key.

idx = sy-tabix.

exit.

endif.

endloop. "t2

if sy-subrc ne 0.

exit.

endif.

endloop. "t1

Regards,

Pavan

Message was edited by: Pavan Lanka

Former Member
0 Kudos

Performance of your program will extremely increase if you use parallel cursor method as described in: SE80, Environment->Performance examples. Next: Internal Tables->Joining internal tables or Internal Tables->Nested loops.

Former Member
0 Kudos

Hi,

I havent tried using it but a few suggessions. Here are a few things from SAP Help :

-


If you read entries from standard tables using a key other than the default key, you can use a binary search instead of the normal linear search. To do this, include the addition BINARY SEARCH in the corresponding READ statements.

READ TABLE <itab> WITH KEY <k1> = <f1>... <kn> = <fn> <result>

BINARY SEARCH.

The standard table must be sorted in ascending order by the specified search key. The BINARY SEARCH addition means that you can access an entry in a standard table by its key as quickly as you would be able to in a sorted table.

-


You can assign the table entry read from the table to a field symbol by specifying <result> as follows:

READ TABLE <itab> <key> ASSIGNING <FS>.

After the READ statement, the field symbol points to the table line. If the line type is structured, you should specify the same type for the field symbol when you declare it. This allows you to address the components of the field symbol. If you cannot specify the type statically, you must use further field symbols and the technique of assigning components of structures to address the components of the structure.

-


I hope the above two things would improve performance.

best regards.

0 Kudos

Karthikeyan,

You can try the following.

Sort itab2.

Loop at itab1.

read table itab2 with key fld1 = itab1-fld1 binary search.

if sy-subrc eq 0.

do processing.

endif.

EndLoop.

Regards,

Srini S

nablan_umar
Active Contributor
0 Kudos

So far reading an internal table with index is always the fastest. When I have 2 large internal tables where both have a common key, I normally do this way:

data: lv_idx type i value 1.

sort tab1 by key1.

sort tab2 by key1.

read table tab2 index lv_idx.

if sy-subrc ne 0.

tab2-key1 = 'zzzzzzzzzz'. * assume key1 is char 10

endif.

loop at tab1.

while tab1-key1 > tab2-key2.

lv_idx = lv_idx + 1.

read table tab2 index lv_idx.

if sy-subrc ne 0.

tab2-key1 = 'zzzzzzzzzz'.

endif.

endwhile.

if tab1-key1 = tab2-key2.

  • they are matching

endif.

endloop.

Former Member
0 Kudos

try this:

field-symbols: <fs_itab1> type ty_itab1,

<fs_itab2> type ty_itab2.

sort itab2 ascending by f1.

loop at itab1 assigning <fs_itab1>.

read table itab2 transporting no fields

with key f1 = <fs_itab1>-f1

binary search.

if sy-subrc = 0.

loop at itab2 assigning <fs_itab2> from sy-tabix.

--- Do some processing -


endloop.

endif.

endloop.