Get Example source ABAP code based on a different SAP table
ABAP_ITAB - Optimizing the WHERE> Condition The statements LOOP AT>>, DELETE>>, and MODIFY>> can be specified together with a WHERE> condition that selects specific lines of the internal table. Searches in a standard table> using the primary table key> are always linear and cannot be optimized. Searches using a sorted key> or a hash key>, however, can be optimized in certain circumstances. These keys are used for:
accessing a sorted table> using its primary key.
accessing a hashed table> using its primary key.
accessing a table of any table type using a secondary table key> specified by USING KEY>. The optimization takes place by mapping all relational expressions> or a group of the relational expressions in the WHERE> condition to a specified key. This specified key performs a corresponding optimized key access (binary search or hash algorithm as in the statement READ TABLE>> or a table expression>), to find one or more lines. The found line or lines are then checked using those remaining relational expressions from the WHERE> condition that were not mapped to the specified key. Prerequisites for the optimization are therefore:
The relational expressions of the WHERE> condition can be transformed to a specified key.
The corresponding key access returns the same results as the evaluation of this part of the logical expression would. This is guaranteed only for compatible data types, since in the case of incompatible data types:
the content of the specified data objects is converted to the data type of the columns before the evaluation in the case of key accesses.
The comparison rules> for incompatible data types apply when evaluating a comparison expression>. Here, all the data types involved play a part in determining which operand is converted into which comparison type. If there are no or insufficient relational expressions to meet both of these prerequisites, no optimization is possible and the behavior is as follows:
If a sorted table or a hashed table is accessed using the primary table key, all lines of the internal table are checked as in a standard table.
If the table is read using a secondary table key, that is, if said key is specified after USING KEY>, a syntax error or syntax check warning, or an exception is raised. Access using a secondary table key must always be executed in an optimized way. The following sections describe exactly when an access can be optimized. ITOC
Latest notes: If the lines selected using WHERE> are modified or deleted using MODIFY>> or DELETE >> and not just read using LOOP AT>>, further update costs are incurred in addition to the search for the lines. When deleting lines from standard tables, it should be noted that searches using a secondary key optimize the selection of lines to be deleted, but not the update required for the primary key, which is usually performed using a linear search. NON_V5_HINTS ABAP_HINT_END
Prerequisites for the Optimization of Hash Keys In the case of hash key> accesses, there must be exactly one relational expression combined using AND> for each component of the key. This expression is either
a comparison expression> with the comparison operator => (or EQ>), whose operands meet the requirements above or
a predicate expression> IS INITIAL> without the addition NOT>. These relational expressions construct the part of the logical expression used for the key access. This part must not contain duplicate key components. The remainder of the logical expression can contain any number of relational expressions combined using AND>. Optimization may not be possible, however, if the Boolean operators NOT> or OR> are used. Key components may be used in the relational expressions not used for the key access.
Latest notes: Relational expressions other than comparisons using => (or EQ >) or predicate expressions IS INITIAL> are not part of the key access. This applies particularly to the tabular comparison operator IN range_tab>>, even if it can be traced back to optimizable comparisons. NON_V5_HINTS ABAP_HINT_END