Professional Documents
Culture Documents
2008.06.24
Ian Maxwell
(Ian.Maxwell@Accenture.com)
1
SAP Development Community of Experts
Writing and Tuning ABAP Programs for Best The Development Soapbox
Performance
In the years that Ive spent working with
SAP development a lot of my time has
been spent looking at areas of
performance tuning
Unfortunately performance
considerations have often been treated
as an after thought
This is not an exhaustive presentation it
covers a few areas that should be
considered when looking at performance
2
SAP Development Community of Experts
Writing and Tuning ABAP Programs for Best The Development Soapbox
Performance
When it comes to performance some of
the common things we hear from
projects are:
We are going live next week and our
nightly batch schedule takes over 40 hours
to run!
Creating a single sales order takes 5
minutes
The program sits there and doesnt seen
to do anything for hours
HELP!!!!!!
3
SAP Development Community of Experts
Bottleneck Bottleneck
6
SAP Development Community of Experts
Overall ABAP
Performance
Excessive
Nested No Index Poor Commit Network
Data Dispatcher
Loops Support Logic Latency
Manipulation
Sequential Poorly
Redundant Unnecessary Program
Scanning Of Structured
Operations Selects Generation
Data Selects
7
SAP Development Community of Experts
8
SAP Development Community of Experts
9
SAP Development Community of Experts
16
SAP Development Community of Experts
17
SAP Development Community of Experts
18
SAP Development Community of Experts
Hash Table
88 4 90 5 27 18 2 53 30 67 13 65 40 3 19 85 49 64 15 42 79
64 Hash Search - Search 1 record
Average search time = 1
19
SAP Development Community of Experts
=
Loop at i_FooBar
INTO wa_FooBar.
INTO wa_FooBar
WHERE Blah = l_Blah.
If wa_FooBar = l_Blah.
***** Do Stuff ***
***** Do Stuff ***
EndIf
EndLoop.
EndLoop.
It is a common misconception that the LoopWhere syntax on a standard internal table will
help the performance of a loop. It actually will still do a sequential scan of the data so the
above two code segments are equivalent from a performance perspective. Because the loop
statement has no knowledge of the sort order of a standard internal table it must look at every
record even if it has been previously sorted.
If the loop is only executed once then a LoopWhere syntax is not usually an issue but if the
loop is nested then it is executed multiple times and there can be a high amount of waste.
20
SAP Development Community of Experts
3) If the record is not a record that matches the If wa_FooBar-Blah <> l_Blah.
Exit.
criteria then all matching records have been Endif.
processed and you can exit the loop.
***** Do Stuff ***
Note, for the code to work it is important that the table
is sorted appropriately by the field Blah. EndLoop.
Reduce waste by not processing records that you dont need to!
22
SAP Development Community of Experts
23
SAP Development Community of Experts
IT_49 75.6%
LOOP AT i_mdsb INTO wa_mdsb1
WHERE matnr EQ wa_mdsb-matnr
AND bdter GT wa_mdsb-bdter.
This loop on i_mdsb is nested and will always do an exhaustive sequential scan on the
internal table.
Recommendation: The internal table is already sorted by Matnr and Bdter. I suggest a
more effective access pattern of Binary Search + Loop From Index + Exit instead of
using a loop where.
24
SAP Development Community of Experts
Sort + Search Records are accessed using a binary search = nlog2n + ( log2n *
Binary Average runtime complexity for a search is log2n s)
A sort is required before any searches can be performed
A sort runtime complexity is nlog2n
n = number of records
s = number of searches
25
SAP Development Community of Experts
27
SAP Development Community of Experts
29
SAP Development Community of Experts
Database Access
30
SAP Development Community of Experts
Database access is a key part of performance tuning and requires a good understanding of how the underlying database handles queries.
Note This presentation will focus on the use of an Oracle database, although the main concepts can be used when looking at performance on
a variety database systems for SAP
Co
SQL
Database
st
Data
?
31
SAP Development Community of Experts
32
SAP Development Community of Experts
CBO
Data Statistics
Gather Statistics
Oracle RDBMS
ABAP Program
SQL Statement
33
SAP Development Community of Experts
34
SAP Development Community of Experts
35
SAP Development Community of Experts
36
SAP Development Community of Experts
999,000 500,000
Records Records
99.9%
500,000
Records
1,000
Records 0.1%
Due to the low selectivity the index will be ignored and the
optimizer will choose another access method.
39
SAP Development Community of Experts
Multi-Field Index:
Order Customer Create
Type Number Date
Good Candidate: All fields in
Select.
the index are utilized
Where Order_Type = A
And Customer_Number = 12345
And Create_Date = 20080610
41
SAP Development Community of Experts
42
SAP Development Community of Experts
43
SAP Development Community of Experts
45
SAP Development Community of Experts
By centralizing data access, the cache can be used across various parts of
the program.
46
SAP Development Community of Experts
1 Clear the cache when a certain criteria is met. In this example the Sales Order Number is used
2 Check the cache to see if the record has already been read (Note, the lookup uses a hash for fast access)
3 Because the value wasnt in the cache, lookup the record in the database
48
SAP Development Community of Experts
49
SAP Development Community of Experts
Batch Window
1 Process: 10 Hours
2 Processes: 5 Hours
5 Hours
4 Processes: 2.5 Hours
2.5 Hours
2.5 Hours
2.5 Hours
Note This example assumes no diminishing returns (Covered in later slides)
50
SAP Development Community of Experts
result in an increase in
runtime due to Number of Threads
contention for
Note: The following is for example purposes only, results will vary depending on the specifics of the job
resources and the system
52
SAP Development Community of Experts
2 4 9 2 4 9
6 6
1 1
3 7 3 7
5 8 5 8
Process 3
Process 1
Process 2
Process 3
Process 1
1 4 8 2 5 7 3 6 9 1 5 9 8 4 6 2 3 7
Records are divided up based on a predefined All processes continue until there are no records
criteria or a preprocessing program remaining to be processed. All processes complete
processing at approximately the same time
If some records take longer to process then others
then some processes will finish later then others Processes can be added or removed at runtime
53
SAP Development Community of Experts
Execute on Execute on
Customer group Data Customer group Data
Z01 Z02 Execute on
Preprocessor a queue
Creates Queues
Queues
Driver/Worker model via Asynchronous RFCs Worklist pattern via Enqueue Objects
Worker Work
Data Processing List
Data Program
Driver
Enqueue Server
54
SAP Development Community of Experts
55
SAP Development Community of Experts
Questions
?
56