Application Development and Automation 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: 
Read only

Avoiding Loop statement

Former Member
0 Likes
2,323

Hi All.

Considering performance, i need to avoid LOOP statements.

My requirement is Let's say i have a internal table containing N number of records which has 3 keys.

After looping that table, for the first row i need to find a last of all rows other than first row which has 2 keys with same value and other key is different.

LOOP AT lt_table INTO lwa_table1.

LOOP AT lt_table INTO lwa_table2 WHERE  key1 = lwa_table1-key1 AND

                                                                key2 = lwa_table1-key2 AND

                                                        NOT  key3 = lwa_table1-key3.

ENDLOOP.

Check sy-subrc eq 0.

statements........

ENDLOOP.

I am using second loop here to read a record that matches the above conditions. The found record should be the last of all matched records.

Can any one please help me replacing second LOOP with other useful statement ?

Thanks in advance.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,792

Your requirement is quite common and what you have done, is what I do as well.

But since you are facing performance issue, you might want to consider moving the data to a dummy table, sorting ascending/descending the field according to your requirements, and using

READ TABLE .... WITH BINARY SEARCH.

you can find the data faster.

If you dont want to/can not do so, try googling "Parallel Cursor Method". You will get some good examples on how it is done.

9 REPLIES 9
Read only

Former Member
0 Likes
1,793

Your requirement is quite common and what you have done, is what I do as well.

But since you are facing performance issue, you might want to consider moving the data to a dummy table, sorting ascending/descending the field according to your requirements, and using

READ TABLE .... WITH BINARY SEARCH.

you can find the data faster.

If you dont want to/can not do so, try googling "Parallel Cursor Method". You will get some good examples on how it is done.

Read only

Former Member
0 Likes
1,792

Hi Kishore,

You can use parallel cursor method to improve the performance.

You just go through this link.

http://wiki.sdn.sap.com/wiki/display/Snippets/ABAP+Code+for+Parallel+Cursor+-+Loop+Processing

Read only

former_member196490
Active Participant
0 Likes
1,792

Hi,

Using the parllel cursor method will definately solve the problem and give better results.

Thanks

Neha

Read only

JJosh
Active Participant
0 Likes
1,792

SORT IT_TABLE2 BY KEY1 KEY2 KEY3.

LOOP AT it_table ASSIGNING <fs_wa1>.

     IF <fs_wa1> IS ASSIGNED.

*** While reading also you can use assigning Field Symbol (FS type any or as per ur requirement)


         READ TABLE IT_TABLE2 INTO WA_TAB2

         WHERE kEY1 = xx

               KEY2 = xx

               KEY3 = xx

               BINARY SEARCH.        

     ENDIF.

ENDLOOP.

Hope this helps.

Read only

Former Member
0 Likes
1,792

Well, or maybe just use a sorted or hashed table ...

Read only

Former Member
0 Likes
1,792

please provide the complete solution using oarallel cursor, if you recommend it. It is possible but quite hard and no the recommended solution.

And please do not confuse LOOPs with READs, they are dfferent, a LOOP can process several records a READ only one!  A LOOP cannot be replaced by a READ!

The LOOP must be optimized by a READ BINARY SEARCH followed by a LOOP. Please search blogs under my name, the solution is in one on internal tables.

Actually only a part of the solution, the NOT condition can not be optimized, only the other two can be used for the READ, the third one must be added to join ...  something like

{code}

LOOP ... FROM index WHERE key3 NOT ...

if ( key1 <>     OR key2 <> ... ).

  EXIT

{code}

Siegfried

Read only

Former Member
0 Likes
1,792

Hi kishore

I think in your case use  parallel loop concept ,it will fix your problem.

let me know if you need pice of code i will update you for the same.

thanks,

Eswar.

Read only

Former Member
0 Likes
1,792

step1.

        sort the ITAB 1 by key1 key2 key3

        sort the ITAB2 by key1 key2 key3.

step2.
loop at ITAB1 into wa_tab1.

READ TABLE ITAB2 INTO WA_TAB2 WITH KEY1 = WA_TAB1-KEY1 AND

                                                                 KEY2 = WA_TAB1-KEY2 AND

                                                                 KEY3 NE WA_TAB1-KEY3 BINARYSEARCH.

If sy-subrc eq 0.

lv_index = sy-tabix.

loop at itab2 into wa_tab2 from lv_index.

if wa_tab2-key1 = wa_tab1-key1 and

wa_tab2-key2 eq wa_tab1-key2 and

wa_tab2-key3 NE wa_tab1-key3.

endif.

endif.

write ur actual logic ...........

endloop.

endloop.

try this one ..let me know..

Thanks,

eswar

Read only

0 Likes
1,792

please the parallel index solution does not use a READ BINARY SEARCH, it move two cursors parallel with some sophisticated logic. This is possible and fast but complicated.

The above code is the common solution the manual optimization for the standard table. However, it is wrong! The BINARY SEARCH can not exploit the NE-condition. The LOOP has no exit-condition, therefore it will always loop to the end of the table, the improvement is marginal from N*N to N*N/2.

The correct solution is N*logN!!