If you use the READ statement with an explicit key specification in the form WITH KEY k1 = v1 ... kn = vn, the key access is optimized in all cases in which this is possible, that is:
If there is a partial key specification that is sufficient for the above conditions, then the system initially uses this partial key to search for an entry. In the case of sorted tables, this is done using binary search, that is with logarithmic effort; in the case of hash tables using the internal hash algorithm, that is with constant effort. If the system finds an entry, it checks whether the remaining key conditions are also met. In particular, this means that over-specified key specifications are also optimized.
The following READ statements are all optimized by the system:
DATA: wa TYPE sflight,
SrtTab TYPE SORTED
TABLE OF sflight
WITH
NON-UNIQUE KEY carrid connid,
HshTab TYPE HASHED TABLE OF sflight
WITH
NON-UNIQUE KEY carrid connid.
READ TABLE SrtTab INTO wa WITH KEY carrid = 'LH'.
READ
TABLE SrtTab INTO wa WITH KEY carrid = 'LH'
connid = '0400'.
READ TABLE SrtTab INTO wa WITH KEY carrid = 'LH'
seatsmax = 100.
READ TABLE SrtTab INTO wa WITH KEY seatsmax = 100
carrid = 'LH'.
READ TABLE HshTab INTO wa WITH KEY carrid = 'LH'
connid = '0400'.
READ TABLE HshTab INTO wa WITH KEY carrid = 'LH'
connid = '0400'
seatsmax = 100.
Conversely, the following READ statements cannot be optimized
because the specified key does not offer any optimization possibilities. Therefore, these tables result in a Full Table Scan (linear search of all table entries):
READ TABLE SrtTab INTO wa WITH KEY connid = '0400'.
READ TABLE SrtTab INTO wa WITH KEY seatsmax = 100.
READ TABLE HshTab INTO wa WITH KEY carrid = 'LH'.
The partially sequential access of a table segment of the type SORTED TABLE using a WHERE condition (LOOP ... WHERE ..., DELETE ... WHERE ..., MODIFY ... WHERE ...) is optimized, if:
Similarly to the READ optimization, the starting point
for the loop is determined by a binary search with the simple conditions, which partly or even completely
cover the table key. From the starting point onwards, the loop is processed only so long as these simple conditions are still met.
The following LOOP statements are all optimized by the system:
LOOP AT SrtTab INTO wa WHERE carrid = 'LH'.
...
ENDLOOP.
LOOP AT SrtTab
INTO wa WHERE carrid = 'LH' AND
connid
= '0400'.
...
ENDLOOP.
LOOP AT SrtTab INTO wa WHERE carrid
= 'LH' AND
seatsmax >
99 AND
seatsmax < 200.
...
ENDLOOP.
while the following LOOP statements cannot be optimized and therefore result in a full table fsScan:
LOOP AT SrtTab INTO wa WHERE connid = '0400'.
...
ENDLOOP.
LOOP AT SrtTab INTO wa WHERE carrid >= 'LH' AND
connid = '0400'.
...
ENDLOOP.