Internal Tables Part 9: Nested LOOP Optimization and Parallel Cursor
Preview · a free account unlocks the full lesson
Fix the most common ABAP performance problem — nested loops over two large tables — with sorted tables and LOOP WHERE, hashed lookups, and the classic parallel cursor technique.
In this lessonShow sectionsHide sections
Visual overview
Explanation
Part 8 covered loop patterns in general; when two tables get combined in memory — headers and items, flights and bookings — the nested loop is where performance is either won or lost.
A LOOP inside a LOOP over two standard tables means every row of the outer table gets compared against every row of the inner table, so rows1 times rows2 comparisons happen in total, and what looked like minutes of runtime quickly becomes hours as the data grows.
There are three practical fixes: make the inner table SORTED by the join key, so that LOOP ...
WHERE gets optimized automatically; if there's only one matching row per key, use a HASHED inner table with READ TABLE instead of a second loop; or, in classic code you can't easily restructure, use the parallel cursor technique — find the starting point with BINARY SEARCH, loop FROM that index, and EXIT once the key changes.
To see the problem concretely, imagine LOOP AT gt_flight INTO gs_flight with 10,000 rows, and inside it, LOOP AT gt_book INTO gs_book WHERE carrid = gs_flight-carrid AND connid = gs_flight-connid AND fldate = gs_flight-fldate with 500,000 rows. With gt_book as a plain standard table, that inner WHERE scans all 500,000 rows for each and every one of the 10,000 flights — five billion comparisons in total.
Create a free account to keep reading
You have read the opening of this lesson. A free ERPClimb account unlocks the rest of it, every other lesson and topic, and your learning history, so you can pick up exactly where you stopped. No card needed.
- Full explanation
- Code example
- Real project scenario
- Common mistakes
- Best practices
- Interview angle
One tap with Google works too. Every topic keeps its first lesson open to everyone.
Keep exploring
Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.