An index helps to speed up selection from the database. An index is a sorted copy of selected database table fields.
The primary index is always automatically created in an
ABAP-based SAP system. It consists of the
primary key fields of the database table.
This means, for each combination of the index fields exists a maximum of one record in the table. This kind of index is called a UNIQUE index.
If the primary index cannot be
used to determine selection result, (for example, the WHERE
condition does not contain any primary index fields), the system searches the whole table. To prevent this, and determine the selection result by searching through a restricted number of database records, you can create a
secondary index.
However,
you should not define an index for all possible fields in the WHERE condition.
You can use the transaction ABAP Dictionary
Change → Indexes... → Create to create an index. To
make the index unique, select UNIQUE. To specify the fields that will comprise the index, choose "Choose fields". Then save and activate the index.
Even if an index consists of several fields, you can still use it when only a few of the fields actually
appear in the WHERE clause. The sequence in which the fields are specified in the index is important.
You can only use a field in the index if all of the preceding fields in the index definition are included in the WHERE condition.
An index can only support search criteria which describe the search value positively, such as EQ or LIKE. The response time of conditions including NEQ is not improved by an index.
An index should only consist of a few fields; as a rule, no more than four. This is because the index has to be updated each time you change its fields in a database operation.
You should not create more than five indexes for any one table because:
If you are using more than one index for a database table, ensure that they do not overlap.
The optimizer generally stops if the WHERE condition contains an OR expression. In other words, it does not evaluate the fields in the OR expression with reference to the index.
An exception to this are OR statements standing on their own. Try to reformulate conditions containing an OR expression for one of the indexed fields. For example, replace:
SELECT * FROM SPFLI
WHERE CARRID = 'LH'
AND (CITYFROM = 'FRANKFURT' OR CITYFROM = 'NEW YORK').
with:
SELECT * FROM SPFLI
WHERE (CARRID = 'LH' AND CITYFROM = 'FRANKFURT')
OR (CARRID = 'LH' AND CITYFROM = 'NEW YORK').
The value NULL is not stored in the index structure of some database systems. The consequence of this is that the index is not used for that field.