ERPClimb logoERPClimb
Internal Tables
ABAP Developmentadvanced 9 min read

Internal Tables Part 9: Nested LOOP Optimization and Parallel Cursor

Preview · a free account unlocks the full lesson

Key takeaway

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 sections

Visual overview

Internal Tables Part 9: Nested LOOP Optimization and Parallel Cursor — learning sheetIn one line: 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.ERPClimb.com / ABAP / LESSON 9/10 / LEARNING SHEETInternal Tables Part 9: Nested LOOP Optimization andParallel Cursor1 · IN ONE LINEFix the most common ABAP performance problem — nested loops over two large tables — withsorted tables and LOOP WHERE, hashed lookups, and the classic parallel cursor technique.2 · KEY IDEAS EXPLAINEDThe ProblemLOOP AT gt_flight INTO gs_flight. " 10,000rowsFix 1 — Sorted Inner TableDeclare gt_book as SORTED TABLE … WITH NON-UNIQUEKEY carrid connid fldate. The inner LOOP WHERE onthe leading key fields now uses binary search to…Fix 2 — Hashed LookupIf the inner table has at most one match per key(e.g. flight → airline name), use a HASHED tablewith UNIQUE KEY and READ TABLE … WITH TABLE KEY…Fix 3 — Parallel Cursor (Classic…SORT both tables by the join key.For each outer row: READ TABLE inner WITH KEY …BINARY SEARCH TRANSPORTING NO FIELDS → sy-tabix =first matching row.3 · DO · AVOIDBest practice!Common mistakeSorted inner tables with LOOP WHERE on leadingkey fields.Hashed tables for one-to-one lookups.Parallel cursor for existing standard-tablecode.✕Nested loops over two large standard tables.✕Parallel cursor without sorting both tables bythe same key.✕Forgetting EXIT when the key changes (loops tothe end).4 · IN A REAL PROJECTA freight settlement job matched 40,000 shipments against 1.2 million cost lines with a nested loop overstandard tables. It ran for nine hours and was often cancelled by the batch window. Changing the cost table toa sorted table keyed by shipment number (inner LOOP WHERE) brought runtime down to under five minutes. In…Original ERPClimb learning sheet • Verify against your SAP release and edition

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.

Free account

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.