Professional Documents
Culture Documents
24efficient ABAP Coding PDF
24efficient ABAP Coding PDF
When using the GET event keyword to retrieve records via a logical
database, selection can be restricted by using the CHECK statement
(using either CHECK select-option(s) or CHECK condition).
Efficient ABAP/4 Coding Techniques
In this case, a CHECK statement that returns a negative result terminates
the processing of GET events for subordinate database tables and the
associated data is not read. For efficiency reasons, you should therefore
always perform the CHECK at the highest possible level of the database
hierarchy.
For example:
UP TO 1 ROW can be used to retrieve one record when the full key is
not known.
Whenever the full key is not known you will need to use the SELECT *
... ENDSELECT version of the SELECT statement.
In this case, specifying values for as many of the tables key fields in a
WHERE clause will make the SELECT statement more efficient than
checking values after the select.
Efficient ABAP/4 Coding Techniques
1.4 Selecting via non-key fields or non-key partial fields
When selecting records from a database table when only part of a field
(on which selection is based) is known, use the LIKE option as part of
the WHERE clause.
For example:
SELECT * FROM T001G
WHERE BUKRS EQ US01
AND TXTKO LIKE __PERS%.
....
ENDSELECT.
is more efficient than:
SELECT * FROM T001G
WHERE BUKRS EQ US01.
CHECK T001G-TXTKO+2(4) = PERS.
....
ENDSELECT.
Efficient ABAP/4 Coding Techniques
1.5 SELECT <field1>, <field2>
If you only need a few fields of a table, it is much more efficient to only
retrieve exactly those fields from the database than to select all of them
(SELECT * ).
Example: if you only need the fields order number, order type and
customer from the sales document table, code as follows:
See the editor help for all the variants of the INTO clause.
Efficient ABAP/4 Coding Techniques
1.6 SELECT UP TO n ROWS
If you only need a certain number of records specify this in your select-
statement:
Example:
SELECT MATNR SUM( KWMENG ) MEINS FROM VBAP INTO TABLE
T_VBAP WHERE GROUP BY MATNR MEINS
1.13 COMMIT
2.1 Filling
To load an internal table from a database table where the structure of the
internal table is at least as wide as the structure of the database table
use:
SELECT * FROM dbtab INTO TABLE itab.
rather than:
SELECT * FROM dbtab.
MOVE dbtab TO itab.
APPEND itab.
ENDSELECT.
To fill an internal table without creating duplicate entries and add up the
Packed, Integer, and Floating Point fields at the same time, use:
COLLECT itab.
Efficient ABAP/4 Coding Techniques
2.2 Sequential Reads
If you are reading through an internal table sequentially using the LOOP
at itab ... ENDLOOP method, but are only interested in certain entries,
use a WHERE clause and specify a condition for selection. Note that the
performance of a LOOP AT ... WHERE statement is improved greatly if all
fields being compared in the WHERE clause are of the same data type.
Therefore, you should try defining the compare fields as follows:
The second method performs a sequential read from the first record until
if finds a match.
The first method performs a binary search to find a matching record, but
the table must be sorted first.
Efficient ABAP/4 Coding Techniques
2.4 Sorting
Wherever possible, identify the fields to be sorted. The format:
SORT itab BY field1 field2.
SORT itab.
SORT itab without fields specified attempts to sort the table by all fields
other than Packed, Integer, and Floating Point fields.
The same applies if you only want to verify that at least one record exists
that satisfies a certain condition:
IF SY-SUBRC = 0.
* Record found
ENDIF.
Efficient ABAP/4 Coding Techniques
2.10 DELETING DATA FROM INTERNAL TABLES
If you need to delete a subset of records from an internal table use the
following:
IF field CP #.
ENDIF.
fieldlength = SY-FDPOS.
Efficient ABAP/4 Coding Techniques
3.3 ASSIGN statement
For example:
ASSIGN TABLE FIELD (KNA1-NAME1) TO <fs>.
is more efficient than:
FORM SUB1.
....
ENDFORM.
rather than:
PERFORM SUB1.
FORM SUB1.
IF field NE 0.
....
ENDIF.
ENDFORM.
Efficient ABAP/4 Coding Techniques
3.8 Hard-coding Text (messages, report output, etc..)
For LOOP AT ... ENDLOOP processing loops, the return code should be
checked after the ENDLOOP statement to check for the success of the
LOOP statement.
Efficient ABAP/4 Coding Techniques
4 String manipulations
4.1 Special operators
Use the special operators CO, CA etc. instead of programming the
operation yourself. Especially on long strings, ABAP statements per
character can cause high CPU consumption.
4.2 CONCATENATE
Instead of programming a string concatenation of your own, use the
CONCATENATE statement.
Also, make use of the SPLIT statement or the STRLEN function whenever
you need to split a string into several components or have to determine
the length of a string.
Using these techniques we can optimize our code and increase the
performance of our program effectively.
Efficient ABAP/4 Coding Techniques
EXERCISE