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

Read Table Vs Loop at

Former Member
0 Likes
15,913

Dear All,

Please let me know which one of the two should I use to improve the performance, for tables containing a lot of data ?

Regards,

Thanks in anticipation.

Alok.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
5,620

Hello,

Case 1:

=======

If table has lot of data first of all loop at using field symbols,

LOOP AT ITAB ASSIGNING <fs>.

ENDLOOP.

This would definitely improve the performance.

Case 1.1:

=========

If you have single loop and dependsing up on the previous value you want to select data. Then first sort that internal table based on primary key and read the table using READ statement with BINARY SEARCH.

SORT ITAB by f1.

READ TABLE ITAB INTO <WA> WITH KEY f1 = <Variable>

BINARY SEARCH.

If sy-subrc EQ 0.

ENDIF.

Case 3:

=======

Always ignore Loop inside loop. Instead go for READ if you have to play around 2 loops.

LOOP AT ITAB1 INTO <WA>.

READ TABLE ITAB2 INTO <WA2> WITH KEY

f1 = <WA>-f1.

if sy-subrc EQ 0.

ENDIF.

ENDLOOP.

Regs,

Venkat Ramanan N

8 REPLIES 8
Read only

Former Member
0 Likes
5,620

Hi,

Follow below steps.

In se30 transaction you can look for

Tip&TRicks button on application toolbar

apart from below conventions

Follow below steps

1) Remove corresponding from select satement

2) Remove * from select

3) Select field in sequence as defined in database

4) Avoid unnecessary selects

i.e check for internal table not initial

5) Use all entries and sort table by key fields

6) Remove selects ferom loop and use binary search

7) Try to use secondary index when you don't have

full key.

😎 Modify internal table use transporting option

9) Avoid nested loop . Use read table and loop at itab

from sy-tabix statement.

10) free intrenal table memory wnen table is not

required for further processing.

11)

Follow below logic.

FORM SUB_SELECTION_AUFKTAB.

if not it_plant[] is initial.

it_plant1[] = it_plant[].

sort it_plant1 by werks.

delete adjacent duplicates from it_plant1 comparing werks

SELECT AUFNR KTEXT USER4 OBJNR INTO CORRESPONDING FIELDS OF TABLE I_AUFKTAB

FROM AUFK

FOR ALL ENTRIES IN it_plant1

WHERE AUFNR IN S_AUFNR AND

KTEXT IN S_KTEXT AND

  • WERKS IN S_WERKS AND

AUART IN S_AUART AND

USER4 IN S_USER4 AND

werks eq it_plant1-werks.

free it_plant1.

Endif.

ENDFORM. "SUB_SELECTION_AUFKTAB

Regards

Amole

Read only

Former Member
0 Likes
5,620

Hi

It depends on your requirement. What you wish to do...

Lets assume you want to take a record from internal table and dosome processing on that..then, Read table stmnt with binary search is the best. (Sorting of the internal table is pre-requisite)

If your task is to loop on all records, and do some calculations/summation etc..then looping on internal table is the only option.

Regards,

Raj

Read only

Former Member
0 Likes
5,620

Hi Alok,

Both are different statements

READ statement is used inside a loop, READ statement will read only one record.

to avoid loop inside a loop we use one loop and read the other internal table

Loop at ITAB.
  READ table ITAB2 with key field1 = itab-field1.
endloop.

Read only

Former Member
0 Likes
5,621

Hello,

Case 1:

=======

If table has lot of data first of all loop at using field symbols,

LOOP AT ITAB ASSIGNING <fs>.

ENDLOOP.

This would definitely improve the performance.

Case 1.1:

=========

If you have single loop and dependsing up on the previous value you want to select data. Then first sort that internal table based on primary key and read the table using READ statement with BINARY SEARCH.

SORT ITAB by f1.

READ TABLE ITAB INTO <WA> WITH KEY f1 = <Variable>

BINARY SEARCH.

If sy-subrc EQ 0.

ENDIF.

Case 3:

=======

Always ignore Loop inside loop. Instead go for READ if you have to play around 2 loops.

LOOP AT ITAB1 INTO <WA>.

READ TABLE ITAB2 INTO <WA2> WITH KEY

f1 = <WA>-f1.

if sy-subrc EQ 0.

ENDIF.

ENDLOOP.

Regs,

Venkat Ramanan N

Read only

Former Member
0 Likes
5,620

if your intension to to read JUST only one record from the internal table,

you can use

READ TABLE ITAB WITH KEY F1 = <VALUE> ...

BINARY SEARDCH.

this statement can ONLY return 1 record. so if you know,only 1 record will be there for the given condition,you can use this statement.

else.

you must use LOOP AT and ENDLOOP statements to process more than 1 record.

LOOP AT ITAB WHERE F1 = <VALUE>.

ENDLOOP.

regards

Srikanth

Read only

Former Member
0 Likes
5,620

Hi alok,

1. Good question.

2. I don't think there is any documentation avaialble

for comparing LOOP v/s READ

3. But, practically, they make take almost,

the same time.

4. just copy paste, for getting a taste of it.

5.

REPORT ABC.

DATA : T1 TYPE SY-UZEIT.

DATA : T2 TYPE SY-UZEIT.

data : BEGIN OF ITAB OCCURS 0,

F(50) TYPE c,

END OF ITAB.

*-------

DO 900000 TIMES.

WRITE SY-INDEX TO ITAB-F.

CONDENSE ITAB-F.

CONCATENATE 'HELLO' ITAB-F INTO ITAB-F.

APPEND ITAB.

ENDDO.

*----- LOOP

GET TIME.

T1 = SY-UZEIT.

loop at itab where f = 'HELLOXYZ'.

endloop.

GET TIME.

T2 = SY-UZEIT.

WRITE 😕 'FOR LOOP' , T1 , T2.

*----- READ

GET TIME.

T1 = SY-UZEIT.

READ TABLE ITAB WITH KEY F = 'HELLOXYZ1'.

GET TIME.

T2 = SY-UZEIT.

WRITE 😕 'FOR READ' , T1 , T2.

regards,

amit m.

Read only

Former Member
0 Likes
5,620

It complete depends on what you wish to do with the table data...lets say you have the main data in internal table itab and want to do some calculation for each record..you need to use LOOP AT.

or if you are processing some other internal table and just need to fetch the corresponding record from itab..use should use READ TABLE ...it also advisable to sort the table internal table when you use READ on it and use BINARY SEARCH.

Read only

Former Member
0 Likes
5,620

Hi Alok

It depends on they table key and the uniqueness of each row. If you can define a unique key for your itab then Read is probably better because you can then specify the unique key in the REad statement. If you are going to be repeatedly reading the table as opposed to just once then you might want to experiment with using a Sorted table type with unique key or a Hashed table. These table types have an overhead that will affect performance when they are modified or created but make reads much quicker (hashed table is constant irrespective of the number of records). If you are not repeatedly reading the table then you might be better off using a standard table. Again, if you can identify a unique key then you can sort the table before reading it and then use the BINARY SEARCH option of the Read statement, this will be much quicker than a full table scan.

With regard to using the LOOP AT statement, I believe it will only compete with the read statement if you are able to use a unique key with the WHERE otpion of the LOOP AT statement. If the table has been sorted in advance then it should automatically use a binary search. If you are not able to specify a unique key then the read statement will just return the first record it finds whereas the loop statement will obviously return the lot throughout the course of the loop.

Unfortuantely, there is no short answer, you will have to try some run-time analysis to see which suits your needs.

Hope this helps

Andy