Professional Documents
Culture Documents
NO YES
A dynamic key access is slower than a static one, since the key
specification must be evaluated at runtime. However, for large
tables the costs are dominated by number of comparison
needed to locate the entry.
No secondary index => linear search Binary search using secondary index
READ TABLE ITAB INTO WA READ TABLE SEC_IDX INTO SEC_IDX_WA
WITH KEY DATE = SY-DATUM. WITH KEY DATE = SY-DATUM
IF SY-SUBRC = 0. BINARY SEARCH.
" ... IF SY-SUBRC = 0.
ENDIF. READ TABLE ITAB INTO WA
INDEX SEC_IDX_WA-INDX.
If you need to access an internal table with different keys " ...
repeatedly, keep your own secondary indices. ENDIF.
With a secondary index, you can replace a linear search with a
binary search plus an index access.
Key access with LOOP/CHECK Key access with LOOP ... WHERE
LOOP AT ITAB INTO WA. LOOP AT ITAB INTO WA WHERE K = 'X'.
CHECK WA-K = 'X'. " ...
" ... ENDLOOP.
ENDLOOP.
Entries in a sorted table are located by binary search. Hashed tables are optimized for single entry access, whereas
The costs depend on the number of entries in the table (O (log sorted tables are optimized for partial sequential loop
Partial sequential access on a sorted table Partial sequential access on a sorted table
LOOP AT HTAB INTO WA WHERE K = SUBKEY. LOOP AT STAB INTO WA WHERE K = SUBKEY.
" ... " ...
ENDLOOP. ENDLOOP.
Hashed tables are optimized for single entry access. The Sorted tables are ordered ascendingly by their key
entries have no specific order. Therefore, a partial sequential components. If a partial key of the form "k1 = v1 AND ... AND
access cannot be optimized. Every entry must be checked to kn = vn" where k1... kn
match the condition matches a left part of the table key, the access is optimized by
(full table scan). the kernel. In that case, only the matching entries are visited.
One-step approach Three-steps: move, sort, delete dupl.
ITAB2[] = ITAB1[].
REFRESH ITAB2. SORT ITAB2 BY K.
LOOP AT ITAB1 INTO WA. DELETE ADJACENT DUPLICATES FROM ITAB2
READ TABLE ITAB2 WITH KEY K = WA-K COMPARING K.
BINARY SEARCH
TRANSPORTING NO FIELDS.
IF SY-SUBRC <> 0.
INSERT WA INTO ITAB2
INDEX SY-TABIX.
ENDIF.
ENDLOOP.
You can use the same syntax (generic INSERT ... INTO TABLE Filling a hash table is faster than for a sorted table. You can
...) for different types of tables (SORTED, HASHED). access single entries very fast, but partial sequential loops are
Filling a sorted table is equivalent to building it up by "READ more expensive than for sorted tables (see the separate
BINARY SEARCH" and "INSERT ... INDEX SY-TABIX" but it is example).
slightly faster and more elegant to use the generic "INSERT ...
INTO TABLE ...". See the example for building a unique
standard table, if you need no access to the table while it is
build up.
Modifying the complete line Modifying selected components only
WA-DATE = SY-DATUM. WA-DATE = SY-DATUM.
MODIFY ITAB FROM WA INDEX 1. MODIFY ITAB FROM WA INDEX 1 TRANSPORTING
DATE.
With the MODIFY variant "MODIFY itab ... TRANSPORTING f1
f2 ..." the task of updating a line of an internal table can be
accelerated.
The longer the table line is, the larger the speed-up is. The
effect increases for tables with complex structured line types.
Modifying all lines completely Modifying selected components only
LOOP AT ITAB INTO WA. LOOP AT ITAB ASSIGNING <WA>.
I = SY-TABIX MOD 2. I = SY-TABIX MOD 2.
IF I = 0. IF I = 0.
WA-FLAG = 'X'. <WA>-FLAG = 'X'.
Ravi K Page 4 4/8/2009
TIPS & TRICKS
MODIFY ITAB FROM WA. ENDIF.
ENDIF. ENDLOOP.
ENDLOOP.
Accessing the table entries directly in a "LOOP ... ASSIGNING
..." accelerates the task of updating a set of lines of an internal
table considerably. Especially if inner tables must not be moved
the speed-up is high.
I = 250. I = 250.
LOOP AT ITAB1 INTO WA. INSERT LINES OF ITAB1 INTO ITAB2
INSERT WA INTO ITAB2 INDEX I. INDEX I.
ADD 1 TO I.
ENDLOOP.
With the DELETE variant "DELETE itab FROM ... TO ..." the
task of deleting a sequence of lines can be transferred to the
kernel.
Pedestrian way to copy internal tables Let the kernel do the work
REFRESH ITAB2.
LOOP AT ITAB1 INTO WA. ITAB2[] = ITAB1[].
APPEND WA TO ITAB2.
ENDLOOP. Internal tables can be copied by MOVE just like any other data
object.
If an internal table itab has a header line, the table itself is
accessed by itab[].
Pedestrian way to compare int. tables Let the kernel do the work ...
* Both tables have the same contents
* Both tables have the same contents
DESCRIBE TABLE: ITAB1 LINES L1,
ITAB2 LINES L2. IF ITAB1[] = ITAB2[].
" ...
IF L1 <> L2. ENDIF.
TAB_DIFFERENT = 'X'.
ELSE.
TAB_DIFFERENT = SPACE.
LOOP AT ITAB1 INTO WA1.
READ TABLE ITAB2 INTO WA2 INDEX SY-TABIX. Internal tables can be compared in logical expressions just like
IF WA1 <> WA2.
TAB_DIFFERENT = 'X'. EXIT. other data objects.
ENDIF. Two internal tables are equal if
ENDLOOP. - they have the same number of lines and
ENDIF. - each pair of corresponding lines is equal.
IF TAB_DIFFERENT = SPACE. If an internal table itab has a header line, the table itself is
" ... accessed by itab[].
ENDIF.
Sort int. table with default sort key Sort with sort key specified explicitly
SORT ITAB. SORT ITAB BY K.
The more restrictively you specify the sort key, the faster the
program will run.
I2 = <F>. I2 = <I>.
Type C Type I
MOVE SPACE TO SY-SUBRC. SY-SUBRC = 0.
CASE SY-SUBRC. CASE SY-SUBRC.
WHEN '1'. WHEN 1.
WHEN '2'. WHEN 2.
WHEN '3'. WHEN 3.
WHEN '4'. WHEN 4.
ENDCASE. ENDCASE.
N3 = N1 + N2. P3 = P1 + P2.
Use number types for arithmetic. Use type-N fields only for pure
digit strings that are not intended for calculations, e.g.,
telephone numbers or parts of a date or time field.
F3 = F1 * F2. F3 = F1 * F2.