Performance Tuning
ABAP Developmentintermediate

Performance Part 5: FOR ALL ENTRIES — Correct Use and Hidden Risks

Key takeaway

Use FOR ALL ENTRIES correctly: the empty driver table trap (reads the whole table), automatic duplicate removal (and why you must select full keys), sorting and deleting duplicates in the driver table, package sizes, large driver tables, and when a JOIN or range is better.

Explanation

📌 KEY POINTS
✅ If the driver table is empty, FOR ALL ENTRIES ignores the whole WHERE condition part with the table and reads all rows — always check IS NOT INITIAL first.

✅ FAE removes duplicate result rows (like DISTINCT on selected fields) — select all key fields or you lose rows (e.g. two items with equal values).

✅ Deduplicate the driver table (SORT + DELETE ADJACENT DUPLICATES on the used fields) to reduce database work.

⚠️ Very large driver tables are split into many statements (packages); a JOIN or a CDS view can be much faster when both tables are in the database anyway.

━━━━━━━━━━━━━━━━━━━━
▶ WHERE WE ARE

Part 4 used FAE to replace SELECT in LOOP. FAE has pitfalls that cause wrong results or huge reads.

━━━━━━━━━━━━━━━━━━━━
▶ 1. EMPTY DRIVER TABLE

  • FAE with an empty table → the FAE conditions are dropped → full table read (or all rows matching other conditions).
  • Always: IF lt_driver IS NOT INITIAL. … ENDIF.

━━━━━━━━━━━━━━━━━━━━
▶ 2. DUPLICATE REMOVAL

  • The result is made unique based on the selected columns.
  • Example: selecting VBELN and NETWR from VBAP for orders: two items with the same net value collapse into one row → wrong totals. Include POSNR (full key).

━━━━━━━━━━━━━━━━━━━━
▶ 3. DRIVER TABLE HYGIENE

  • Only fields used in WHERE; SORT and DELETE ADJACENT DUPLICATES.
  • Avoid FAE on huge driver tables (hundreds of thousands) without packaging (Part 9).

━━━━━━━━━━━━━━━━━━━━
▶ 4. HOW IT RUNS

The database interface converts FAE into several statements with IN lists or OR conditions (profile parameter rsdb/max_blocking_factor etc.); HANA handles it efficiently, but many packages still mean many round trips.

━━━━━━━━━━━━━━━━━━━━
▶ 5. ALTERNATIVES

  • JOIN when the driver data comes from the database anyway (Part 7).
  • Range table (IN) for small sets of single values.
  • CDS views for reusable joins/aggregations (Part 8).

━━━━━━━━━━━━━━━━━━━━
▶ 6. OTHER RULES

  • No ORDER BY with FAE except PRIMARY KEY; aggregates are not allowed in the same SELECT (in classic Open SQL) — aggregate afterwards or use JOIN.

━━━━━━━━━━━━━━━━━━━━
▶ STEP BY STEP

  1. Run the code example with an empty driver table (in a test system) and see the row count explode — then add the check.
  2. Select without POSNR and compare the row count with POSNR included.
  3. Deduplicate the driver table and compare ST05 executions.

✅ CHECKPOINT: you use FOR ALL ENTRIES safely and know when to choose alternatives.

Code example

ABAP Code
REPORT zperf_fae. TYPES: BEGIN OF ty_head,         vbeln TYPE vbeln_va,       END OF ty_head,       BEGIN OF ty_item,         vbeln TYPE vbeln_va,         posnr TYPE posnr_va,          " full key -> no lost rows         netwr TYPE netwr_ap,       END OF ty_item. DATA: lt_head TYPE STANDARD TABLE OF ty_head,      lt_item TYPE STANDARD TABLE OF ty_item,      lv_rows TYPE i. PARAMETERS p_auart TYPE auart DEFAULT 'OR'. START-OF-SELECTION.  SELECT vbeln FROM vbak INTO TABLE lt_head    WHERE auart = p_auart AND erdat >= '20260101'. * ===== Driver hygiene =====  SORT lt_head BY vbeln.  DELETE ADJACENT DUPLICATES FROM lt_head COMPARING vbeln. * ===== Mandatory empty check: otherwise ALL VBAP rows are read =====  IF lt_head IS NOT INITIAL.    SELECT vbeln posnr netwr FROM vbap      INTO TABLE lt_item      FOR ALL ENTRIES IN lt_head      WHERE vbeln = lt_head-vbeln.  ENDIF.   DESCRIBE TABLE lt_item LINES lv_rows.  WRITE: / 'Items read:', lv_rows. * Without POSNR in the field list, items with identical VBELN+NETWR would be merged* (FAE removes duplicate result rows) -> wrong totals.

Real project scenario

A nightly interface occasionally took 6 hours instead of 10 minutes and transferred the entire BSEG content. On days without new documents, the driver table was empty and FOR ALL ENTRIES read everything.
Adding the empty check fixed the runtime. A second bug surfaced in review: FAE without the item number lost items with equal amounts, producing small differences in totals that finance had been reconciling manually.

Common mistakes

  • Missing empty driver check.
  • Not selecting full keys (lost rows).
  • Duplicate entries in the driver table.
  • FAE on huge driver tables without packages.
  • Using FAE where a JOIN fits better.

Best practices

  • Always check IS NOT INITIAL.
  • Select key fields to keep rows unique.
  • Sort and deduplicate the driver.
  • Package very large drivers.
  • Consider JOIN/CDS alternatives.

Interview angle

Questions: "What happens with FOR ALL ENTRIES if the driver table is empty?", "Why can FAE lose records?", "FAE vs JOIN?"
Strong answer: empty table ignores the FAE condition and reads all rows — check IS NOT INITIAL; FAE removes duplicates on selected fields — include full keys; deduplicate driver; converted into packaged statements; JOIN better when both sides in DB; no aggregates/ORDER BY (except primary key) with FAE in classic SQL.

Practice this lesson

Quick check

1. Why must a FOR ALL ENTRIES driver table be checked for emptiness?

2. Why include full key fields in a FOR ALL ENTRIES field list?

3. What must be checked before using FOR ALL ENTRIES?

Interview questions

1. An MM report occasionally reads nearly all purchase order items and runs for hours. What is the likely cause?

Answer it in your own words first, then compare.

2. What is the risk of using FOR ALL ENTRIES with an empty internal table?

Answer it in your own words first, then compare.

Practice more with Interview AI →

Reference-verified: the steps are checked against official SAP documentation and tutorials, and object and field names in the sample code are checked for consistency. The code has not been compiled or run on an SAP system by ERPClimb. Syntax marked release-dependent varies by SAP release, so check it in your own system.

More lessons in this topic

Interview questions on this topic

Related SAP topics