SAP ABAP Performance Tuning Questions
1. Table of Contents
1.SQL Interface...................................................................................................................... 3
2. String manipulation............................................................................................................. 5
3. Internal Tables.................................................................................................................... 6
4. Internal tables vs. Field group............................................................................................. 8
5. Typing................................................................................................................................. 8
6. If, Case, ............................................................................................................................. 8
7. Field Conversion................................................................................................................. 9
8. Modularization.................................................................................................................... 9
a) Subroutines.................................................................................................................. 9
b) Function Modules...................................................................................................... 10
9. Tools................................................................................................................................. 10
a) Static Analysis (Extended program check).............................................................. 10
b) Dynamic Analysis...................................................................................................... 10
c) Database Access...................................................................................................... 10
10. Getting the data / Data table identification.................................................................... 11
a) F1-F9 / Technical info............................................................................................... 11
b) Runtime analysis....................................................................................................... 11
c) Debug........................................................................................................................ 12
d) SE38.......................................................................................................................... 12
e) Sql trace.................................................................................................................... 12
f) Logical database........................................................................................................ 12
g) Function modules...................................................................................................... 12
11. List of important tables................................................................................................... 13
12. List of function modules................................................................................................. 14
13. Formulas for pricing in MM, SD, and FI......................................................................... 14
1.SQL Interface
* Select ... Where vs. Select + Check
Always specify your conditions in where clause rather than first selecting all the records and then using the check syntax.
* Select with index support
Try and access a table using all its indexes. Consider creating a secondary index for a table (transparent only) which is frequently accessed for read-only operations. If there is no index a full table scan is carried out while reading. The secondary index puts an extra load onto the system whenever the data is changed or new records are created, as the index is also a table and has to be updated. Hence, create a secondary index only if must.
* Select single vs. Select-Endselect
If you are interested in just one database record than use select single rather than select….where
* Select ... Into Table t
Selecting into table is much faster than select…appending the table .
* Select aggregates
If you want to find the maximum, minimum, average and sum from the table, use SQL aggregrate functions.
* Select-Endselect vs. Array-Sele
If you want to process your data only once while selecting, use select into table and loop…endloop rather than select…endselect.
* Select with view
To process a join use a view compared to a nested select statement.
* Select with select list
Use a select list (fields to be selected) or a view compared to select * if you only want to process a few fields.
* Select with buffer support
For all frequently used read-only tables, try to use SAP buffering. The table access is automatically buffered unless the following syntax is used:
* Select distinct
* Select single for update or
* Select single…bypassing buffer
* Select…..aggregate functions
* Using Native SQL
* Select count v/s select single *
For checking whether a database record exists, use select single instead of select count.
* Array Insert VS Single-row Insert
Whenever possible use array operations instead of single row.
e.g.Array insert
INSERT CUSTOMERS FROM TABLE TAB.
Single insert
Loop at tab.
INSERT INTO CUSTOMERS VALUES TAB.
Endloop.
* Column Update
Whenever possible use column updates compared to single-row updates.
e.g
UPDATE SFLIGHT SET SEATSOCC = SEATSOCC - 1.
instead of
SELECT * FROM SFLIGHT.
SFLIGHT-SEATSOCC = SFLIGHT-SEATSOCC –1.
UPDATE SFLIGHT.
ENDSELECT.
* Select single * for update v/s Enqueue
Avoid locking database records with statement select…..update. A better solution is to use enqueue.
* Accessing cluster/pool tables
When accessing cluster/pool tables ensure that access is ALWAYS by primary key. The arrangement of data in the pooled tables is such that all the non-key data is stored as a single field. And a select based on the non-key field is very slow.
* Select header data first
Whenever accessing the item details, first get all the header data and then access the details table giving as many keys as possible using the header data. This is useful in most case like BKPF-BSEG, VBRK-VBRP, LIKP-LIPS, etc. Instead of directly accessing the details table, and accessing via the header table increases the performance. Help of logical database (SE36) hierarchical structure can be used to determine which is the header table. A table higher up in the hierarchy should be accessed before a lower one.
2. String manipulation
* Special operators in IF (CA, ...)
Use the special string operators CA, CO, CS, Concatenate, Split instead of programming the logic yourself as the standard functionality is optimized.
* Deleting leading spaces
If you want to delete the leading spaces in a string use the ABAP statement shift….left deleting leading. Other logic like using sy-fdpos is not that fast.
* String length
Use the strlen() function to restrict the DO loop passes.
* Initializing strings
Use “clear f with val” whenever you want to initialize a field with a value different from the field’s type specific initial value.
3. Internal Tables
* Building condensed tables
If the amount of data is small, the READ/INSERT approach isn’t bad, but for large amounts of data(> 1000), the collect string is much faster but do not use collect with any other table filling statements like append insert as collect cannot use its hash algorithm.
* Building tables without duplicates
Use Delete adjacent duplicate entries after sorting for large amount of data.
* Linear vs. binary search
Binary search for an internal table is much faster than just Read.
* Different forms of key access
If possible, specify the key fields for read access explicitly. Otherwise the key fields have to be computed dynamically by the run time system.
* Secondary indices
If you want to access an internal table with different keys repeatedly use your own secondary indicies. With a secondary index, you can replace a linear search with a binary search plus an index access.
e.g.
READ TABLE TAB_INDEX WITH KEY DATE = SY-DATUM BINARY SEARCH.
IF SY-SUBRC = 0.
READ TABLE TAB INDEX TAB_INDEX-INDX.
ENDIF.
Instead of
READ TABLE TAB WITH KEY DATE = SY-DATUM.
IF SY-SUBRC = 0.
" ...
ENDIF.
* Key access to multiple lines
Loop….where is faster than loop/check because loop…where evaluates the specified condition internally.
* Using explicit work areas
Avoid unnecessary moves by using the explicit work area operations. Append wa to tab.
* Copying internal tables
To copy the entire contents of one table into another, use taba() = tabb() instead of append tabb.
* Comparing internal tables
Internal tables can be compared in a much faster way by if taba() = tabb().
* Sorting internal tables
The more restrictively you specify the sort key, the faster the program will run. i.e. sort tab by k.
* Nested loops
Use the parallel cursor approach for joining or nesting two internal tables.
e.g
I2 = 1.
LOOP AT TAB1.
LOOP AT TAB2 FROM I2.
IF TAB2-K <> TAB1-K.
I2 = SY-TABIX.
EXIT.
ENDIF.
ENDLOOP.
ENDLOOP.
Instead of
LOOP AT TAB!.
LOOP AT TAB2 WHERE K = TAB1-K.
ENDLOOP.
ENDLOOP.
* Modifying selected components
With the modify variant, Modify itab…transporting f1 f2 accelerates the task of updating the internal table.
* Modifying a set of lines
For updating a set of lines use Modify itab…transporting f1 f2 with where….clause.
* Appending a table
With the append variant append lines of tab1 to tab2 the task of appending can be transferred to the kernel which is faster.
* Inserting a table
With the insert variant insert lines of tab1 into tab2 index idx the task of inserting can be transferred to the kernel which is faster.
* Deleting a sequence of lines
With the delete variant delete tab1 from idx1 to idx2 the task of deleting can be transferred to the kernel which is faster.
* Deleting a set of lines
Use delete where to delete a set of lines.
4. Internal tables vs. Field group
When there is large amount of data, go for the field group. When there is a requirement of sorting the extracted data, as the number of records increase, the internal table sorting becomes slower. When there is a case of sorting large amount of data, always use field group.
5. Typing
* Typed vs. untyped Parameters/field symbols
If you specify the type of formal parameters/field symbols in your source code, ABAP can optimize your code more thoroughly. In addition the risk of using the wrong sequence of parameters in a perform statement is much less.
6. If, Case, ....
* If vs. Case
CASE statements are clearer and a little faster than IF-constructions.
* While vs. Do
While is easier to understand and faster to execute.
* Case vs. Perform i Of ...
A very fast way to call a certain routine using a given index is the perform I of …statement. e.g
(I1 = 5 in this test)
PERFORM I1 OF
PV1
.
.
PV5.
Instead of
CASE I1.
WHEN 1. PERFORM PV1.
.
.
.
WHEN 5. PERFORM PV5.
ENDCASE.
7. Field Conversion
* Field Types I and P
Use fields of type I for typical integral variables like indices.
* Literals Type C and Type I
Use numeric literals when you are dealing with type-I variables like sy-subrc instead of character strings.
* Constants Type F
Use properly typed constants instead of literals e.g.
CONSTANTS: PI TYPE F VALUE '3.1415926535897932'.
Instead of
DATA: FLOAT TYPE F.
FLOAT = '3.1415926535897932'.Arithmetic
* Mixed Types
Don’t mix types unless absolutely necessary.