TIPS & TRICKS
WHERE ID = AP1.SELECT SINGLE NAMEINTO NAME2FROM SAIRPORTWHERE ID = AP2.ENDSELECT.ENDSELECT.
Context advantages:- no double fetch by DEMAND: 1. fetch, 2. get from buffer - more performance (best SELECT-statement)- better survey of code
Linear search in an internal table
* Entries: 1000, Line width: 100* Key width: 20* The READ ends with SY-SUBRC=4READ TABLE ITAB INTO WAWITH KEY K = 'X'.
If internal tables are assumed to have many (>20) entries, alinear search through all entries is very time-consuming.Try to keep the table ordered and use binary searchor used a table of type SORTED TABLE.If TAB has n entries, linear search runs in O( n ) time, whereasbinary search takes only O( log2( n ) ).
Binary search in an internal table
* Entries: 1000, Line width: 100* Key width: 20* The READ ends with SY-SUBRC=4READ TABLE ITAB INTO WAWITH KEY K = 'X'BINARY SEARCH.
Dynamically specified key
READ TABLE ITAB INTO WAWITH KEY (NAME) = 'X'.
A dynamic key access is slower than a static one, since the keyspecification must be evaluated at runtime. However, for largetables the costs are dominated by number of comparisonneeded to locate the entry.
Statically specified key
READ TABLE ITAB INTO WAWITH KEY K = 'X'.
No secondary index => linear search
READ TABLE ITAB INTO WAWITH KEY DATE = SY-DATUM.IF SY-SUBRC = 0." ...ENDIF.
If you need to access an internal table with different keysrepeatedly, keep your own secondary indices.With a secondary index, you can replace a linear search with abinary search plus an index access.
Binary search using secondary index
READ TABLE SEC_IDX INTO SEC_IDX_WAWITH KEY DATE = SY-DATUMBINARY SEARCH.IF SY-SUBRC = 0.READ TABLE ITAB INTO WAINDEX SEC_IDX_WA-INDX." ...ENDIF.
Key access with LOOP/CHECK
LOOP AT ITAB INTO WA.CHECK WA-K = 'X'." ...ENDLOOP.
LOOP ... WHERE is faster than LOOP/CHECK because LOOP... WHERE evaluates the specified condition internally.As with any logical expressions, the performance is better if theoperands of a comparison share a common type.The performance can be further enhanced if LOOP ... WHEREis combined with FROM i1 and/or TO i2, if possible.
Key access with LOOP ... WHERE
LOOP AT ITAB INTO WA WHERE K = 'X'." ...ENDLOOP.
READ on a unique sorted table
DO 250 TIMES.N = 4 * SY-INDEX.READ TABLE STAB INTO WA WITH TABLE KEY K = N.IF SY-SUBRC = 0." ...ENDIF.ENDDO.
Entries in a sorted table are located by binary search.The costs depend on the number of entries in the table (O (log
READ on a unique hashed table
DO 250 TIMES.N = 4 * SY-INDEX.READ TABLE HTAB INTO WA WITH TABLE KEY K = N.IF SY-SUBRC = 0." ...ENDIF.ENDDO.
Hashed tables are optimized for single entry access, whereassorted tables are optimized for partial sequential loop
Ravi KPage 34/8/2009