What are the internal table categories in ABAP, how do keys work, and when do you use READ TABLE with BINARY SEARCH instead of a sorted or hashed table?
Say your answer out loud first.
Practise another way
There are three categories. Standard tables are accessed by index and a key read is linear. Sorted tables stay ordered by their key and a key read is a binary search.
Hashed tables have a unique key and a constant-time key read but no index. I use BINARY SEARCH only on a standard table that I sorted by exactly that key.
If the table is mostly looked up, I declare it sorted or hashed, because then the system guarantees the order for me.
Categories and cost (ABAP keyword documentation):
- Standard table: index access is fastest, filled with APPEND. A key read is a linear search. If you SORT it first, a read with BINARY SEARCH is logarithmic.
- Sorted table: always ordered by the primary key, filled with INSERT. A key read is a binary search, done by the system. The key may be unique or non-unique. Good for partly sequential LOOP with the first key part in the WHERE condition.
- Hashed table: key must be unique, no index access, and the cost of a key read does not grow with the table size.
Keys. Every table has a primary key (the default key if you give none) and may have secondary keys. A secondary sorted or hashed key lets one table be read fast in two ways, so you avoid keeping two copies.
BINARY SEARCH. This addition on READ TABLE only gives correct results if the standard table is sorted by the same fields, in the same order, as the search key. If it is not sorted, the read can miss rows that exist, and sy-subrc is 4 with no error.
This is the classic bug. With a sorted or hashed table the system uses binary or hash search itself when your search key starts with the primary key (hashed: the full key).
TYPES: BEGIN OF ty_stock, matnr TYPE matnr, werks TYPE werks_d, qty TYPE i, END OF ty_stock.
DATA lt_old TYPE STANDARD TABLE OF ty_stock WITH DEFAULT KEY.
DATA lt_new TYPE HASHED TABLE OF ty_stock WITH UNIQUE KEY matnr werks.
SORT lt_old BY matnr werks. " old style
READ TABLE lt_old INTO DATA(ls_old)
WITH KEY matnr = 'M1' werks = '1000' BINARY SEARCH.
DATA(ls_new) = VALUE #( lt_new[ matnr = 'M1' werks = '1000' ] OPTIONAL ). " 7.40+
How I choose. If I read by key many times, hashed (unique key) or sorted. If I only append and loop, standard. If I need order and key lookups, sorted. Modern code uses table expressions and secondary keys rather than BINARY SEARCH.
Release note. Header lines and the OCCURS addition are obsolete and not allowed inside classes.
Names all three categories with the cost of a key read, says BINARY SEARCH needs the same sort order and shows the silent failure, and mentions secondary keys.
Says hashed is always fastest and uses BINARY SEARCH without sorting, or cannot say what happens to a key read on a standard table.
Using BINARY SEARCH without a SORT on the same key and order. The result is a wrong sy-subrc, not a dump, so the bug survives testing with small data.
- What happens if you use BINARY SEARCH on an unsorted table?
- When do you add a secondary key instead of a second table?
- Why can a hashed table not be read by index?
- How does a table expression differ from READ TABLE when the row is missing?
4 official pages and 8 prep guides. Confidence: high. Checked 21 Nov 2026.
- Official page Backs the answer.
- Official page Backs the answer.
- Official page Backs the answer.
- Official page Backs the answer.
- Prep guide Gives the question.
- Prep guide Gives the question.
- Prep guide Gives the question.
- Prep guide Gives the question.
- Prep guide Gives the question.
- Prep guide Gives the question.
- Prep guide Gives the question.
- Prep guide Gives the question.