You are on page 1of 11

Performance Tuning in SAP ABAP

Performance tuning techniques in SAP

Run-time analysis
Check efficiency of SAP ABAP programs and Function modules using SAP
run-time analysis

Run-time analysis is used to check the efficiency of a program or function module or t-

code in terms of what is the load on the database server, application server, presentation
server etc.
The run-time analysis will display the load in a graph with %'s and the time in micro

seconds.
The graph will be displayed with either red color or green color.
If the graph contains green color, then the program execution time is very good or very
less.
If the graph contains red color, the program execution time is very bad or very long .
Always make sure the the load on database server should be less than 40% and should
be green.

T-code for run-time analysis is SE30 or SAT(Latest Versions).

Example program on run time analysys in SAP ABAP


Program1: Create a program to get MARA details and display as below.
REPORT ZSAN_SE30.
**Runtime analysis using SE30
DATA : IT_MARA TYPE TABLE OF MARA.
DATA : WA_MARA TYPE MARA.
SELECT * FROM MARA INTO TABLE IT_MARA UP TO 500 ROWS.

LOOP AT IT_MARA INTO WA_MARA.


WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MBRSH, WA_MARA-MATKL, WA_MARAMEINS.
ENDLOOP.

Go to SE30, provide program name, click on execute button, the out put will bi displayed, click on
back and click on evaluate to see run-time analysis.

The result will be like below(may vary based on server capacity).

In the above image you can see ABAP and database layers are in red color, means the time
taken to process these layers is more.
Program:2
Now change the above program like below and test again.
REPORT ZSAN_SE30.
TYPES: BEGIN OF TY_MARA,
MATNR TYPE MARA-MATNR,

MTART TYPE MARA-MTART,


MBRSH TYPE MARA-MBRSH,
MATKL TYPE MARA-MATKL,
MEINS TYPE MARA-MEINS,
END OF TY_MARA.
**Runtime analysis using SE30
DATA : IT_MARA TYPE TABLE OF TY_MARA.
DATA : WA_MARA TYPE ty_MARA.
SELECT MATNR MTART MBRSH MATKL MEINS FROM MARA INTO TABLE IT_MARA UP TO 500 ROWS.
LOOP AT IT_MARA INTO WA_MARA.
WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MBRSH, WA_MARA-MATKL, WA_MARA-MEINS.
ENDLOOP.

Now go to SE30 and follow the above process.

Now the evaluation will be like below.

You can see the database layer is in green color means the standards are good.
A good program with coding standards always have database layers is in green color.

Performance tuning ST05


SAP performance tuning to check the performance of SAP ABAP programs
ST05 is a T-code which is used to trace performance of SAP Objects, things we can done using
ST05 are below.

SQL Trace which is used to trace time execution for open SQL statements like select,
insert, update, delete etc.
Enqueue Trace which is used to trace locking and unlocking of lock objects.
Buffer Trace, this is used to monitor table buffer accesses.
RFC Trace, this is used to trace remote calls made of a application.
HTTP Trace which is used to trace HTTP requests(ex: browser requests) of SAP
applications.

To perform trace operations go to ST05, select trace, activate trace, open another session and
execute the application which we wants to trace, comeback to ST05, deactivate the trace and
display the trace.

ABAP programing standards for


performance tuning
Programing standards for SAP ABAP programers, best practices SAP ABAP
programing
Programming Standard

Never use select *, always use


select with list of fields
because if you use select * it
will get all columns from
database, if you use select list
of fields it will get selected
columns only.

Always specify key fields in


where condition, because it
will be having primary index.

Bad example

SELECT * FROM MARA


INTO TABLE ...

SELECT * FROM MARA


INTO TABLE IT_MARA

Good example

SELECT MATNR MTART MEINS


MBRSH FROM MARA ..

SELECT MATNR MTART MEINS


FROM MARA
INTO TABLE IT_MARA

WHERE MTART = 'FERT'.

WHERE MATNR =
'0001'.

Some times we may need to


use non-key fields in where
condition, in such cases create
secondary index to improve
performance.
Never use select with
corresponding .

SELECT * FROM MARA


INTO CORRESPONDING
FIELDS OF TABLE IT_MARA.

Always use read table key with


READ TABLE IT_MARA INTO
binary search, binary search
WA_MARA WITH KEY MATNR =
will improve performance.
'001'

SELECT MATNR MTART MEINS


FROM MARA
INTO TABLE IT_MARA.

READ TABLE IT_MARA INTO


WA_MARA WITH KEY MATNR =
'001'.

BINARY SEARCH.

Avoid using select joins for


more than 3 database tables.

Select Joins in SAP ABAP

Using select for all entries

Always use select for all


entries for more than three
tables.

Select Joins in SAP ABAP

Using select for all entries

Programming Standard

Bad example

When using select for all


SELECT * FROM MAKT
entries, check whether the
parent internal table is initial or INTO IT_MAKT
not .

FOR ALL ENTRIES IN


IT_MARA

Never use nested loops.

Good example

IF IT_MARA IS NOT INITIAL.


SELECT * FROM MAKT INTO
IT_MAKT<br> FOR ALL ENTRIES
IN IT_MARA<br>
WHERE
MATNR = IT_MARA-MATNR.

WHERE MATNR =
IT_MARA-MATNR.

ENDIF.

LOOP AT IT_MARA INTO


WA_MARA.

LOOP AT IT_MARA INTO


WA_MARA.<br>READ TABLE
IT_MAKT INTO WA_MAKT WHERE
MATNR = WA_MARAMATNR.<br><br>ENDLOOP.

LOOP AT IT_MAKT INTO


WA_MAKT.

ENDLOOP.<br>ENDLOOP.<br>

Never use select statements


inside loops .

<br>LOOP AT IT_MARA INTO INSTEAD USE FOR ALL


WA_MARA.<br>SELECT * FROM ENTRIES
MAKT ...<br><br>ENDLOOP.

Increase performance using


Parallel Cursor in SAP ABAP
The first thing that comes into the mind of a good ABAPer is performance of his object, the main
problem for performance issues are bad Select statements ex: select joins for more than 3
tables, nested Loops, how ever there is a concept of parallel cursor in SAP ABAP to avoid
performance issues with nested loops with where condition.
To understand parallel cursor, follow and understand the below programs.

Program without parallel cursor


REPORT ZSAN_PARALLEL_CURSER.
TYPES: BEGIN OF TY_MARA,
MATNR TYPE MARA-MATNR,
MTART TYPE MARA-MTART,
MBRSH TYPE MARA-MBRSH,
MATKL TYPE MARA-MATKL,
MEINS TYPE MARA-MEINS,
END OF TY_MARA.
DATA : IT_MARA TYPE TABLE OF TY_MARA,
WA_MARA TYPE TY_MARA.
TYPES: BEGIN OF TY_MVKE,
MATNR TYPE MVKE-MATNR,
VKORG TYPE MVKE-VKORG,
VTWEG TYPE MVKE-VTWEG,
END OF TY_MVKE.
DATA : IT_MVKE TYPE TABLE OF TY_MVKE,
WA_MVKE TYPE TY_MVKE.
SELECT
MATNR
MTART
MBRSH
MATKL
MEINS FROM MARA INTO TABLE IT_MARA UP TO 500 ROWS.
SORT IT_MARA.
DELETE ADJACENT DUPLICATES FROM IT_MARA COMPARING ALL FIELDS.
SELECT MATNR

VKORG
VTWEG FROM MVKE INTO TABLE IT_MVKE
FOR ALL ENTRIES IN IT_MARA WHERE MATNR = IT_MARA-MATNR.
LOOP AT IT_MARA INTO WA_MARA.
LOOP AT IT_MVKE INTO WA_MVKE WHERE MATNR = WA_MARA-MATNR.
WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MATKL, WA_MVKE-VKORG, WA_MVKEVTWEG.
ENDLOOP.
ENDLOOP.

Program with parallel cursor.


REPORT ZSAN_PARALLEL_CURSER.
TYPES: BEGIN OF TY_MARA,
MATNR TYPE MARA-MATNR,
MTART TYPE MARA-MTART,
MBRSH TYPE MARA-MBRSH,
MATKL TYPE MARA-MATKL,
MEINS TYPE MARA-MEINS,
END OF TY_MARA.
DATA : IT_MARA TYPE TABLE OF TY_MARA,
WA_MARA TYPE TY_MARA.
TYPES: BEGIN OF TY_MVKE,
MATNR TYPE MVKE-MATNR,
VKORG TYPE MVKE-VKORG,
VTWEG TYPE MVKE-VTWEG,
END OF TY_MVKE.
DATA : IT_MVKE TYPE TABLE OF TY_MVKE,
WA_MVKE TYPE TY_MVKE.
SELECT
MATNR
MTART
MBRSH
MATKL
MEINS FROM MARA INTO TABLE IT_MARA UP TO 500 ROWS.
SORT IT_MARA.
DELETE ADJACENT DUPLICATES FROM IT_MARA COMPARING ALL FIELDS.

SELECT MATNR
VKORG
VTWEG FROM MVKE INTO TABLE IT_MVKE
FOR ALL ENTRIES IN IT_MARA WHERE MATNR = IT_MARA-MATNR.
SORT: IT_MARA, IT_MVKE.
DATA V_INDEX TYPE SY-TABIX.
LOOP AT IT_MARA INTO WA_MARA.
READ TABLE IT_MVKE INTO WA_MVKE WITH KEY MATNR = WA_MARA-MATNR BINARY SEARCH.
IF SY-SUBRC = 0.
V_INDEX = SY-TABIX.
LOOP AT IT_MVKE INTO WA_MVKE FROM V_INDEX. "no where condition for loop
IF WA_MVKE-MATNR <> WA_MARA-MATNR.
EXIT.
ENDIF.
WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MATKL, WA_MVKE-VKORG, WA_MVKEVTWEG.
ENDLOOP.
ENDIF.
CLEAR: WA_MARA, WA_MVKE.
ENDLOOP.

10 best ABAP programming


standards for performance tuning
Below are the most common and best programming standards for developing ABAP
applications.

1. When reading data from database table, never use SELECT *, always use
select with list of fields.
2. Always specify key fields in where conditions of SELECT statements,
because these will have primary index.
3. Sometimes we may need to use non-key fields in where conditions of
SELECT statements, in that case create secondary indexes and use.
4. Never use select with corresponding in SELECT statements.
5. Always use read table with binary search and make you have sorted the
internal table in ascending order before using read table binary search.
6. Never use select joins for more than 3 tables.
7. Always use select for all entries for more than 3 tables.
8. Always check whether the parent internal table is initial or not before using for
all entries.
9. Never use nested loops, instead use parallel cursor .
10. Never use select statements inside loops, instead use for all entries.