Sunday, December 17, 2006

Performance Tuning

Performance Tuning
In the world of SAP programming, ABAP is the universal language. Often due to the race against schedule and deliveries, focus of making efficient programs is overlooked. A performance optimized ABAP program saves the time of the end user, thus increasing the productivity of the user, and in turn keeping the user and the management happy .

1. Use of selection criteria
Instead of fetching all the data and then filtering unwanted data, filter out unwanted data during selection itself.
Not Recommended:
Select * from zflight. Check : zflight-airln = ‘LF’ and zflight-fligh = ‘BW222’. Endselect.
Recommended:
Select f1 f2 f3 … fn from zflight into table itab_flight where airln = ‘LF’ and fligh = ‘222’.

2. Usage of Aggregate Functions.
Use already provided aggregate functions instead of manually coding it in ABAP.
Not Recommended
Maxnu = 0. Select * from zflight where airln = ‘LF’ and cntry = ‘IN’.
Check zflight-fligh > maxnu. Maxnu = zflight-fligh. Endselect.
Recommended
Select max( fligh ) from zflight into maxnu where airln = ‘LF’ and cntry = ‘IN’.
Similarly use MIN, AVG,COUNT,and SUM as needed.

3. Usage of Views.
Instead of using multiple select statements or joins to fetch data from multiple tables, use views.
Not recommended
Select * from zcntry where cntry like ‘IN%’. Select single * from zflight where cntry = zcntry-cntry and airln = ‘LF’.
Endselect.
Recommended
Select * from zcnfl where cntry like ‘IN%’ and airln = ‘LF’. Endselect.

4. Use INTO clause instead of appending record by record during database selection.

5. Avoid appending corresponding fields of table during database selection
Not Recommended
Refresh: itab_flight.
Select * from zflight into intab_flight.
Append intab_flight.
Clear intab_flight.
Endselect.

Recommended
Refresh: intab_flight.
Select * from zflight into table intab_flight

6. Modifying lines of internal table
Not Recommended
Loop at int_fligh.
If int_fligh-flag is initial.
Int_fligh-flag = ‘X’.
Endif.
Modify int_fligh.
Endloop.

Recommended
Int_fligh-flag = ‘X’.
Modify int_fligh transporting flag where flag is initial.

7. Read internal table with binary search
Read table makes use of sequential search which slows down the processing. Instead sort the internal table and then read with binary search.
Not Recommended
Read table int_fligh with key airln = ‘LF’.

Recommended
Read table int_fligh with key airln = ‘LF’ binary search.

8. Appending two internal tables
Instead of loop at one internal table and then append, use variation of APPEND statement.
Not Recommended
Loop at int_fligh1.
Append int_fligh1 to int_fligh2.
Endloop.

Recommended
Append lines of int_fligh1 to int_fligh2.

9. Table Buffering
Use buffered tables as far as possible however buffering is bypassed while using :
Select distinct
Select … for update
Order by, group by, having clause
Joins

10. Usage of FOR ALL ENTRIES.
Not Recommended
Loop at int_cntry.
Select single * from zfligh into int_fligh where cntry = int_cntry-cntry.
Append int_fligh.
Endloop.

Recommended
if not int_cntry[] is initial.
Select * from zfligh appending table int_fligh For all entries in int_cntry Where cntry = int_cntry-cntry.
endif.

11. Usage of Table indexes
While building the where clause take extra care to use the key fields in the order in which index is built so that it helps optimizer use the appropriate index while fetching data.

12. Usage of Joins
When multiple SAP tables are logically joined, it is always advisable to use inner join to read the data from them. This certainly reduces the load on the network.
However take precaution while volume of data is too much and too many tables are involved.

13. Usage of Nested Selects and Nested Loops
Avoid nested selects and nested loops. I.e as far as possible do not use SELECT within SELECT/ENDSELECt and LOOP inside LOOP ENDLOOP.

14. Sort functionality Vs Order by
Use ABAP SORT instead of using ORDER BY during database selection.

0 comments:

Blogger template 'YellowFlower' by Ourblogtemplates.com 2008