Secondary Keys on ABAP Internal Tables
Secondary table keys give one ABAP internal table additional sorted or hashed access paths without changing its primary table category. They can make repeated alternative-key reads dramatically cheaper, especially on large tables, but they consume memory and add maintenance cost when rows are inserted, deleted, or changed. Use them only when a measured access pattern justifies the extra index or hash structure.
Practitioner guide to Secondary Keys on ABAP Internal Tables: what it means, when to use it, how it fits SAP architecture, a concrete support/development example, decision rules, production pitfalls, S/4HANA implications, ownership, and related technical objects.
Reviewed by an ERPClimb SAP consultant on 20 Sept 2026· 734 words
What it is
An internal table always has a primary key, and ABAP additionally supports named secondary sorted and hashed keys. A sorted secondary key creates a secondary table index and can be unique or non-unique; a hashed secondary key provides hash-based access and is unique. Statements must explicitly select a secondary key when needed, for example with USING KEY or KEY additions. SAP documentation notes that secondary keys improve access but cost memory and runtime to build or maintain. That trade-off makes them particularly valuable for tables filled once and read many times through more than one business key.
When to use it
Use a secondary key when the same large in-memory dataset is repeatedly read by an access pattern that is not well served by the primary key. Typical cases include a standard table that must preserve input order but is also looked up thousands of times by document/item, or a hashed table that needs ordered iteration through another field combination. Avoid secondary keys when the table is tiny, changes constantly, or the alternative lookup happens only once. A second SELECT or different primary table type can sometimes be simpler.
How it fits the stack
Secondary keys are an in-memory indexing mechanism, analogous in purpose—not implementation—to database indexes. They work with READ TABLE, table expressions, LOOP, MODIFY, DELETE, FILTER and other internal-table statements that can name a key. They do not change the database query that originally filled the table. The design choice therefore comes after the database result set has been minimized and before hot loops are optimized. A useful review asks which keys are read often, which rows are changed often, and whether the index cost is amortized.
A worked example
A batch program loads purchase-order items into a standard table because it must later reproduce the original order. During enrichment it repeatedly searches by material/plant and again by purchase-order/item. Instead of sorting and resorting the same standard table, the developer adds a named sorted secondary key for material/plant and a hashed secondary key for purchase-order/item. The loops explicitly select the appropriate key. Runtime drops because the table no longer performs repeated linear scans, while the primary sequence remains available for output.
How to choose
- Add a secondary key only for an access path used often enough to repay its memory and maintenance cost.
- Use a sorted secondary key for ordered or partial-key access; use a hashed secondary key for complete unique-key lookup.
- Keep key width as small as business semantics allow; wide keys increase index/hash overhead.
- If writes dominate and reads are rare, a secondary key can make performance worse.
- Benchmark the full workload, including table fill and change operations, not only the read statement.
Common pitfalls
- Declaring a secondary key but forgetting to specify it in the consuming statement, so ABAP still uses the primary key.
- Adding many secondary keys 'just in case', increasing memory and change cost.
- Using a hashed secondary key where duplicate business values are valid.
- Modifying key-relevant fields without considering uniqueness and maintenance behavior.
- Assuming a secondary key fixes a database query that already fetched too many rows.
ECC, S/4HANA and clean core
Secondary internal-table keys are part of current ABAP and remain relevant on S/4HANA and ABAP Cloud. HANA optimizes database operations, not arbitrary in-memory scans after data has reached the ABAP application server. Modern code using table expressions can still benefit from named optimized keys, but the expression must be compatible with the chosen key to obtain the intended access path.
Whose problem this is
ABAP development owns the declaration and explicit key usage. Performance engineering should validate the benefit with SAT or controlled benchmarks on realistic row counts. Business owners define whether a key is truly unique; that semantic fact must not be guessed from test data.
Related SAP objects
Reviewed pages this object connects to in the ERPClimb knowledge graph.
Source: ERPClimb — https://erpclimb.com/sap-technical-topics/abap-internal-table-secondary-keysERPClimb is an independent platform and is not affiliated with SAP SE. Reference pages are written and reviewed by SAP consultants for learning and troubleshooting.