Optimize LOOP AT using Field Symbols

The time taken for an ABAP report to run is mostly spent in database queries and internal table processing. While the responsibility of database query optimization is spread across so many different levels, optimizing internal table processing remains solely the developer’s responsibility. In this article, we will see the advantage of making using of Field Symbols instead of Work areas in LOOP AT.

Consider the situation where we loop through an internal table and modify each line item. The obvious solution would look like the following code.

loop at it_tab into wa_stru.
	wa_stru-fieldname = 'value'.
	modify it_tab from wa_stru.

But if we see close, we are actually moving every line into the work area, then we modify the work area and update the internal table. This should be simplified. It would be better if we are able to make the modification directly in the corresponding line of the internal table.

loop at it_tab assigning <fs>
	<fs>-fieldname = 'value'.

The above LOOP AT ... ASSIGNING construct can help us make a Field Symbol point to the line in the internal table. Field Symbols are pointers to memory areas, so any change in <fs> will change the corresponding line directly in it_tab and hence MODIFY statement is not needed.

I did some analysis with different size of internal tables, varying from 1,000 lines to 1,000,000 lines to come to a conclusion on how much improvement this method can bring in performance.

And to my surprise, Field Symbols method is 9 times faster than using Work Area.

This is an incredible performance gain. Imagine a report which takes 9 minutes to run, which can be reduced to as low as 1 minute. Though at development time we are least bothered about the performance, at production the amount of data keeps on accumulating and our code starts getting aged. With little tweaks to our programming methods, our code will live a longer life at production.

And the program with which I did the analysis is as follows. This program only gives the time taken for both methods in different size of internal tables.

report  ztest_loopat.

      itab type standard table of vbak,
      stru type vbak,
      t1 type i,
      t2 type i,
      dt type i,
      begin of wa_stat,
        records type i,
        select type i,
        modify type i,
        fs type i,
      end of wa_stat,
      t_stat like standard table of wa_stat.

               <fs> type vbak.

define record_time.
  t1 = t2.
  get run time field t2.
  dt = t2 - t1.


  do 5 times.
    wa_stat-records = sy-index * 1000.


    perform fetch_data
      using wa_stat-records.

    wa_stat-select = dt.

    perform loop_at_modify.

    wa_stat-modify = dt.

    perform fetch_data
      using wa_stat-records.


    perform loop_at_assigning.

    wa_stat-fs = dt.

    append wa_stat to t_stat.


  write:/ 'Records',
          'Select time',
          'Modify time',
          'FS time'.

  loop at t_stat into wa_stat.
    write:/ wa_stat-records,

form fetch_data using row_count.

  select * from vbak
    into table itab
    up to row_count rows.

endform.                    "fetch_data

form loop_at_modify.

  loop at itab into stru.
    stru-ernam = 'Test'.
    modify itab from stru.

endform.                    "loop_at_modify

form loop_at_assigning.

  loop at itab assigning <fs>.
    <fs>-ernam = 'Tester'.

endform.                    "loop_at_assigning
comments powered by Disqus