ERPClimb logoERPClimb
Performance Tuning
ABAP Developmentintermediate 7 min read

Performance Part 4: SELECT Inside LOOP — The Classic Performance Killer

Preview · a free account unlocks the full lesson

Key takeaway

Recognise and fix SELECT inside LOOP: why thousands of single reads are slow, how it appears in ST05, rewriting with one set-based read (FOR ALL ENTRIES or JOIN) plus a sorted/hashed lookup table, handling SELECT SINGLE in loops, and hidden cases inside function modules and methods called per row.

In this lessonShow sections

Visual overview

Performance Part 4: SELECT Inside LOOP — The Classic Performance Killer — learning sheetFlow: Run the code example's BAD version with ST05 and note executions → Rewrite to the GOOD version and trace again → Compare runtime and results. Key ideas: WHY IT IS SLOW; RECOGNISING IT; FIX PATTERN; SELECT SINGLE IN LOOPS. Watch out: Hidden versions: a function module, method or FORM called inside the loop that itself does a SELECT — the code looks clean but behaves the same.ERPClimb.com / ABAP / LESSON 4/10 / LEARNING SHEETPerformance Part 4: SELECT Inside LOOP — The ClassicPerformance Killer1 · HOW IT FLOWS1Run the code example'sBAD version with ST05 andnote executions2Rewrite to the GOODversion and trace again3Compare runtime andresults2 · KEY IDEAS EXPLAINEDWhy It Is SlowEach execution has fixed overhead (network,parsing/cursor, DB call) — even a 0.5 ms readcosts 25 seconds for 50,000 rows, and real reads…Recognising ItCode: SELECT inside LOOP/DO/WHILE orSELECT…ENDSELECT with nested SELECT.ST05: statement with executions ≈ number of rows;often also value-identical (same key readrepeatedly).Fix Pattern1. Collect the keys from the driving table (SORT +DELETE ADJACENT DUPLICATES).Select Single In LoopsSame problem; plus often repeated for the same key(value-identical) → at least buffer results in ahashed table; better read all upfront.Buffered tables (customizing) are cheap — theproblem mainly affects application tables.3 · REMEMBER · WATCH OUTRememberA SELECT (or SELECT SINGLE) inside a LOOP executes once per row —50,000 rows = 50,000 database round trips.Fix pattern: collect keys → one set-based read before the loop (FORALL ENTRIES or JOIN) → store in a sorted/hashed table → READ TABLE…ST05 shows it as one structure-identical statement with a very highexecution count.!Watch outHidden versions: a function module,method or FORM called inside theloop that itself does a SELECT — thecode looks clean but behaves thesame.4 · IN A REAL PROJECTAn order backlog report read customer names, material descriptions and plant data with three SELECT SINGLEsper item. With 80,000 items it ran 45 minutes; ST05 showed 240,000 executions. The rewrite read all threedatasets upfront with FOR ALL ENTRIES into hashed tables. Runtime dropped to 2 minutes with identical output.Original ERPClimb learning sheet • Verify against your SAP release and edition

Explanation

Part 3 of this series found the top offenders in a slow program, and the single most frequent one is a SELECT sitting inside a LOOP. A SELECT, or SELECT SINGLE, placed inside a loop executes once per row, so 50,000 rows means 50,000 separate database round trips.

The reason this is so slow is that every single execution carries fixed overhead — network time, parsing and cursor setup, the actual database call — so even a read that only takes 0.5 milliseconds ends up costing 25 seconds across 50,000 rows, and real-world reads are often slower than that.

You can recognize the pattern in a few ways: in the code itself, as a SELECT inside a LOOP, DO, or WHILE, or as a SELECT…ENDSELECT with a nested SELECT; in ST05, as a statement whose execution count is roughly equal to the number of rows, and which is often also value-identical, meaning the same key gets read over and over; and in SAT, as many calls to the same function module or method that each spend time in the database.

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

Referenced SAP objects

Reviewed reference pages for the objects this page mentions.

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.