ABAP Performance
ABAP Performance
html
V. PERFORMANCE STANDARDS
General Performance Standards
1. "Dead" code
Avoid leaving "dead" code in the program. Comment out variables that are not
referenced and code that is not executed. To analyze the program, use the
Program Analysis function in SE38 -> Utilities -> Program Analysis.
2. Use logical databases
Choose the most efficient logical data base possible. Study the selection criteria
and which secondary indexes are used for that view. Provide the appropriate
selection criteria to limit the number of data base reads. Force users to provide
selection criteria by evaluating the selection criteria entered on the selection
screen during the AT SELECTION-SCREEN event. Finally, when possible take
advantage of the matchcodes to increase speed.
3. Subroutine usage
For good modularization, the decision of whether or not to execute a subroutine
should be made before the subroutine is called. For example:
This is better:
IF f1 NE 0.
PERFORM sub1.
ENDIF.
FORM sub1.
...
ENDFORM.
Than this:
PERFORM sub1.
FORM sub1.
IF f1 NE 0.
...
ENDIF.
ENDFORM.
4. IF statements
When coding IF tests, nest the testing conditions so that the outer conditions
are those which are most likely to fail. For logical expressions with AND, place
the mostly likely false first and for the OR, place the mostly likely true first.
5. CASE vs. nested IFs
When testing fields "equal to" something, one can use either the nested IF or
the CASE statement. The CASE is better for two reasons. It is easier to read
and after about five nested IFs the performance of the CASE is more efficient.
6. MOVE-ing structures
When records a and b have the exact same structure, it is more efficient to
MOVE a TO b than to MOVE-CORRESPONDING a TO b.
MOVE BSEG TO *BSEG.
is better than
Instead of this:
Use this:
loop at <itab>
<do the row-level processing here>
endloop.
instead of using:
SORT ITAB.
13. Number of entries in an internal table
To find out how many entries are in an internal table use DESCRIBE.
DESCRIBE TABLE ITAB LINES CNTLNS.
LOOP AT ITAB.
CNTLNS = CNTLNS + 1.
ENDLOOP.
14. Length of a field
To find out the length of a field use the string length function.
FLDLEN = STRLEN (FLD).
IF FLD CP ‘* #’.
ENDIF.
FLDLEN = SY-FDPOS.
15. Performance diagnosis
To diagnose performance problems, it is recommended to use the SAP
transaction SE30, ABAP/4 Runtime Analysis. The utility allows statistical
analysis of transactions and programs.
16. Nested SELECTs versus table views
Since OPEN SQL does not allow table joins, often a nested SELECT loop will be
used to accomplish the same concept. However, the performance of nested
SELECT loops is very poor in comparison to a join. Hence, to improve
performance by a factor of 25x and reduce network load, you should create a
view in the data dictionary then use this view to select data.
17. If nested SELECTs must be used
As mentioned previously, performance can be dramatically improved by using
views instead of nested SELECTs, however, if this is not possible, then the
following example of using an internal table in a nested SELECT can also
improve performance by a factor of 5x:
Use this:
form select_good.
data: tvbak like vbak occurs 0 with header line.
data: tvbap like vbap occurs 0 with header line.
Instead of this:
form select_bad.
select * from vbak up to 200 rows.
select * from vbap where vbeln = vbak-vbeln.
endselect.
endselect.
endform.
18. SELECT * versus SELECTing individual fields
In general, use a SELECT statement specifying a list of fields instead of a
SELECT * to reduce network traffic and improve performance. For tables with
only a few fields the improvements may be minor, but many SAP tables contain
more than 50 fields when the program needs only a few. In the latter case, the
performace gains can be substantial. For example:
Use:
Instead of using:
Instead of:
<tab> = <tab_wa>.
append <tab> (modify <tab>).
Instead of:
Instead of this:
loop at <tab1>.
append <tab1> to <tab2>.
endloop.
However, if <tab2> is not empty and should not be overwritten, then use: