0% found this document useful (0 votes)
112 views

Sap Loop Improvment

The document discusses implementing a "Parallel Cursor" technique to improve performance when nested loops are used. A Parallel Cursor uses a variable to maintain the index position when looping through nested tables, avoiding needing to search from the beginning each time. This allows looping to start from the last index position, making searches faster. The document provides an example of converting a nested loop to use a Parallel Cursor by sorting the tables and storing the index position in a variable to start the inner loop from.

Uploaded by

Mirko
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
112 views

Sap Loop Improvment

The document discusses implementing a "Parallel Cursor" technique to improve performance when nested loops are used. A Parallel Cursor uses a variable to maintain the index position when looping through nested tables, avoiding needing to search from the beginning each time. This allows looping to start from the last index position, making searches faster. The document provides an example of converting a nested loop to use a Parallel Cursor by sorting the tables and storing the index position in a variable to start the inner loop from.

Uploaded by

Mirko
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

How to implement a "Parallel Cursor"?

By levi aliangan On 12/09/2012 Leave a Comment

During our development, there are cases which we need to perform a nested loop. Nested loop
is simply a loop within a loop. This cannot be avoided since there are tables in the data
dictionary wherein we loop a header and carry out some processing for each of the lines in the
corresponding item level. Doing so have a great impact on the performance of the program
specially when deployed on the production system where data is about millions of lines. An
example of these tables are EKKO (Purchasing Document Header) and EKPO (Purchasing
Document Item) tables. Consider this sample code below showing a Nested Loop.

1 DATA :it_ekko TYPE STANDARD TABLE OF ekko,


2 wa_ekko TYPE ekko,
3 it_ekpo TYPE STANDARD TABLE OF ekpo,
4 wa_ekpo TYPE ekpo.
5
6 DATA :lv_tabix TYPE sy-tabix.
7
8 SELECT * FROM ekko INTO TABLE it_ekko.
9 SELECT * FROM ekpo INTO TABLE it_ekpo.
10
11 LOOP AT it_ekko INTO wa_ekko.
12 LOOP AT it_ekpo INTO wa_ekpo WHERE ebeln = wa_ekko-ebeln.
13 "Logical ABAP statements
14 ENDLOOP.
15 ENDLOOP.

As seen from the code above, a LOOP is within a LOOP. This way of code will greatly affect
the performance of your program. In this situation, what you need to implement is a Parallel
Cursor technique.

What is a Parallel Cursor?


Parallel cursor is the technique to increase the performance of the program, when there are
nested loops. The approach uses a variable using which we can maintain a cursor which will
avoid the search to start all over again, from the first record. Instead it will control the loop to
start the search from a specified index. This as a result makes the search faster and thus the
high end performance improvement is scene in data loading processes. A mandatory
prerequisite before using the approach is that the internal tables are both sorted by
the respective key fields.

Nested Loop with Parallel Cursor


1 DATA:
2 it_ekko TYPE STANDARD TABLE OF ekko,
3 wa_ekko TYPE ekko,
4 it_ekpo TYPE STANDARD TABLE OF ekpo,
5 wa_ekpo TYPE ekpo.
6 DATA:
7 lv_tabix TYPE sy-tabix.
8
9 SELECT * FROM ekko INTO TABLE it_ekko.
10 SELECT * FROM ekpo INTO TABLE it_ekpo.
11 SORT it_ekko BY ebeln.
12 SORT it_ekpo BY ebeln ebelp.
13
14 LOOP AT it_ekko INTO wa_ekko.
15 READ TABLE it_ekpo TRANSPORTING NO FIELDS
16 WITH KEY ebeln = wa_ekko-ebeln BINARY SEARCH.
17 IF sy-subrc EQ 0.
18 lv_tabix = sy-tabix.
19 LOOP AT it_ekpo INTO wa_ekpo FROM lv_tabix.
20
21 Logical ABAP statements ...
22
23 AT END ebelp.
24 EXIT.
25 ENDAT.
26 ENDLOOP.
27 ENDIF.
28 ENDLOOP.

Having this code implemented with parallel cursor, the inner loop will execute four times and
statement inside the loop will execute three times. This will result in a major performance
improvement over the nested loop code.

Again, thank you so much for reading this article. If you find it useful and helpful, please
consider sharing it to your friends. Remember always, SHARING is CARING . Until next
tutorial.

You might also like