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

Replacing Read with Loop will make a difference performance wise

former_member391265
Participant
0 Likes
1,740

Hi all, I have checked all the forums but i just have a doubt which i need to clarify. My basic requirement is to optimize the abap code which is written in BW Field Routine. I know that we cannot use Relational operator with Read Statement So, if we replace Read Statement with Loop Statement. Will it be same Performance wise. Because what i feel is it will loop multiple times which again will slow down the performance if we have millions records.

Read table ITAB into WA With KEY    

                         F1  = SF-F1                  

                AND  F2 =  SF-F2                  

               AND  F3 >  SF-F3.

(ERROR as it does not accept relational Op)              RESULT = XYZ.

****************************************************************************************************************************

LOOP AT ITAB ASSIGNING Field Symbol FS    WHERE  F1  = SF-F1  AND  F2 =  SF-F2  AND  F3 >  SF-F3.

             RESULT = XYZ.

ENDLOOP.

Or Do we have any other alternative to replace read statement in a better way?

Message was edited by: Matthew Billingham - fixed formatting (to an extent)

1 ACCEPTED SOLUTION
Read only

bauert
Explorer
0 Likes
1,227

Due to the 'greater than' you have to use the LOOP instead of the READ Statement.

Make sure the ITAB is defined as SORTED or HASHED TABLE.

Maybe it is an Option to first delete all entries from ITAB where F3 <= SF-F3.

Then you can use the READ Statement instead of the LOOP.

You have to verify with SAT-Transaction which Version is the better one for your case.

Make also sure that the ITAB structure only contains fields you really Need in further processing.

Having an ITAB with a wide structure and millions of entries will also slow down Performance.

6 REPLIES 6
Read only

Former Member
0 Likes
1,227

I don't know about the BW field routine,  but the read you are using there is for a standard table.

If you are certain that your table does not have duplicate keys you can define it as a hashed table and read it using the 'WITH TABLE KEY' clause of the READ statement.

Mind you,  if you have millions of records you will always have some sort of a delay.....

Read only

bauert
Explorer
0 Likes
1,228

Due to the 'greater than' you have to use the LOOP instead of the READ Statement.

Make sure the ITAB is defined as SORTED or HASHED TABLE.

Maybe it is an Option to first delete all entries from ITAB where F3 <= SF-F3.

Then you can use the READ Statement instead of the LOOP.

You have to verify with SAT-Transaction which Version is the better one for your case.

Make also sure that the ITAB structure only contains fields you really Need in further processing.

Having an ITAB with a wide structure and millions of entries will also slow down Performance.

Read only

ArcherZhang
Product and Topic Expert
Product and Topic Expert
0 Likes
1,227

This message was moderated.

Read only

0 Likes
1,227

This message was moderated.

Read only

Tomas_Buryanek
Product and Topic Expert
Product and Topic Expert
0 Likes
1,227

You can simply EXIT loop after you find first line which meet WHERE criteria.


LOOP AT itab WHERE a > b.

     EXIT.

ENDLOOP.

IF sy-subrc = 0.

     "found

ELSE.

     "not found

ENDIF.

READ also searches whole table until it finds resulting line.

If you have performance problem with that. Then use different kind of itab as already suggested, set itab key/s....

-- Tomas --
Read only

matt
Active Contributor
0 Likes
1,227

Since this thread is now attracting incorrect answers, I've decided to lock it. The answer has been given

1. Use a SORTED or HASHED table, with key as defined as F1 F2

2. Put an EXIT before the ENDLOOP

3. Check sy-subrc after the ENDLOOP. If it is non-zero then no record was found