- Select Where vs. Select + Check
Always specify your conditions in where clause rather than first selecting all the records and then using the check syntax.
- Select with index support
Try and access a table using all its indexes. Consider creating a secondary index for a table (transparent only) which is frequently accessed for read-only operations. If there is no index a full table scan is carried out while reading. The secondary index puts an extra load onto the system whenever the data is changed or new records are created, as the index is also a table and has to be updated. Hence, create a secondary index only if must.
- Select single vs. Select-Endselect
If you are interested in just one database record than use select single rather than select….where
- Select ... Into Table t
Selecting into table is much faster than select…appending the table .
- Select aggregates
If you want to find the maximum, minimum, average and sum from the table, use SQL aggregrate functions.
- Select-Endselect vs. Array-Sele
If you want to process your data only once while selecting, use select into table and loop…endloop rather than select…endselect.
- Select with view
To process a join use a view compared to a nested select statement.
- Select with select list
Use a select list (fields to be selected) or a view compared to select * if you only want to process a few fields.
- Select with buffer support
For all frequently used read-only tables, try to use SAP buffering. The table access is automatically buffered unless the following syntax is used:
b)Select single for update or
c)Select single…bypassing buffer
e)Using Native SQL
- Select count v/s select single
For checking whether a database record exists, use select single instead of select count.
- Array Insert VS Single-row Insert
Whenever possible use array operations instead of single row.
INSERT CUSTOMERS FROM TABLE TAB.
Loop at tab.
INSERT INTO CUSTOMERS VALUES TAB.
- Column Update
Whenever possible use column updates compared to single-row updates.
UPDATE SFLIGHT SET SEATSOCC = SEATSOCC - 1.
SELECT * FROM SFLIGHT.
SFLIGHT-SEATSOCC = SFLIGHT-SEATSOCC –1.
- Select single * for update v/s Enqueue
Avoid locking database records with statement select…..update. A better solution is to use enqueue.
- Accessing cluster/pool tables
When accessing cluster/pool tables ensure that access is ALWAYS by primary key. The arrangement of data in the pooled tables is such that all the non-key data is stored as a single field. And a select based on the non-key field is very slow.
- Select header data first
Whenever accessing the item details, first get all the header data and then access the details table giving as many keys as possible using the header data. This is useful in most case like BKPF-BSEG, VBRK-VBRP, LIKP-LIPS, etc. Instead of directly accessing the details table, and accessing via the header table increases the performance. Help of logical database (SE36) hierarchical structure can be used to determine which is the header table. A table higher up in the hierarchy should be accessed before a lower one.
2. String manipulation
- Special operators in IF (CA, ...)
Use the special string operators CA, CO, CS, Concatenate, Split instead of programming the logic yourself as the standard functionality is optimized.
- Deleting leading spaces
If you want to delete the leading spaces in a string use the ABAP statement shift….left deleting leading. Other logic like using sy-fdpos is not that fast.
- String length
Use the strlen() function to restrict the DO loop passes.
- Initializing strings
Use “clear f with val” whenever you want to initialize a field with a value different from the field’s type specific initial value.
3. Internal Tables
Building condensed tables
If the amount of data is small, the READ/INSERT approach isn’t bad, but for large amounts of data(> 1000), the collect string is much faster but do not use collect with any other table filling statements like append insert as collect cannot use its hash algorithm.
Building tables without duplicates
Use Delete adjacent duplicate entries after sorting for large amount of data.
Linear vs. binary search
Binary search for an internal table is much faster than just Read.
Different forms of key access
If possible, specify the key fields for read access explicitly. Otherwise the key fields have to be computed dynamically by the run time system.
If you want to access an internal table with different keys repeatedly use your own secondary indicies. With a secondary index, you can replace a linear search with a binary search plus an index access.
READ TABLE TAB_INDEX WITH KEY DATE = SY-DATUM BINARY SEARCH.
IF SY-SUBRC = 0.
READ TABLE TAB INDEX TAB_INDEX-INDX.
READ TABLE TAB WITH KEY DATE = SY-DATUM.
IF SY-SUBRC = 0.
Key access to multiple lines
Loop….where is faster than loop/check because loop…where evaluates the specified condition internally.
Using explicit work areas
Avoid unnecessary moves by using the explicit work area operations. Append wa to tab.
Copying internal tables
To copy the entire contents of one table into another, use taba() = tabb() instead of append tabb.
Comparing internal tables
Internal tables can be compared in a much faster way by if taba() = tabb().
Sorting internal tables
The more restrictively you specify the sort key, the faster the program will run. i.e. sort tab by k.
Use the parallel cursor approach for joining or nesting two internal tables.
I2 = 1.
LOOP AT TAB1.
LOOP AT TAB2 FROM I2.
IF TAB2-K <> TAB1-K.
I2 = SY-TABIX.
LOOP AT TAB!.
LOOP AT TAB2 WHERE K = TAB1-K.
Modifying selected components
With the modify variant, Modify itab…transporting f1 f2 accelerates the task of updating the internal table.
Modifying a set of lines
For updating a set of lines use Modify itab…transporting f1 f2 with where….clause.
Appending a table
With the append variant append lines of tab1 to tab2 the task of appending can be transferred to the kernel which is faster.
Inserting a table
With the insert variant insert lines of tab1 into tab2 index idx the task of inserting can be transferred to the kernel which is faster.
Deleting a sequence of lines
With the delete variant delete tab1 from idx1 to idx2 the task of deleting can be transferred to the kernel which is faster.
Deleting a set of lines
Use delete where to delete a set of lines.
4. Internal tables vs. Field group
When there is large amount of data, go for the field group. When there is a requirement of sorting the extracted data, as the number of records increase, the internal table sorting becomes slower. When there is a case of sorting large amount of data, always use field group.
Typed vs. untyped Parameters/field symbols
If you specify the type of formal parameters/field symbols in your source code, ABAP can optimize your code more thoroughly. In addition the risk of using the wrong sequence of parameters in a perform statement is much less.
6. If, Case, ....
If vs. Case
CASE statements are clearer and a little faster than IF-constructions.
While vs. Do
While is easier to understand and faster to execute.
Case vs. Perform i Of ...
A very fast way to call a certain routine using a given index is the perform I of …statement. e.g
(I1 = 5 in this test)
PERFORM I1 OF
WHEN 1. PERFORM PV1.
WHEN 5. PERFORM PV5.
7. Field Conversion
Field Types I and P
Use fields of type I for typical integral variables like indices.
Literals Type C and Type I
Use numeric literals when you are dealing with type-I variables like sy-subrc instead of character strings.
Constants Type F
Use properly typed constants instead of literals e.g.
CONSTANTS: PI TYPE F VALUE '3.1415926535897932'.
DATA: FLOAT TYPE F.
FLOAT = '3.1415926535897932'.Arithmetic
Don’t mix types unless absolutely necessary.
- determine need to perform subroutine before you call it
- take into account initilization time for local variables
- use LOCAL rather than TABLES statement in subroutines
- use of STATICS rather than LOCAL variables
- access is slightly more expensive than internal subroutines since you are loading another program.
a)Static Analysis (Extended program check)
PERFORM / FORM
- Checks that the called FORM exists
- Checks that the number of actual and formal parameters matches
- Checks that the parameter categories (USING, CHANGING, TABLES) match
- Checks types
- Checks whether a literal is called in a structured parameter or in the category CHANGING
- Checks whether a FORM is called
- Lists untyped FORM parameters
ABAP/4 Run-time analysis
–Tools-> ABAP/4 workbench ->Test->Runtime analysis (tranx SE30)
- Hit lists
- number of conversions
- Flow trace
- table access
- Hints and Tips (RSHOWTIM)
• SQL (database) trace
–Tools -> ABAP/4 workbench -> Test -> SQL trace (tranx ST05)
–to check database calls of programs and reports
•l ook for where you make unnecessary or repeated access to tables
–EXPLAIN SQL feature
• to analyze inefficient database accesses
• DB indices used
• Access sequenced with joins
–only one user at a time to create a trace data file
10. Getting the data / Data table identification
a) F1-F9 / Technical info
This is the procedure used most often. The data when displayed in any dialog transaction, F1 can be pressed after putting the cursor on that field to display the help for it. Then technical info can give the details regarding the field.
- The table name and field name can directly give which table the data is coming from.
- If the matchcode is attached, then details of the matchcode can give more information to the data table.
- If the above two methods do not give the data table, then see where used for the data element. A list of tables and structures will be displayed. From the description of the table, you can find out which table contains the data required.
b) Runtime analysis
Use runtime analysis (SE30) and execute the transaction or the report. Usually the display transactions are used. Go only to the screen where the data you want to find exist and come out of the transaction. If you want to find which tables are updated by a create transaction, then execute the entire transaction for document posting. After executing the transaction and coming back to runtime analysis, analyze the result. All the tables, read or written by the transaction are listed under the Tables heading. From the table description, try to identify the table which may contain the required data. You can choose a particular table for which all the access made are displayed. From there you can go to the actual source code where the access was done by selecting an entry and pressing button source text.
This method can be used to find out which tables are being used, but it is one of the slowest method and requires lots of patience. You can try putting break points for messages, select, call function to speed up the process.
Looking at the data declaration for the program can help sometimes.
e) Sql Trace
You can switch on the SQL trace and find out if all the tables are accessed by a particular transaction, but I think runtime analysis is much better as it also gives the list of all the tables read and written. In this method the amount of information available is too large and the actual table can be lost in that.
f) Logical database
Look at the logical database (SE36) for the required area. Most of the main tables are part of one of the logical database. So depending on the area, pick appropriate logical database, and look at its structure. The tables are displayed in the hierarchical structure. From the table description the table can be identified.
g) Function modules
Many times the function modules exist for accessing the data. Instead of reading up the table, you can directly use these function modules. If same kind of information is read in various transactions, there is a strong possibility that a function module exist for reading that information. The runtime analysis or the debug can give more information. You can also search for the function module based on some keyword. For example, if you were searching for a function module for pricing, search the function module giving *pricing*. If you do not find the function module which is actually required but a related one, then find out the function group of that function module, and check out all the function modules in that function group. The function group can also be identified using the matchcode and all the function modules in that group can be listed.
The function module many times does not have any documentation. Put a break point in the main code of the function module and execute the transaction. If the function is getting called, then the system will go in the debug mode at that point. You can see all the data that is passed to that function module at that point, and see if you can pass that data from your own program.
11. List of important tables
|SKAT||G/L account text|
|TCURX||Decimal places currency|
BKPF - Accounting document header
BSEG - Line items of the accounting document
VBKPF - Parked documents
PAYR - Check register
BSET - Tax related
BSEC - One time customer
REGUH - Automatic check printing data proposal
REGUP - Processed items
EBEN - Purchase requisition
EKKO - Purchase document header
EKPO - Purchase document item
EKET - Purchase delivery schedules
EKBE - History of purchasing document
EKBZ - History of purchasing document – Delivery costs
MARA - Material master
MAKT - Material texts
STKO - BOM header
STPO - BOM item
STPU - BOM subitems
STXH - Text object field header
VBUK (for SO)
VBUK (for SO)
VBFA - Sales document flow
KONP - Item conditions
KONV - Conditions data
CDHDR - Change document header
CDPOS - Change document item (when checking for changed values, the field VALUE_NEW and VALUE_OLD contains character values left justified and numeric values right justified. These fields are 254 character long, hence, the numeric value cannot be seen in SE16 or in debug mode. To see the values, in debug mode use VALUE_NEW+220).
List of main logical database
CMC - Material BOM
DDF - Customer database
KOF - Vendor database
SDF - G/L account database
E*M - Purchasing document
VAV - Sales order
VLV - Deliveries
VFV - Billing document
12. List of function modules
READ_TEXT - read the long text
SAPGUI_PROGRESS_INDICATOR - progress indicator
WS_* - function modules for presentation server operations
WS_FILENAME_GET - use for file name selection, mask= ‘,*.*,*.*;.’
POPUP* - for various popups
POPUP_TO_CONFIRM_STEPS for user- confirmation
POPUP_TO_DECIDE - user decision
POPUP_GET_VALUES - to get multiple inputs from user in a popup box
13. Formulas for pricing in MM, SD, and FI
You can write the pricing formulas, like the condition value, condition base value, scale value, in the transaction VOFM. All the copy condition formulas, billing split formulas are also listed under this transaction.