You are on page 1of 9

ABAP Best Practices for Business

Intelligence

Applies to:
SAP BI Netweaver 2004s. For more information, visit the Business Intelligence homepage.

Summary
This tutorial will give you a brief understanding of the best practices that should be followed while using
ABAP for BI taking into account the performance

Author: Mansi Dandavate


Company: Deloitte
Created on: 22 May 2009

Author Bio
Mansi Dandavate is a Netweaver 2004s Certified Consultant with 2.5 years of experience in SAP
BW/BI

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com


© 2009 SAP AG 1
ABAP Best Practices for Business Intelligence

Table of Contents
How to decide whether to use Start Routines, End Routines or Individual Update Routines ............................3
Declaring an Internal Table.............................................................................................................................3
Deleting a Record ...........................................................................................................................................3
Database Access using Select .......................................................................................................................4
Example: ......................................................................................................................................................................4
Send Messages to Monitor .............................................................................................................................5
Value Comparison...........................................................................................................................................5
Modifying a Record .........................................................................................................................................5
Example: ......................................................................................................................................................................5
Example: ......................................................................................................................................................................6
Copying Internal Tables......................................................................................................................................6
Read data from an Internal Table ...................................................................................................................6
Example : .....................................................................................................................................................................6
Using Nested Loop..........................................................................................................................................7
Comment your Code .......................................................................................................................................7
Related Content..................................................................................................................................................8
Disclaimer and Liability Notice............................................................................................................................9

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com


© 2009 SAP AG 2
ABAP Best Practices for Business Intelligence

Overview
ABAP for a BI Consultant is usually limited to implementing the logic and making it work out.
Hardly a BI Consultant is concerned about the performance while implementing the Business Logic in Start
Routines.
The consequences of these are faced by the Support Team who doesn’t understand why the Data load is
taking so much time in Production.
So for a Successful BI Implementation it is mandatory that we also consider the performance aspect while
writing ABAP Logic. Performance does matter when it comes to loading lakhs of records in the Production
System.
This document gives you an overview of the best practices which should be followed while writing an ABAP
Code considering the performance.

How to decide whether to use Start Routines, End Routines or Individual Update
Routines
ƒ Start Routines are preferred when any transformation is needed in the data at package Level
ƒ When you need to calculate the value of any field based on any other field which is read from some
other ODS or table, Start Routines can be used.
ƒ Individual Routines can be used to read data from the global Internal Table which is populated in the
start routines or to implement any simple formula or a calculation
ƒ End Routines are Target Specific. Any fields which you wish to populate which are not present in the
souce but present in the target, can be filled using End Routines. End routine alone can replace start
routines as well as individual routines in some cases.

Declaring an Internal Table


TYPES : Begin of fs_XX,
F1(4) type c,
F2(2) type n,
F3(2) type c,
End of fs_xx.
DATA : it_XX type standard/sorted/hashed table of fs_XX,
Wa_XX like line of it_XX.

Also if the internal table is not been used in the Individual routines then declare it in the local part of your
start routine and remember to clear it at the end of the routine with the below statement.
Refresh it_xx.
free it_xx.

This frees the memory allocated for the internal table.

Deleting a Record
Use the following statement.
Delete source_package where F1 = ‘X’.

Loop at source_package assigning <source_fields>.


If <source_fields>-F1 = ‘X’.
Delete source_package.
Not
Endif. Preferred
Endloop.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com


© 2009 SAP AG 3
ABAP Best Practices for Business Intelligence

Sometimes you might come across a requirement when you want to delete records for multiple values.
Eg. I would like to delete all records from source_package where F1 = X, and Y.
In that case you can use the SELECT-OPTIONS type.

DATA : TEMP type F1. “i.e. the field for which we are creating SELECT-OPTIONS

SELECT-OPTIONS : s_F1 for temp.

DATA : wa like line of s_F1.

wa-sign = 'I'.
wa-option = 'EQ'.
wa-low = 'ABCD'.
append wa to s_F1.

wa-sign = 'I'.
wa-option = 'EQ'.
wa-low = 'EFGH'.
append wa to s_F1.

Delete source_package where F1 in s_f1.

Range tables provide an easier method to either add or remove values we want.

Database Access using Select


Instead of using Select single always select the entire data of the database table into an internal table.
Remember, lesser the access to the database, more is the performance.
Also select only records which are relevant, avoid selecting the entire data of the tables.
In Real Time, there may be some database tables which will contain millions of records. So lesser the
records we select, less is the size of the internal table and thus better the performance.

Example:
DATA : nb type n.
Describe table it_XX lines nb.
If nb is not initial.
Select F1 F2 F3 from YYY into it_XX for all entries in source_package
Where F1 = source_package-F1.
Endif.

Remember to use “FOR ALL ENTRIES IN” as used above, this will select only those records which are
relevant for selection.
Imagine there is a delta load going on and source_package contains only 10 records, however the database
table YYY contains 12400 records.
So not using “FOR ALL ENTRIES IN” would select the entire 12400 records, although we require only the 10
which are relevant, thus affecting the performance badly.
Also if you are using the “FOR ALL ENTRIES IN” then do remember to check if the internal table is filled, if it
is not then it will select all the records from the internal table.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com


© 2009 SAP AG 4
ABAP Best Practices for Business Intelligence

Loop at source_package.
Select single F1 F2 F3 into wa_xx from YYY Severly affects
where F1 = source_package-F1. Performance
Endloop.

Also while selecting data from active table of an ODS, always base the selections on its key fields. If not
remember to create secondary indexes.

Send Messages to Monitor


Whenever any unexpected conditions occur you can pass messages to the MONITOR to notify about it.
MONITOR-msgid = 'YBW'.
MONITOR-msgty = 'W'.
MONITOR-msgno = '000'.
MONITOR-msgv1 = source_package-doc_number.
MONITOR-msgv2 = source_package-s_ord_item.
APPEND MONITOR.

Use the transaction SE91 to create a Message Class. Define your message there with a message number.
The message type will be Warning.
You can pass any relevant information like Document Number and Item

Value Comparison
Always use “IS INITIAL” statement instead of is equal to ‘ ‘
Because null for a character is ‘ ‘ but for a integer is ‘0’ and also if you use initial you don’t need to handle the
length as well.
Example:
If <source_fields>-F1 is initial.
Endif.

Modifying a Record
Modify a record of an internal table consumes time, because it copies the entire record into a separate work
area. So instead use Field Symbols. It gives about 10 times better performance

Example:
FIELD-SYMBOLS: <SOURCE_FIELDS> TYPE _ty_s_SC_1.
Loop at source_package assigning <source_fields>.
<source_fields>-F1 = ‘X’.
* Since Field Symbol is a pointer the above statement automatically changes the
*value of the record. So no need to modify.
Endloop.

Loop at source_package into wa_xx.


Wa_xx-F1 = ‘X’. Severly affects
Modify source_package.
Endloop.
Performance

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com


© 2009 SAP AG 5
ABAP Best Practices for Business Intelligence

If you want to use the modify statement use it with index.

Example:
Loop at source_package into wa_xx.
L_tabix = sy-tabix.
Wa_xx-F1 = ‘X’.
Modify source_package from wa_xx index l_tabix.
Endloop.

Copying Internal Tables


If you want to copy data from one internal table to another( where the two internal tables are exactly same)
then it can be done using a simple statement as follows,
It1[] = it2[].

Even the following method can be used to do the same thing, but performance wise the above statement is
anytime better.
Loop at it1 into wa1.
Clear wa2. Execution takes
Move-corresponding wa1 to wa2.
Append wa2 to it2.
more time
Endloop.

Read data from an Internal Table


While reading an internal table it is a good practice to sort it before with the fields which are used to read a
record.
This helps to make a read access on internal table faster.
Whenever you make a read access to a global internal table in the Individual Routines, make sure that you
do it using RECNO (Record Number).
The reason is if you make a read access using with Key then it might give wrong output in case of loading
deltas, since Before Image and After Images have the same keys.

Example :
TYPES : BEGIN OF datapack1.
INCLUDE STRUCTURE XXX.
TYPES : recno TYPE sy-tabix,
F1 type c,
F2 type n,
F3 type c,
END OF datapack1.
DATA : it_datapack1 TYPE STANDARD TABLE OF datapack1,
wa_datapack1 LIKE LINE OF it_datapack1.

In the individual routines:


Read table it_datapack1 into wa_datapack1 with key RECNO = record_no.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com


© 2009 SAP AG 6
ABAP Best Practices for Business Intelligence

Using Nested Loop


Nested Loop leads to bad performance. So as far as possible we should try to avoid it.
But in many situations if the requirement is very complex it not possible to avoid this.
In such cases, you can use nested loop, but with the concept of parallel cursor.
Example : You want to use the following code:
LOOP AT itab1 INTO wa1.
LOOP AT itab2 INTO wa2 WHERE f1 = wa1-f1
AND f3 = wa1-f3.
ENDLOOP.
ENDLOOP.

So to enhance the performance of the above code, you can write it as follows :
SORT itab2 BY f1 f3.
LOOP AT itab1 INTO wa1.
READ TABLE itab2 WITH KEY f1 = wa1-f1
f3 = wa1-f3 BINARY SEARCH TRANSPORTING NO FIELDS.
IF sy-subrc EQ 0.
v_tabix = sy-tabix.
LOOP AT itab2 INTO wa2 FROM v_tabix.
IF wa2-f1 NE wa1-f1 or wa2-f3 NE wa1-f3. EXIT ENDIF.
*processing or records here
ENDLOOP.
ENDIF.
ENDLOOP.

Sort the internal table 2 with the fields that will be used in the where clause.
Also instead of looping at itab2 directly and checking the value, we first read the index and then start looping
from that index.
As the result the above code will be faster since the number of loop parses will be reduced.

Comment your Code


Last but not the least always develop a habit of commenting your code.
It might be possible that someone else would be maintaining the code written by you.
Comments would make the code more readable for even a layman who doesn’t have knowledge of coding.
Sometimes even though you might have written the code and you have a look at it after quite a long time, it
becomes difficult to remember, why was the code written that way.
So in such cases also comments are useful.
Mention your name, Date of Coding and the requirement for the logic.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com


© 2009 SAP AG 7
ABAP Best Practices for Business Intelligence

Related Content
http://help.sap.com/saphelp_nw04s/helpdata/en/44/bd9b5be97c112ce10000000a11466f/content.htm
https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/e73bfc19-0e01-0010-23bc-ef0ad53f2fab
https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/docs/library/uuid/fc61e12d-0a01-0010-2883-
e2fc63ef729b
For more information, visit the Business Intelligence homepage.
For more information, visit the ABAP homepage.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com


© 2009 SAP AG 8
ABAP Best Practices for Business Intelligence

Disclaimer and Liability Notice


This document may discuss sample coding or other information that does not include SAP official interfaces and therefore is not
supported by SAP. Changes made based on this information are not supported and can be overwritten during an upgrade.
SAP will not be held liable for any damages caused by using or misusing the information, code or methods suggested in this document,
and anyone using these methods does so at his/her own risk.
SAP offers no guarantees and assumes no responsibility or liability of any type with respect to the content of this technical article or
code sample, including any liability resulting from incompatibility between the content within this document and the materials and
services offered by SAP. You agree that you will not hold, or seek to hold, SAP responsible or liable with respect to the content of this
document.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com


© 2009 SAP AG 9

You might also like