Performance Part 4: SELECT Inside LOOP — The Classic Performance Killer
Preview · a free account unlocks the full lesson
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 sectionsHide sections
Visual overview
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.
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
Interview questions on this topic
Related SAP topics
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.