Hi all,
There are occasions where we have a code that looks like this:

FORM loop_with_select .

  DATA: t01 TYPE i ,
        t02 TYPE i ,
        dif TYPE i .

  DATA: it_sflight TYPE TABLE OF sflight .
  FIELD-SYMBOLS: <st_sflight> LIKE LINE OF it_sflight .

  DATA: st_scarr TYPE scarr .

  SELECT * FROM sflight INTO TABLE it_sflight .

  SORT it_sflight BY fldate .

  GET RUN TIME FIELD t01.

  LOOP AT it_sflight ASSIGNING <st_sflight> .

* For each row we go and select a record from scarr
    SELECT SINGLE * INTO st_scarr
      FROM scarr
      WHERE carrid EQ <st_sflight>-carrid .

  ENDLOOP .

  GET RUN TIME FIELD t02 . dif = t02 t01.

  WRITE: / ‘loop_with_select:’ , dif .

ENDFORM .                    “loop_with_select

We have a loop and within the loop we execute some select single to get some extra data (e.g. text).

This raise a performance problem .

One solution is to implement a cache service in the program. The STATICS declaration allow us to create a form for that purpose .

FORM get_cache_data
  USING
    carrid   TYPE scarrcarrid
  CHANGING
    st_scarr TYPE scarr .

  CLEAR st_scarr .

* Use static variable as cache
  STATICS: it_scarr TYPE HASHED TABLE OF scarr
  WITH UNIQUE KEY
    carrid .

* Try the cache .
  READ TABLE it_scarr INTO st_scarr
  WITH TABLE KEY
    carrid = carrid .

  CHECK sysubrc NE 0 .

  st_scarrcarrid = carrid .

  SELECT SINGLE * INTO st_scarr
  FROM scarr
  WHERE
    carrid EQ carrid .

* We insert the result of the select always so we have
* empty description in those cases where there is no data in scarr for given “carrid”
  INSERT st_scarr INTO TABLE it_scarr .

ENDFORM .                    “get_cache_data

Lets use FORM get_cache_data:

FORM loop_with_cache .

  DATA: t01 TYPE i ,
        t02 TYPE i ,
        dif TYPE i .

  DATA: it_sflight TYPE TABLE OF sflight .
  FIELD-SYMBOLS: <st_sflight> LIKE LINE OF it_sflight .

  DATA: st_scarr TYPE scarr .

  SELECT * FROM sflight INTO TABLE it_sflight .

  SORT it_sflight BY fldate .

  GET RUN TIME FIELD t01.

  LOOP AT it_sflight ASSIGNING <st_sflight> .

* For each row we go and ask for a record from the cache
    PERFORM get_cache_data
     USING
       <st_sflight>carrid
    CHANGING
       st_scarr .

  ENDLOOP .

  GET RUN TIME FIELD t02 . dif = t02 t01.

  WRITE: / ‘loop_with_cache:’ , dif .

ENDFORM .                    “loop_with_cache

Pros:

  • No change in program logic.
  • Performance boost (IT_SFLIGHT contain about 400 entries )

Clipboard01.jpg

Cons:

  • Higher memory requirement .
  • Extra code.

Notes:

SAP use functions for this purpose (Use SE37 and search for *SINGLE*READ* )  but some times it is better to create your own.

Happy coding.

To report this post you need to login first.

1 Comment

You must be Logged on to comment or reply to a post.

Leave a Reply