Preface – This post is part of the ABAP Beginner series.
Parallel Cursor in ABAP is a way to modify the conventional nested loop in such a way that the overall performance of the ABAP program gets improved.
Table of Contents
Suppose we have two tables, one table has Employee basic details and another table has Employee monthly salary details. And the requirement is to print Employee Basic details with all his salaries. What a programmer will do, he will write two Loops. The first loop will get the employee basic details one by one and the other loop will get employee salary details. You can check this thing in below ABAP code:
LOOP AT zemployee_basic INTO wa_employee_basic. LOOP AT zemployee_salary INTO wa_employee_salary where employee_id = wa_employee_basic-employee_id. Write: wa_employee_basic-employee_id, wa_employee_salary-month, wa_employee_salary-amount. ENDLOOP. ENDLOOP.
The above code will work fine and will print Employee ID, Month of the Salary and Amount of the salary of each and every employee.
What if you want to find Salary Amount of one person for a particular month? You will again write the same loops to find the required details.
Also, for every new Employee ID, the second Loop still goes through the whole data.
What we conclude with this process is that, this process is neither good for printing data nor good for search operation as it wastes ample amount of time.
Parallel Cursor Concept
To curb this problem, a concept called Parallel Cursor was introduced by SAP ABAP.
According to Parallel cursor concept, we need to follow following steps:
Step 01: Sort both the tables
Step 02: Write the first loop, like the previous way itself.
Step 03: Read the second table with the key that was required to search the data. This would give the first value regarding the key and also its position in the table [SY-TABIX].
Step 04: If the above step executed successfully, it means data is there in the second table associated with the key. Hence the SY-SUBRC will be set to zero. In this step we will check the value of SY-SUBRC. If it is zero, than we will proceed.
Step 05: Assign the SY-TABIX value into a local variable. We do this step to preserve the SY-TABIX value as it changes with the loop.’
Step 06: Now it’s the time to write the second loop. This time we don’t write the WHERE condition. This time we will write FROM condition with the local variable we initialized above.
Step 07: In this step we check if the key of first table is equal to the key of second table or not. If not, then exit the loop. This stops the loop from reading non required data.
Step 08: In this step we write our actual logic or print statements. And then close both the loops.
Parallel Cursor Example
Let us implement parallel cursor algorithm in the last example we discussed.
SORT: zemployee_basic, zemployee_salary. [Step 01]
LOOP AT zemployee_basic INTO wa_employee_basic. [Step 02]
READ TABLE zemployee_salary INTO wa_temp WITH KEY employee_id = wa_employee_basic-employee_id BINARY SEARCH. [Step 03]
IF SY-SUBRC = 0. [Step 04]
lv_index = SY-TABIX. (you need to declare this variable earlier only) [Step 05]
LOOP AT zemployee_salary INTO wa_employee_salary FROM lv_index . [Step 06]
IF wa_employee_basic-employee_id <> wa_employee_salary-employee_id. [Step 07]
Write: wa_employee_basic-employee_id, wa_employee_salary-month, wa_employee_salary-amount. [Step 08]
From above steps and example, it is clear that Parallel cursor is better than conventional nested loops. Actually nested loops take O(n1*n2) time to execute both the loops while parallel cursor takes O(n1+n2) time to execute the same.
Parallel cursor more details
Great Explain but I have a question about Step 3.
Why did we use wa_temp ?
When I replace wa_temp with zemployee_salary , result still same .
when you replace zemployee_salary, then purpose of improving poerformamce is defeated as we are finding out from whcih entry in salary table we should do the loop
Berk what is there is no employee id in the inner loop for the outer one still it has to go through the whole loop that’s why we are putting read for just a single time check. And we are taking sy-tabix from that as read table will read the 1st matched record. Suppose there are total 20 records and the employee ID is there in 18th and 20th record then it will start the inner loop from 18 and will go until 20th inner record.
what if we do like this?
SORT: zemployee_basic, zemployee_salary.
LOOP AT zemployee_basic INTO wa_employee_basic .
LOOP AT zemployee_salary INTO wa_employee_salary WHERE employee_id = wa_employee_basic- employee_id.
IF wa_employee_basic-employee_id wa_employee_salary-employee_id. [Step 07]
why you don’t add the time of read statement (log n) to the running time of the algorithm