Code examples · Modern ABAP · Advanced
The most common performance bug: SELECT in a loop versus one join, measured with ST05
The problem
A report that lists open sales orders with the customer name takes minutes in production but was fast in the test system.
How we solve it
- 1The slow wayloop with SELECT
- 2The fast wayone join
- 3Prove itcompare and ST05
The result
Both versions return the same 200 rows, and the join needs one database call instead of about 200.
SAP objects in play
1. The requirement
A report that lists open sales orders with the customer name takes minutes in production but was fast in the test system. Find the cause, fix it, and show the proof so that the reviewer believes it.
2. How we design it
The slow version asks the database once per order: 200 orders mean 200 round trips. The fast version asks once and lets the database combine the two tables. The cost of a database call is the trip, not the size of the answer, which is why the test system with 20 orders looked fine. We measure both versions in the same program with the run time clock and count the database calls, and then confirm it independently with the SQL trace ST05, because a measurement from the program alone is easy to doubt.
Why we decided it this way
- 1Measure, do not guessA performance fix that is not measured is an opinion. Two numbers in the same output are the proof.
- 2One join instead of a loop with SELECT SINGLEOne round trip instead of 200. The database can also choose a good access path for the whole query.
- 3Confirm with ST05The trace counts the real statements sent to the database and cannot be influenced by the program.
- 4Same result, checkedA faster program that returns something else is not a fix. The program compares both results.
Before you start
- A system with at least a few hundred sales orders.
- Authorization to use the SQL trace (transaction ST05) and SAP GUI.
3. The code, step by step
Step 1 of 3: The slow way: a SELECT in a loop
A version that works and is slow, with a clock around it.
" Read 200 orders first
CLASS zcl_loop_vs_join DEFINITION PUBLIC FINAL CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
ENDCLASS.
CLASS zcl_loop_vs_join IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
SELECT vbeln, kunnr FROM vbak
ORDER BY vbeln
INTO TABLE @DATA(orders)
UP TO 200 ROWS.
TYPES: BEGIN OF ty_result,
vbeln TYPE vbak-vbeln,
name1 TYPE kna1-name1,
END OF ty_result,
ty_results TYPE STANDARD TABLE OF ty_result WITH EMPTY KEY.
DATA slow_result TYPE ty_results.
DATA: t0 TYPE i, t1 TYPE i.
" SELECT SINGLE inside LOOP
GET RUN TIME FIELD t0.
LOOP AT orders INTO DATA(order).
SELECT SINGLE name1 FROM kna1 WHERE kunnr = @order-kunnr INTO @DATA(customer_name).
IF sy-subrc = 0.
APPEND VALUE #( vbeln = order-vbeln name1 = customer_name ) TO slow_result.
ENDIF.
ENDLOOP.
GET RUN TIME FIELD t1.
DATA(slow_ms) = ( t1 - t0 ) / 1000.
out->write( |Loop with SELECT SINGLE: { lines( slow_result ) } rows, about { lines( orders ) } database calls, { slow_ms } ms| ).
Part by part (plain text)
- Read 200 orders first. The orders come from one SELECT. We take only the order number and the customer number. The two clocks use microseconds as whole numbers, so the arithmetic is simple.
- SELECT SINGLE inside LOOP. For every order the program asks the database for one customer. GET RUN TIME FIELD gives the clock in microseconds, so the difference of two readings is the time of the block. The result is appended only when a customer was found, which matches what the join returns. This is the pattern to find in a code review: a database statement between LOOP and ENDLOOP.
Open in the portal
The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.
4. The final outcome
Both versions return the same 200 rows, and the join needs one database call instead of about 200.
What you should see
Loop with SELECT SINGLE: 200 rows, about 200 database calls, 184 ms One join: 200 rows, 1 database call, 6 ms Results are identical (Times differ by system. The difference grows with the number of orders.)
Try it and check the result
- Put the code parts together in order, activate and run with F9. Compare the two lines and the last line.
- Raise UP TO 200 ROWS to 2000 in both SELECTs: the loop version grows with the number of orders, the join stays nearly flat.
- Follow the ST05 steps and count the statements on KNA1 for the loop version.
Common mistakes
- Performance in the test system with a few rows proves nothing. The problem shows up with production volumes.
- INNER JOIN drops orders whose customer does not exist. Use LEFT OUTER JOIN if every order must appear, and decide what the report shows for a missing customer.
- Times in a single run are noisy: the first run also fills caches. Run twice and compare the second.
- A join is not always the answer. Very large joins without a good WHERE condition can be slow too. Read the execution plan or use the SQL monitor when the join itself is slow.