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: 

help needed on performance improvement avoiding nested loops

Former Member
0 Kudos

Hi Everybody,

Is there any thing similar to "for all entries" for internal tables to fetch multiple records

from an internal table at a time based on certain selection criteria.

We can read only record using "read" statement right. I want to read multiple records

from an internal table at a time without using loop.

I have three internal tables itab1, itab2, itab3. Based on a field value in itab1, I have to fetch

multiple records from itab2 into itab3. itab2 and itab3 are of same line type.

itab1

-


machine | function name

-


123 | FM1

456 | FM1

789 | FM2

itab2

-


machine | m/c description | start time | stop time |.....|.....|...

-


123 | mach1 | 07:00 | 08:00

123 | mach1 | 08:00 | 09:00

123 | mach2 | 06:00 | 08:00

456 | mach2 | 24:00 | 08:00

456 | mach2 | 07:00 | 12:00

789 | mach3 | 04:00 | 09:00

When I loop over itab1, and get first record, I want to get all records from itab2 corresponding to the machine number in the itab1 at a time into itab3 without using loop.

Thanks,

Sunny

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Try this...

Loop at itab2.

itab3-machine = itab2-machine.

itab3-m/c description = itab2-m/c description.

itab3-start time = itab2-start time.

itab3-stop time = itab2-stop time.

read table itab1 with key .....

if sy-subrc = 0.

itab3-function name = itab1-function name.

endif.

append itab3.

endloop.

8 REPLIES 8

Former Member
0 Kudos

Try this...

Loop at itab2.

itab3-machine = itab2-machine.

itab3-m/c description = itab2-m/c description.

itab3-start time = itab2-start time.

itab3-stop time = itab2-stop time.

read table itab1 with key .....

if sy-subrc = 0.

itab3-function name = itab1-function name.

endif.

append itab3.

endloop.

0 Kudos

Hi Ravi,

Actually I need to loop over itab1 and have to create a new itab3 by using itab2. In itab3, it should contain

the records from itab2 for the corresponding machines(records in itab1) which have same function module in itab1.

In that loop, I used at end of 'function name' and in that event i have to call the function module.

For that funmodule, i have to pass itab3 as a parameter. The purpose is ,the function module which

I called will calculate some values for the records( machines) which I passed as parameter to the function

module.

The main purpose is I want to pass only records(machines) from itab2 which are relevant to the fun module as a parameter which i am calling in the "at end of fun name". So I am trying to filter records

from itab2 an store it in itab3 based on the fun name value in itab1 .

itab1

-


machine | function name

-


123 | FM1

456 | FM1

789 | FM2

itab2

-


machine | m/c description | start time | stop time |.....|.....|...

-


123 | mach1 | 07:00 | 08:00

123 | mach1 | 08:00 | 09:00

123 | mach2 | 06:00 | 08:00

456 | mach2 | 24:00 | 08:00

456 | mach2 | 07:00 | 12:00

789 | mach3 | 04:00 | 09:00

In the example, which I gave, when i am looping at itab1 before the control each at end of fun name,

itab3 shd contain

123 | mach1 | 07:00 | 08:00

123 | mach1 | 08:00 | 09:00

123 | mach2 | 06:00 | 08:00

456 | mach2 | 24:00 | 08:00

456 | mach2 | 07:00 | 12:00

since the machines 123,456 have same fun name FM1.

Thanks,

Aditya

0 Kudos

Hi,

no such thing as "for all entries" for internal tables. However if your itab2 is declared as a sorted table with non-unique key machine, looping through it for the matching machine from itab1 should not be a performance pooper.

Cheers

Thomas

Edit:

you could also loop at itab2, and at end of machine you read the corresponding entry of itab1 to find the function to be called. itab1 should also be declared as sorted table with key machine.

former_member194613
Active Contributor
0 Kudos

the FOR ALL ENTRIES is nothing very special, for internal tables it is not necessary, because a loop and read inside the loop can be

very fast, there is no room for improvement.

As the FOR ALL ENTRIES will not work if there is no suitable index, so the read will not work if it can not faciliate a binary search

(either by a sorted table or by sorting the standard table and using binary search).

Nested loops are unavoidable and that is fine. There are is a lot of coding of nested loops with performance bugs, but these bugs are avoidable.

Siegfried

Former Member
0 Kudos

Thank you very much for all of your help. I wanted to find out if I can avoid nested loops. As per your suggestion, I am using loops.

Thanks,

Sunny

0 Kudos

Hi Sunny,

As said by Harish parallel cursor method is best way to increase the performance. if it is compulsory to use nested loops, then we need to go for PARALLEL CURSOR METHOD

this is very efficient method. This decreases the execution time and increases the performance.

here is a sample code for PARALLEL CURSOR METHOD

Nested Loops – This is one of the fear factors for all the ABAP developers as this consumes lot of program execution time. If the number of entries in the internal tables is huge, then the situation would be too worse. The solution for this is to use parallel cursor method whenever there is a need for Nested Loop.

Program using Normal Nested Loop:

REPORT ZNORMAL_NESTEDLOOP.

TABLES:

likp,

lips.

Data:

t_likp type table of likp,

t_lips type TABLE OF lips.

data:

W_RUNTIME1 TYPE I,

W_RUNTIME2 TYPE I.

START-OF-SELECTION.

select *

from likp

into table t_likp.

select *

from lips

into table t_lips.

get RUN TIME FIELD w_runtime1.

loop at t_likp into likp.

loop at t_lips into lips where vbeln eq likp-vbeln.

endloop.

endloop.

get RUN TIME FIELD w_runtime2.

w_runtime2 = w_runtime2 - w_runtime1.

write w_runtime2.

Nested Loop using Parallel Cursor:

REPORT zparallel_cursor2.

TABLES:

likp,

lips.

DATA:

t_likp TYPE TABLE OF likp,

t_lips TYPE TABLE OF lips.

DATA:

w_runtime1 TYPE i,

w_runtime2 TYPE i,

w_index LIKE sy-index.

START-OF-SELECTION.

SELECT *

FROM likp

INTO TABLE t_likp.

SELECT *

FROM lips

INTO TABLE t_lips.

GET RUN TIME FIELD w_runtime1.

SORT t_likp BY vbeln.

SORT t_lips BY vbeln.

LOOP AT t_likp INTO likp.

LOOP AT t_lips INTO lips FROM w_index.

IF likp-vbeln NE lips-vbeln.

w_index = sy-tabix.

EXIT.

ENDIF.

ENDLOOP.

ENDLOOP.

GET RUN TIME FIELD w_runtime2.

w_runtime2 = w_runtime2 - w_runtime1.

WRITE w_runtime2.

Analysis report: Runtime in microseconds:

Iteration No ....._Normal Nest Loop_ ..... Using Parallel Cursor

1 ....................... 34,796,147 ................... 63,829

2 .........................38,534,583 ................... 56,894

3 .........................34,103,426 ................... 50,510

u can check this site for more details

http://www.saptechnical.com/Tutorials/ABAP/ParallelCursor.htm

reward if helpful

raam

harishaginati
Explorer
0 Kudos

Hi Suny,

Best way is use parallel cursors technique. here is a sample code to implement.

please reward me if useful and close ticket.

  • Entries: 100 (ITAB1), 1000 (ITAB2)

  • Line width: 100

  • Both tables sorted by key K

I = 1.

LOOP AT ITAB1 INTO WA1.

LOOP AT ITAB2 INTO WA2 FROM I.

IF WA2-K <> WA1-K.

I = SY-TABIX.

EXIT.

ENDIF.

" ...

ENDLOOP.

ENDLOOP.

if you have any issues to sort both tables with same key you can modify above algorithm slightly to suite your requirement.

LOOP AT ITAB1 INTO WA1.

read table itab2 into wa2 with key field1 = itab1-field1 binary search.

if sy-subrc eq 0.

I = sy-tabix.

LOOP AT ITAB2 INTO WA2 FROM I.

IF WA2-field1 <> WA1-field1.

EXIT.

ENDIF.

" ...

ENDLOOP.

endif.

ENDLOOP.

Former Member
0 Kudos

Hi Everybody,

Thank you very much for your help. I found another way without using nested loop.

My input is

itab1

-


machine | function name

-


123 | FM1

456 | FM1

789 | FM2

itab2

-


machine | m/c description | start time | stop time |.....|.....|...

-


123 | mach1 | 07:00 | 08:00

123 | mach1 | 08:00 | 09:00

123 | mach2 | 06:00 | 08:00

456 | mach2 | 24:00 | 08:00

456 | mach2 | 07:00 | 12:00

789 | mach3 | 04:00 | 09:00

Output

In the example, which I gave, when i am looping at itab1 before the control each "at end of fun_name" event, I need to get all records from itab2 for the machines which have same function name in the itab1. In the above example machines 123 and 456 have same fun-name in itab1, so i got all the records from itab2 for which mach no is 123,456 into itab3.

itab3 shd contain

123 | mach1 | 07:00 | 08:00

123 | mach1 | 08:00 | 09:00

123 | mach2 | 06:00 | 08:00

456 | mach2 | 24:00 | 08:00

456 | mach2 | 07:00 | 12:00

My logic

ranges: r_mchno for ztable-mach_num..

loop at itab1 into wa

r_mchno-sign = 'I'

r_mchno-option = 'EQ'

r_mchno-low = wa-mchno.

append r_mchno.

at end of 'function_name'.

itab3 = itab2.

delete itab3 where machno not in r_mchno. " now i have only records which are having same fun name.

call wa-funciton_name

exporting

tab = itab3

clear r_mchno.

endloop.

Main concept is I am copying the whole itab2 into itab3 and then i am deleting the unwanted records from itab3 based on the r_mchno (ranges). I mean I am deleting all the records from itab3 which are not in the r_mchno. In this way I avoided loop over itab2 inside loop over itab1 (avoiding nested loops).

Sunny

Edited by: Sunny on Apr 9, 2008 12:15 PM