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 scarr-carrid
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 sy-subrc NE 0 .
st_scarr-carrid = 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:
Cons:
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
4 | |
3 | |
2 | |
2 | |
2 | |
1 | |
1 | |
1 | |
1 | |
1 |