Efficient ABAP 4 Programming Technique:---OBJECTIVE In this chapter we are going to learn efficient coding Techniques in ABAP/4.
The following topics are covered.
Processing Database tables
Processing internal tables
String Manipulation 1 Processing Database Tables 1.1 Reading through a set of related tables When reading through a set of related tables, use a logical database whenever possible. The combination of defining a logical database (a program that reads a table and its dependant table entries) and using the logical database-related event keywords (e.g. GET) is more efficient than a set of nested SELECT ... ENDSELECT statements. 1.2 Checking SELECT-OPTIONS when reading via a Logical Database 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 -opt ion (s) or CHECK condition). 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: SELECT-OPTIONS: S_CNTRY FOR KNA1-LAND1, S_COY FOR KNB1-BUKRS. ... GET KNA1. CHECK S_CNTRY.
.. . ( full key). ENDSELECT version of the SELECT statement.. rather than: SELECT db_field1 db_field2 db_field3 FROM dbtab WHERE «. use: SELECT SINGLE * FROM dbtab WHERE «. GET KNB1...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. CHECK S_COY is more efficient than: SELECT-OPTIONS: S_CNTRY FOR KNA1-LAND1. SELECT * SELECT SINGLE * is more efficient than SELECT * « ENDSELECT.. . . ENDSELECT. S_COY. specifying values for as many of the table¶s key fields in a WHERE clause will make the SELECT statement more efficient than checking values after the select.. CHECK SELECT-OPTIONS. use the LIKE option as part of the WHERE clause. Whenever the full key of a table is known.3 SELECT SINGLE vs.
1.. Whenever the full key is not known you will need to use the SELECT * . For example:
. (or CHECK: S_CNTRY. (full key). S_COY FOR KNB1-BUKRS. UP TO 1 ROW can be used to retrieve one record when the full key is not known. In this case.GET KNB1.)
Therefore. 1.. use internal tables when you have to read a header . code as follows: SELECT VBELN AUART KUNNR FROM VBAK INTO (VBAK-VBELN. .. . ENDSELECT. order type and customer from the sales document table. CHECK T001G-TXTKO+2(4) = µPERS¶.5 SELECT <field1>. Also. Example: if you only need the fields order number.7 SELECT INTO TABLE Reading an internal table is faster than reading a database table. See the editor help for all the variants of the INTO clause.. it is much more efficient to only retrieve exactly those fields from the database than to select all of them (SELECT * «). ENDSELECT. This is much faster than issuing a SELECT without the UP TO clause and then checking for the system variable SY-DBCNT.. <field2> « If you only need a few fields of a table.item structure for which you would otherwise use nested SELECTs Instead of processing a
.. 1. VBAK-AUART.SELECT * FROM T001G WHERE BUKRS EQ µUS01¶ AND TXTKO LIKE µ__PERS%¶. is more efficient than: SELECT * FROM T001G WHERE BUKRS EQ µUS01¶. VBAK-KUNNR) WHERE « WRITE: / « ENDSELECT. use internal tables to store information that must be accessed multiple times throughout a program.6 SELECT « UP TO n ROWS If you only need a certain number of records specify this in your selectstatement: SELECT « FROM « UP TO 10 ROWS.. 1.
1. that means: fill the internal table via a SELECT statement and then sort via the SORT statement instead of coding a SELECT « ORDER BY. customizing settings). However.
1.9 Aggregate Functions Instead of using ABAP. These are: SUM. « ENDLOOP.g.10 SELECT without WHERE Coding a SELECT statement without a WHERE condition is only allowed for very small tables (e. whereas the sorting within the ABAP program µonly¶ affects the application server. For all other tables this is not permitted as performance problems will occur within a short period of time. if an index exists on the table that can be used for the sorting then the SELECT « ORDER BY doesn¶t cause any undue strains on the system. AVG.8 SELECT FROM « ORDER BY In most cases it is preferable to do the sorting within the ABAP program instead of on the database server. Example: SELECT MATNR SUM( KWMENG ) MEINS FROM VBAP INTO TABLE T_VBAP WHERE « GROUP BY MATNR MEINS This example will select the cumulative sales quantities grouped by material number and quantity unit.
. some calculations can be done by using aggregate functions for the SELECT. MIN and MAX.SELECT « INTO <itab-fields> APPEND ITAB ENDSELECT statement it is far more efficient to select the fields straight into the internal table and process the data from the internal table: SELECT « FROM « INTO TABLE ITAB WHERE « LOOP AT ITAB. The sorting of large amounts of data on the database server affects the performance of all users on the system.
2. Rollback segments are kept in memory and missing COMMITs can lead to overflows of the rollback segment areas. UPDATE ZVBAK. define your record selection in the UPDATE statement. ENDSELECT. UPDATE ZVBAK SET VKBUR = W_VKBUR WHERE VBELN IN S_VBELN.11 UPDATE Instead of updating records within a SELECT « ENDSELECT construct e.13 COMMIT ABAP reports that issue INSERT.12 DELETE The same consideration as for the UPDATE is true for the DELETE: Instead of deleting records within a SELECT « ENDSELECT construct e. Missing COMMITs can lead to bottlenecks on the database side because the system has to keep track of the table changes via rollback segments (in order to enable a rollback of all changes since the last commit).1. UPDATE or DELETE commands have to issue COMMIT WORK statements after a logical unit of work is completed. DELETE FROM ZVBAK WHERE VBELN IN S_VBELN.g. ZVBAK-VKBUR = W_VKBUR.SELECT * FROM ZVBAK WHERE VBELN IN S_VBELN. 1. ENDSELECT. Processing Internal Tables 2. define your record selection in the DELETE statement.1 Filling
.SELECT * FROM ZVBAK WHERE VBELN IN S_VBELN. DELETE ZVBAK.
1.g. Also the database system holds locks on the changed records that are not released until COMMIT time.
2. APPEND itab. . MOVE dbtab TO itab. The first method performs a binary search to find a matching record. Note that the performance of a LOOP AT . To fill an internal table without creating duplicate entries and add up the Packed. compare_field = <value to compare>.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. END OF itab. ENDLOOP. rather than: READ TABLE itab WITH KEY key The second method performs a sequential read from the first record until if finds a match.. use a WHERE clause and specify a condition for selection.. use: READ TABLE itab WITH KEY key BINARY SEARCH. WHERE statement is improved greatly if all fields being compared in the WHERE clause are of the same data type. but the table must be sorted first. you should try defining the µcompare¶ fields as follows: DATA: compare_ fie ld LIKE itab-field1.
.. LOOP AT itab WHERE fie ld1 =comp are_field. ENDSELECT.
2. use: COLLECT itab . and Floating Point fields at the same time. but are only interested in certain entries. rather than: SELECT * FROM dbtab.. BEGIN OF itab OCCURS 100.. Integer. ENDLOOP method. field2(5).2 Sequential Reads If you are reading through an internal table sequentially using the LOOP at itab .3 Direct Reads If you are not processing the entire internal table. field1(5).. Therefore.
If you only want to verify the existence of a record but don¶t need any of the fields from the record then use the addition TRANSPORTING NO FIELDS If you only need a few fields from the internal table for processing then use the addition TRANSPORTING <field1> <field2> « 2. identify the fields to be sorted. SORT itab without fields specified attempts to sort the table by all fields other than Packed.4 Sorting Wherever possible.2. 2. you can access the table via the READ TABLE T_VBAK WITH KEY VBELN = W_VBELN BINARY SEARCH statement. Integer. use: DESCRIBE TABLE itab LINES field. and Floating Point fields. where field is a work field of type µI¶ (integer).7 Moving data from internal table 1 to internal table 2 If you need to move all entries from one internal table to another one which has the same structure you can simply do it via the following statement: ITAB2 = ITAB1. The format: SORT itab BY field1 field2. W_COUNT = W_COUNT + 1.6 Reading large internal tables If you have to retrieve selected records from a large internal table. 2. In this way. ENDLOOP. keep this table sorted.
.5 Counting Entries
To count up the number of entries in an internal table. rather than: LOOP AT itab. is more efficient than: SORT itab.
* Record found « ENDIF. 2. DELETE ADJACENT DUPLICATES FROM T_VBAK [COMPARING field1 field2 «]
2.2.10 DELETING DATA FROM INTERNAL TABLES If you need to delete a subset of records from an internal table use the following: DELETE T_VBAK WHERE « 2. ENDLOOP.8 Appending data from internal table 1 to internal table 2 If you need to append records from one internal table to another one which has the same structure you can simply do it via the following statement: APPEND LINES OF ITAB1 TO ITAB2.9 Looping at internal tables If you are looping at an internal table just to count the number of records that fulfill certain criteria then use the following variant of the loop statement: LOOP AT T_VBAK TRANSPORTING NO FIELDS WHERE « ADD 1 TO COUNTER. ENDLOOP. If there is no comparing condition the table should be sorted by all fields. The same applies if you only want to verify that at least one record exists that satisfies a certain condition: LOOP AT T_VBAK TRANSPORTING NO FIELDS WHERE « EXIT. IF SY-SUBRC = 0.11 DELETING DUPLICATE ENTRIES FROM AN INTERNAL TABLE To delete duplicate entries from an internal table the table has to be sorted by the fields used in the comparing condition.
4 Testing for Initial Value The use of the IF statement for checking whether a field is empty (i.e.3 ASSIGN statement If assigning the contants of a field belonging to a Dictionary defined table/data structure which itself contains a name of a field to a field symbol. Is more efficient than: ASSIGN (KNA1-NAME1) TO <FS>. 3. Use: MOVE structure1 TO structure2. For example: IF MARA-IDNRA = SPACE. For example: ASSIGN TABLE FIELD (KNA1-NAME1) TO <fs>.2 Determining the length of a string. In contrast to the second method above. 3.1 Moving data from one table work area/structure to another one with an identical structure.
. This is because the search for the field (in this case KNA1-NAME1) is carried out only in the Data Dictionary and not in the symbol table. ENDIF. the performance does not depend on the number of fields used within the program.3 Miscellaneous 3. equal to the initial value for its data type) can be made more efficient by comparing the field with another of the same data type. 3. Use: fieldlength= STRLEN( field). This improves the performance of this statement considerably. rather than: IF fie ld CP µ#¶. fieldlength= SY-FDPOS. rather than: MOVE-CORRESPONDING structure 1 TO structure2. use the ASSIGN TABLE FIELD syntax of the ASSIGN statement. The field must then be a component field of a database table declared with the TABLES statement.
3. .. . .. ENDIF.. always test values in order of the likelihood of each value occurring ... But the improved readability of the program code associated with the CASE statement dictates that its use should be applied for levels of three or greater. WHENvalue2.. then µA¶...
The first (IF«. A value of µB¶ is the most likely value to occur... you can use: IF field = value1. . is more efficient than: IF MARA-IDNRA IS INITIAL. WHENvalue3..5 Testing one field for multiple values When testing an individual field for multiple values... field x can have values µA¶.ENDIF) method is more efficient when checking a field for up to about five values....
.. ELSEIF f ield = value2. To optimize a CASE statement for fie ld x... WHENvalue1. ENDCASE. WHEN value n. ENDIF.. For example.. . 3. WHEN µB¶. followed by µC¶...6 Optimizing IF and CASE structures To optimize IF and CASE structures. code the CASE statement as follows: CASE field x. or: CASE field. or µC¶. .. ..
..8 Hard-coding Text (messages. WHEN µC¶. etc...³Most likely value . ENDFORM.. ENDCASE. or to be written to a report should be created as Text Symbols. For SELECT ..9 Checking Return Code The return code should always be checked after any database table read/update statements. . ³Next most likely value . functions. the return code should be checked after the ENDSELECT statement to check for the success of the SELECT statement. rather than: PERFORM SUB1..7 Subroutine / function performance Because of the added overhead of calling subroutines.
3.) DON T ! Wherever possible.. all text to be passed to messages... FORM SUB1. 3. 3. This allows text to be more easily maintained and supports the maintenance of these texts in other languages. WHEN µA¶.. ³Least likely value .. report output. you should avoid the following style of coding: Use: IF field NE 0. IFfield NE 0. .. PERFORM SUB1..... FORM SUB1.. ENDSELECT processing loops.. etc.. ENDFORM.
use the CONCATENATE statement. Also..1 Special operators Use the special operators CO. ABAP statements per character can cause high CPU consumption. instead of programming the operation yourself.For LOOP AT .2 CONCATENATE Instead of programming a string concatenation of your own. 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.. use the ABAP statement SHIFT « LEFT DELETING LEADING « Avoid in any case using SHIFT inside a WHILE loop. 4.
CONCLUSION Using these techniques we can optimize our code and increase the performance of our program effectively.3 Deleting leading spaces If you want to delete the leading spaces in a string. 4 String manipulations String manipulations 4. Especially on long strings. the return code should be checked after the ENDLOOP statement to check for the success of the LOOP statement.
. ENDLOOP processing loops. CA etc. 4.
7. 5. SELECT SINGLE * is more efficient than________________________ Whenever the full key of a table is known. DESCRIBE TABLE itab LINES field is used to____________________. 2. COMMIT WORK
. 8. to count up the number of entries in an internal table 5. 3. BINARY SEARCH 4. SELECT * « ENDSELECT 2.____________ system variable used to check the return code. Internal table 3. If you are not processing the entire internal table use_____________ 4. ABAP reports that issue INSERT.
SOLUTION 1.EXERCISE 1. Reading an ____________ table is faster than reading a database table._______________ allows text to be more easily maintained and supports the maintenance of these texts in other languages. TEXT SYMBOLS 6. SY-SUBRC 7. DELETE ADJACENT DUPLICATES 8. UPDATE or DELETE commands have to issue _____________ statement. To delete duplicate entries from an internal table__________ statement is used. 6.