Thursday, May 3, 2012

ABAP Performance Tuning Tips

Tools that can be used to help with performance tuning.
1. ST05 is the performance trace. It contains the SQL Trace plus RFC, enqueue and buffer trace. Mainly the SQL trace is is used to measure the performance of the select statements of the program.
2. SE30 is the Runtime Analysis transaction and can be used to measure the application performance.
3. SAT transaction is the replacement SE30. It provides same functionality as SE30 plus some additional features.
4. ST12 transaction (part of ST-A/PI software component) is a combination of ST05 and SAT. Very powerful performance analysis tool used primarily by SAP Support.

5. One of the best tools for static performance analyzing is Code Inspector (SCI). There are many options for finding common mistakes and possible performance bottlenecks.
Steps to optimize the ABAP Code
1. Database
a. Use WHERE clause in your SELECT statement to restrict the volume of data retrieved. 
b. Design the Query to use as many index fields as possible from left to right in the WHERE statement
c. Use FOR ALL ENTRIES in your SELECT statement to retrieve the matching records at one shot
d. Avoid using nested SELECT statement and SELECT within LOOPs, better use JOINs or FOR ALL ENTRIES. Use FOR ALL ENTRIES when the internal table is already there. Try JOINs if the SELECT statements are right behind each other.
e. Avoid using ORDER BY in SELECT statements if it differs from used index  (instead, sort the resulting internal table), because this may add additional work to the database system which is unique, while there may be many ABAP servers
f. INDEX: Creation of Index for improving performance should not be taken without thought. Index speeds up the performance
but at the same time adds two overheads namely; memory and insert/append performance. When INDEX is created, memory is used up for storing the index and index sizes can be quite big on large transaction tables! When inserting a new entry in the table, all the indices are updated. More indices, more time. More the amount of data, bigger the indices, larger the time for updating all the indices
g. Avoid Executing an identical Select (same SELECT, same parameter) multiple times in the program
h. Avoid using join statements if adequate standard views exist.

2. Table Buffer
a. Defining a table as buffered (SE11) can help in improving the performance but this has to be used with caution. Buffering of tables leads to data being read from the buffer rather than from table. Buffer sync with table happens periodically, only if something changes. If this table is a transaction table chances are that the data is changing for particular selection criteria, therefore application tables are usually not suited for table buffering. Using table buffering in such cases is not recommended. Use Table Buffering for configuration data and sometimes for Master Data
b.  Avoid using complex Selects on buffered tables, because SAP may not be able to interpret this request, and may transmit the request to the database. The code inspector tells which commands bypass the buffer.


3. Internal Table
a. Use sorted tables when nested loops are required.
b. Use assign (field symbol) instead of into in LOOPs for table types with large work areas
c. Use READ TABLE BINARY SEARCH with large standard tables speed up the search. Be sure to sort the internal table before binary search.
d. Use transaction SE30 to check the code

4. Miscellaneous
a. PERFORM: When writing a subroutine, always provide type for all the parameters. This reduces the overhead which is present when the system determines on its own each type from the formal parameters that are passed.

Which is the better - JOINS or SELECT... FOR ALL ENTRIES?
The effect of FOR ALL ENTRIES needs to be observed first by running a test program and analyzing SQL trace. Certain options set by BASIS can cause FOR ALL ENTRIES to execute as an 'OR' condition. This means if the table being used FOR ALL ENTRIES has 3 records, SQL Trace will show 3 SQLs getting executed. In such a case using FOR ALL ENTRIES is useless. However of the SQL Trace shows 1 SQL statement it's beneficial since in this case FOR ALL ENTRIES is actually getting executed as an IN List.
JOINS are recommended to be used till 5 joins. If the JOIN is being made on fields which are key fields in both the tables, it reduced program overhead and increases performance. So, if the JOIN is between two tables where the JOINING KEYS are key fields JOIN is recommended over FOR ALL ENTRIES.
You can use for all entries to reduce the database hits, and use non-key fields.
Here is a code with join :
SELECT A~VBELN A~KUNNR A~KUNAG B~Name1
into table i_likp
FROM LIKP AS A
  INNER JOIN KNA1 AS B
    ON A~kunnr = B~KUNNR.

* For with limited data using for all entries:
* Minimize entries in I_likp by deleting duplicate kunnr.

LOOP AT i_likp INTO w_likp.
  w_likp2-KUNAG = w_likp-KUNAG.
  APPEND w_likp2 TO i_likp2.
ENDLOOP.

SORT i_likp2 BY kunnr.
DELETE ADJACENT DUPLICATES FROM i_likp2 COMPARING kunnr.
* GET DATA FROM kna1
IF NOT i_likp2[] IS INITIAL.
  SELECT kunnr name1
    INTO TABLE i_kna1
    FROM kna1
    FOR ALL ENTRIES IN i_likp2
    WHERE kunnr = i_likp2-KUNNR.
ENDIF.


Avoid use of nested loops
When a nested loop has to be used, use a condition for the inner loop.  Otherwise in the production environment it may be possible that the loop takes a lot of time and dumps.
loop at itab1.
  loop at itab2 where f1 = itab1-f1.
  ....
  endloop.
end loop.

Another option is to use READ with BINARY SEARCH for the second table.
SORT itab2 BY f1.loop at itab1.
  Read table itab2 with key f1 = itab1-f1 BINARY SEARCH.
  if sy-subrc = 0.
    idx = sy-tabix.
    loop at itab2 from idx.
      if itab2-f1 <> itab1-f1.
        exit.
      endif.
     ....
    endloop.
  endif.
endloop.

No comments:

Post a Comment