ABAP performance and best practices . - a Bible.

Page 1

Symbols used :

Symbol

Meaning

Very good performance(recommended).

Forbidden (never use).

Not the best method (to be used only when there is no other way).

A prerequisite (must do activity).

Additional information .

Page 2

Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved

WBEI Engagement Overview

What is it all about ?

Write a program quickly and save time ?

Write a program accurately which will be more performing in its execution time and would not break in real time?

Page 3

Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved

WBEI Engagement Overview

Write fine tuned new ABAP programs .Goals :    Explore the areas of performance tuning in ABAP . Page 4 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . Fine tune existing ABAP programs .

Selection on a single table .           General Rules. Operations on internal tables . Special topics . Page 5 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . Mass treatment – Internal table and data base . Tools available to measure performance.Performance – Areas in ABAP . Tools available to validate source code from performance and standards point of view. Data Dictionary . Commit and Rollback . Selection on many tables .

interface. function. DO‟S Maintain standard documentation headers for programs and subprograms (describing author. changes.General rules : Dos of a good programming . side-effects) Document the interfaces of subroutines and functions Indent the lines of your programs Modularize your programs Specify the type of data objects explicitly (if possible) Use speaking names for variables and fields Page 6 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview .

Page 7 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . DONT‟S – – – – – – Avoid NULL statements (statements without effect) Avoid deeply nested loops Avoid unreachable program parts Avoid side-effects on global variables Do not use a variable for multiple purposes (e.General rules : Don’ts of a good programming . as counter and as string) Avoid spaghetti code .g.

exchanges between the database server and the application server are one recording by one recording. Page 8 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . When we use the <SELECT … ENDSELECT> instruction.General rules : Data loading in Internal table When we use the <SELECT INTO TABLE wt_table> instruction. exchanges between the database server and the application server are made by packets.

Logical databases Nested select GROUP BY ORDER BY […]CORRESPONDING FIELDS OF […] DISTINCT Page 9 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview .General rules : SELECT SINGLE SELECT UP TO 1 ROWS … ENDSELECT SELECT INTO TABLE wt_table SELECT INTO TABLE wt_table … FOR ALL ENTRIES SELECT … ENDSELECT.

General rules : Queries Queries are forbidden because programs generated by a Query are not performing at all. Page 10 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . thus execution time is too high. Requests are not optimized.

ENDSELECT. SELECT SINGLE * FROM table WHERE field1 = „xxx‟. Use : field1 is the primary key or a part of the primary key. After many compared tests.Selection on a single table : Use : field1 is not the primary key. Page 11 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . SELECT * FROM table WHERE field1 = „xxx‟. it is proven that the <SELECT SINGLE> instruction seems to be more efficient that <UP TO 1 ROWS> one. Treatment. ENDSELECT. IF sy-subrc = 0. Treatment. Treatment. SELECT * FROM table UP TO 1 ROWS WHERE field1 = „xxx‟. ENDIF. EXIT.

SELECT SINGLE field1 INTO table-field1 FROM table WHERE field2 = „xxx‟. number of selected fields number of fields of the table 0. This implies that it is always necessary to test the return code after the <SELECT> instruction. Except for the particular cases: Use: Selection of a limited number of fields. Page 12 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview .8 SELECT SINGLE * FROM table WHERE field2 = „xxx‟. we do not know if we have gone into the <SELECT> instruction or not.Selection on a single table : When we are on the instruction which follows <ENDSELECT>.

The order of the fields in the <WHERE> clause has an impact on the execution time of the request. he/she must respect the following rules by ascending priority: Put the fields in the same order of the existing index (primary or secondary). The first field of the <WHERE> clause permits the pointer to position itself. Therefore the order of the tests in the <WHERE> clause is very important. SAP works with a positioning pointer on the tables. Runtime chooses the index based on the fields used in <where> clause.Selection on a single table : The <where> clause . Order of the fields in <where> clause is very important in deciding index. Page 13 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . When the developer codes an instruction in order to read in the database. Put the fields in the same order of the key . Then the table is sequentially read.

Created automatically when the table is activated . When the table is client dependent . secondary indexes have to be created explicitely . As far as possible . Primary Fields forming the primary key of the table form primary index. Every table has a primary index by default.Selection on a single table : Index (to speed up data fetch) . When the primary index cannot be used . Secondary No Index Page 14 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . primary index should be used . Fields in the <where> clause doesnot form fields from any index . Consists of fields from non primary keys. Slows related SAP transactions . the secondary index must contain MANDT as first field .

SELECT SINGLE * FROM table WHERE field = „xxxx‟. Page 15 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview .Selection on a single table : <select > statement SELECT SINGLE * INTO table FROM table WHERE field = „xxxx‟.

ENDIF. SELECT * INTO TABLE wt_table FROM table WHERE field1 = „xxxx‟. Treatment. LOOP AT wt_table. Treatment. SELECT * FROM table WHERE field1 = „xxxx‟ ORDER BY field2. IF sy-dbcnt NE 0. SORT wt_table BY field2. Sorting on the application server. ENDSELECT. Data sorting Sorting on the database server. EXIT. Page 16 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . ENDLOOP.Selection on a single table : Data sorting.

ENDIF.Selection on a single table : <into> option Treatment without storing the data into a table. SELECT SINGLE field2 INTO wv_field2 FROM table WHERE field1 = „xxxx‟ IF sy-dbcnt NE 0. ENDIF. Page 17 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . wv_field2 = table-field2. Treatment. SELECT SINGLE * INTO table FROM table WHERE field1 = „xxxx‟. IF sy-dbcnt NE 0. Treatment.

There is no <ENDSELECT> instruction because it is not a loop treatment. Page 18 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . the <APPEND> of the internal table is implicit. Furthermore. The storage of the table is carried out at one go. The updating of the table is carried out at one go. <APPENDING TABLE> option : This option permits to add further records to an internal specified table. The <APPEND> instruction is included is the <APPENDING TABLE> instruction. It is not necessary to carry out a <REFRESH> of the internal table because it is automatically made by this option. the <APPENDING TABLE> instruction must not be used. it is the same as using the <INTO TABLE> instruction. Therefore if the internal table is empty.Selection on a single table : <INTO TABLE> option : This option permits to stock many records in an internal table. If the internal table is refreshed (<REFRESH>) just before using the <APPENDING TABLE> instruction. There is no <ENDSELECT> instruction because it is not a loop treatment.

The <INTO CORRESPONDING FIELDS OF> instruction is equivalent to a <MOVE-CORRESPONDING> one between the selected columns and the stocking fields. it must have the same structure as the chosen fields. Indeed. In this case. this option is used when there is a difference between the structure consisted of the fields of the <SELECT> and the target (in the <INTO>). But it is better to put the fields of the <SELECT> instruction in the same order of the structure declaration or the internal table in order to be quicker.Selection on a single table : <[…] CORRESPONDING FIELDS OF […]> options If we want to stock the contents of the chosen fields in another field (different of the buffer). the SAP process researches one after one the fields which have the same names and then make the allocation. Page 19 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview .

SELECT SINGLE * INTO CORRESPONDING FIELDS OF ws_struct FROM table WHERE field1 = „xxxx‟. DATA: BEGIN OF ws_struct. SELECT SINGLE field1 field2 field3 INTO ws_struct FROM table WHERE field1 = „xxxx‟. field3 LIKE table-field3. field2 LIKE table-field2. field1 LIKE table-field1. field2 LIKE table-field2. Page 20 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . SELECT SINGLE * INTO CORRESPONDING FIELDS OF table FROM table WHERE key = „xxxx‟. field1 LIKE table-field1. SELECT SINGLE * FROM table WHERE key = „xxx‟. DATA : BEGIN OF ws_struct. field3 LIKE table-field3.Selection on a single table : Not recommended use of <INTO CORRESPONDING FIELDS OF…> Direct storage into a table or a structure. END OF ws_struct. END OF ws_struct. without using the fields.

field1 LIKE table-field1. APPEND wt_table. SELECT field1 field2 field3 INTO TABLE wt_table FROM table WHERE field1 = „xxxx‟. SELECT * INTO CORRESPONDING FIELDS OF TABLE wt_table FROM table WHERE field1 = „xxxx‟. field1 LIKE table-field1. field2 LIKE table-field2. DATA : BEGIN OF wt_table OCCURS 0. Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . END OF wt_table. SELECT field1 field2 field3 INTO TABLE wt_table FROM table WHERE field1 = „xxxx‟.Selection on a single table : Not recommended use of <INTO CORRESPONDING FIELDS OF…> Direct storage into a table or a structure. Page 21 DATA : BEGIN OF wt_table OCCURS 0. END OF wt_table. without using the fields. ENDSELECT. field3 LIKE table-field3. field3 LIKE table-field3. SELECT * INTO CORRESPONDING FIELDS OF wt_table FROM table WHERE field1 = „xxxx‟. field2 LIKE table-field2.

the <SELECT> instruction loops on all of the data of the selected table. Page 22 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . It avoids some dumps of the programs because of a lack of memory in the table2 table (See the following example) If we know that the internal table has duplicates. we have to copy the internal table.Selection on a single table : <FOR ALL ENTRIES IN> To use the <FOR ALL ENTRIES IN> option: The internal table must not be empty. The <WHERE> clause must consist of equalities. If the internal table is empty. sort it and delete its duplicates. The recordings must be sorted in order to delete the duplicates. The deletion of the duplicates is compulsory for two reasons: It enables to accelerate the execution of the <SELECT… FOR ALL ENTRIES> instruction since the volume of the internal table is smaller.

SORT wt_table1 BY field1. Treatment.sort the recordings . SELECT * INTO TABLE wt_table1 FROM table WHERE field1 IN s_field1.test the number of recordings . ENDIF.Selection on a single table : <For all entries> . DELETE ADJACENT DUPLICATES FROM wt_table1 COMPARING field1.delete the duplicates SELECT * INTO TABLE wt_table1 FROM table1 WHERE field1 IN s_field1. SELECT * INTO TABLE wt_table2 FROM table2 FOR ALL ENTRIES IN wt_table1 WHERE field1 = wt_table1-field1 AND field2 = „xxxx‟. IF sy-dbcnt NE 0. SELECT * INTO TABLE wt_table2 FROM table2 FOR ALL ENTRIES IN wt_table1 WHERE field1 = wt_table1-field1 AND field2 = „xxxx‟. Page 23 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . Treatment.

 Reusability .  Selection screen .Selection on many tables : Logical database (LDB) Advantages :  Directory structure of related tables. Performance : This method is forbidden because in SAP R/3 it is far from being the most performing method for reading data in the databases. Page 24 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview .

Treatment. Page 25 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . SELECT * FROM table2 WHERE table1-field1 = table2-field1 AND … . ENDSELECT. this method is not very performing from the point of view of the execution duration. Treatment.Selection on many tables : Nested select. Considering the important number of accesses to the database. Treatment. Therefore it is forbidden to use it. SELECT * FROM table1 WHERE … .

A sub-query solicits the database sever. it is not recommended. So. Page 26 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview .Selection on many tables : Sub-query SELECT * FROM table INTO TABLE wt_table WHERE field IN ( SELECT… FROM … WHERE…).

The join can be assimilated to a view but the difference is that the join is not defined in the dictionary. Treatment. Page 27 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview .Selection on many tables : Joins Principle : Instead of carrying out the selections in many tables from the database. the join permits to carry out dynamically in memory only one access to the different tables. SELECT mkpf~mblnr mkpf~mjahr mseg~zeile mseg~… INTO TABLE wt_table1 FROM mkpf INNER JOIN mseg ON mseg~mblnr = mkpf~mblnr AND mseg~mjahr = mkpf~mjahr WHERE mkpf~… = … AND mseg~… = ….

Drawback The system needs a lot of memory space because it dynamically creates a pseudo table of pointers in order to optimize the different accesses to different tables of the join. Page 28 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview .Selection on many tables : Joins Advantage The developer maintains that he makes only one access because the code can be interpreted like a <SELECT* FROM table WHERE …… Treatment ENDSELECT>. In the other cases. The inner join can be only used in the case of a 1 to n relation on the key of type header data <-> item data. This is a lot quicker than the method of nested SELECT because there are fewer accesses to the database. The performance problem comes from the fact that the code generated by SAP is interpreted by an internal layer at SAP and not by the database server itself. it is forbidden.

CLEAR wt_mkpf. SORT wt_mseg BY mblnr mjahr zeile. Treatment. SORT wt_mkpf BY mblnr mjahr. ENDIF. Treatment. * Treatment of the selected data LOOP AT wt_mseg. AT NEW mjahr. * Selection of data from the table lines SELECT * INTO TABLE wt_mseg FROM mseg FOR ALL ENTRIES IN wt_mkpf WHERE mblnr = wt_mkpf-mblnr AND mjahr = wt_mkpfmjahr AND lgort = wc_lgort. Treatment. ENDIF. READ TABLE wt_mkpf WITH KEY mblnr = wt_mseg-mblnr mjahr = wt_mseg-mjahr BINARY SEARCH. ENDLOOP. ENDAT. IF sy-dbcnt NE 0. AT END OF mjahr. Treatment. Treatment. IF sy-dbcnt NE 0.Selection on many tables : <For all entries> * Selection of data at the top of the table SELECT * INTO TABLE wt_mkpf FROM mkpf WHERE … AND …. ENDAT. Page 29 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview .

only if the internal table contains at least one record. If there are several identical records. Advantage : This reduces the number of accesses to the database not only from the data selection but also from the data processing in the body of the program. Page 30 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . Drawback : In some cases we can choose more data than necessary in reality. the result given by the clause <for all entries in> will keep only the first occurrence. It is important to test the number of records returned during the stocking in an internal table from the first transparent table and to read the second transparent table. Indeed.<For all entries> : Pros and Cons . it is compulsory to quote the complete key in the list of fields to be selected. 1. Furthermore if there are many tables to stock it will request a large quantity of memory. the access to the second table will provide ALL the records of this table. if the test is not done and if the first table is empty. 2. To avoid this major inconvenience.

Furthermore. Advantage : There are very few accesses to the database for choosing many records which are in different tables. Drawback : If we want to optimize all the programs by this method. it should be necessary to create at least as many views as that of the programs. During the first call of the view. But it takes a lot of memory space. So we must use them in priority (instead of using a inner join).Selection on many tables : Views Principle : A view is a virtual table which does not have a physical existence and which is composed of columns belonging to one or many tables. the relation between them must point on a key or on an index so that the view may be the most performing. if there are many tables in the view. the system loads all the data into a memory table corresponding to the view. Many views have been created by SAP. Page 31 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . We must avoid creating new specific views because of the memory space.

Use the views created by SAP. Forbidden. Avoid creating new specifics views. Forbidden. Test if the table is empty. sort the table and delete duplicates. Join View Sub-query Nested select Logical database 2 3 4 5 6 Page 32 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . Not recommended.Using recommendations : Type of selection on many tables Using recommendat ions (1=high. Use it when there is a 1 to n relation on the key. 6=low) 1 Commentary For all entries Always use it except in the case of a 1 to n relation.

test the return code of the correction instruction to make a rollback if the sy-subrc is not equal to zero. It is also forbidden to use the <COMMIT> instruction in loop structure. NE space By extension.Commit and Rollback: Best practices DELETE FROM y9nav WHERE zznom OR zzpavillon NE space. it is important to keep the database in a stable statement. ROLLBACK WORK. ENDIF. Page 33 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . EXIT. To do this. IF sy-subrc NE 0. It is forbidden to use the <COMMIT> instruction in the case of a program that updates tables by carrying out many treatments to do it (in User-Exit for example). after each database correction.

Is the table often modified? Table category Is the size of the table in production uncertain? Data type “APPL1” Choose a category with a size greater than those of the forecast Page 34 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview .Data dictionary: Table creation Summary of the parameters for a specific table Question Delivery class Do we wish to update the table in production only by transport? Does the modification of the table need obligatory integration tests? Table maintenance Must the table be manually modified? Is it possible to use the SM30/SM31 transaction to maintain it? Is the table of delivery class “C”? Tick “authorized SM30/SM31” box and generate the table maintenance Data type “APPL2” Specific program Yes Delivery class “C” No Delivery class “A” Data Type See the following question. Data type “APPL0” Key the corresponding size category between 0 and 4.

company. country code…)? Log data changes Is the volume of creations/modifications is small? Is it important to know who and when a table has been modified? Is it important to be able to restitute the table contents at a given date? Page 35 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved Yes It is possible to buffer the table. See the following question.Data dictionary: Table creation Summary of the parameters for a specific table Question Buffering Is the table often read by transactions? Is the table rarely modified ? Is the table use buffering? Is the table small? Is the number of readings high? Is the number of writings small? Is the table large? Are few different recordings read? Is the table always read with the same beginning of key (language code. Complete buffering No It is forbidden to buffer the table. analytical perimeter. Individual buffering Generic buffering See the following question. No buffering Log Data Changes No Log Data Changes WBEI Engagement Overview .

However. we can use it. Page 36 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . AND access by primary key (or a part of it). The particular case is the following one: transparent or pool table. in some cases and when a good optimization is important. AND access to the table by reading. AND few data updating. Buffering a table is forbidden in most of the cases.Data dictionary: Buffering . AND a lot of queries to the table.

Instead of erasing the records one by one. it is advised to stock the records for deletion (only the key fields) in an internal table with the format of the complete key (even <mandt> field) of the transparent table and afterwards to use the instruction <DELETE table FROM TABLE wt_table>. Large deletion. Page 37 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . ENDLOOP. DELETE FROM wt_table WHERE field1 = „xxx‟. LOOP AT wt_table WHERE field1 = „xxx‟. DELETE wt_table. Internal table : If we want to delete many records from an internal table satisfying a condition. Transparent table : The mass deletion is good when we want to delete records from a transparent table. it is recommended to carry out a large suppression instead of a suppression record by record.Mass treatment – Deletion . Deletion record by record.

Mass deletion SELECT * FROM table WHERE field1 = wc_const. DELETE table. ENDIF. DELETE FROM table WHERE field1 = wc_const. ENDSELECT. IF sy-subrc = 0. Page 38 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview .Mass treatment – Deletion Case 1: The comparison in the <WHERE> clause is made with a constant Deletion record by record.

Storage of the data for deletion in an internal table. ENDIF. LOOP AT wt_table.Mass treatment – Deletion .Deletion of the data of the transparent table thanks to the internal table. DELETE table. DELETE table FROM TABLE wt_table. key1 LIKE table-key1. . DELETE ADJACENT DUPLICATES FROM wt_table_tmp COMPARING field1. SELECT mandt key1 key2 … (all the keys) INTO TABLE wt_table FROM table FOR ALL ENTRIES IN wt_table_tmp WHERE field1 = wt_table_tmp-field1. SORT wt_table_tmp BY field1. SELECT * FROM table WHERE field1 = wt_table-field1. Page 39 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . . Case 2: The comparison in the <WHERE> clause is made with a variable (a field of wt_table) Deletion record by record. … (all the keys of the table) END OF wt_table. IF sy-dbcnt NE 0. ENDIF. ENDSELECT. DATA: BEGIN OF wt_table OCCURS 0. IF sy-subrc = 0. mandt LIKE table-mandt. ENDLOOP. key2 LIKE table-key2.

MODIFY wt_table TRANSPORTING field2 field3 WHERE field1 = ‘xxx’. wt_table-field3 = „bbb‟.Mass treatment – Modification. MODIFY wt_table. wt_table-field3 = „bbb‟. Correction record by record Mass correction LOOP AT wt_table WHERE field1 = „xxx‟. wt_table-field2 = „aaa‟. ENDLOOP. CLEAR wt_table. Page 40 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . wt_table-field2 = „aaa‟.

Mass treatment – Modification.
Case 1: Use of the <UPDATE> instruction Modification record by record The <UPDATE> instruction does not make any check. UPDATE table SET field1 = wc_const1 Field2 = wc_const2 WHERE field3 = wc_const.

SELECT * FROM table WHERE field3 = wc_const field1 = wc_const1. field2 = wc_const2. MODIFY table. ENSELECT.

Update 1. 2. 3. Faster. Doesnot make any check before update . Results into dump if record to be modified is not found in DB.

Modify 1. 2. 3. Slow. Makes a check before update. If record doesnot exist in DB , it will insert a record .

Page 41

Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved

WBEI Engagement Overview

Mass treatment – Modification.
Case 2: Use of the <MODIFY table FROM TABLE wt_table> instruction Correction record by record
- Storage of the data for correction in an internal table - Modifications of the data of the transparent table thanks to the internal table.

LOOP AT wt_table. table-field1 = wt_table-field4. table-field2 = wt_table-field5. MODIFY table. ENDLOOP.

LOOP AT wt_table_tmp. CLEAR wt_table. wt_table-field1 = wt_table_tmp-field4. wt_table-field2 = wt_table_tmp-field5. APPEND wt_table. ENDLOOP. DESCRIBE TABLE wt_table. IF sy-tfill NE 0. SORT wt_table BY field1. DELETE ADJACENT DUPLICATES FROM wt_table COMPARING field1. MODIFY table FROM TABLE wt_table. ENDIF.
Page 42
WBEI Engagement Overview

Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved

Mass treatment – Insert.
Internal tables

LOOP AT wt_table1. MOVE wt_table1 TO wt_table2. APPEND wt_table2. ENDLOOP.

APPEND LINES OF wt_table1 TO wt_table2.

The wt_table1 table must have the same structure as the wt_table2 table.

Performance This method of appending lines of one table to another is about 3 to 4 times faster than appending them line by line in a loop.

Page 43

Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved

WBEI Engagement Overview

wt_table-field2 = … . table-field2 = … . LOOP treatment. Page 44 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . wt_table-field1 = … . ENDLOOP. DELETE ADJACENT DUPLICATES FROM wt_table COMPARING field1. APPEND wt_table. Insertion record by record .Storage of the data for insertion in an internal table .Mass treatment – Insert. INSERT table. SORT wt_table BY field1 …. ENDLOOP. INSERT table FROM TABLE wt_table. LOOP treatment. table-field1 = … .Insertion of the data into the transparent table thanks to the internal table.

The recordings must be sorted before the reading The sort must be ascending and on the reading key. To carry out a direct reading on an internal table. the <READ TABLE wt_table WITH KEY field1 = wv_field1 BINARY SEARCH> will give a bad result. Binary search – most prefferred Sequential data research. READ TABLE wt_table WITH KEY field1 = wv_field1 BINARY SEARCH. SORT wt_table BY field1. Forbidden except in the case of a small internal table READ TABLE wt_table WITH KEY field1 = wv_field1. This instruction positions itself on the first record in the table that contains the value. Page 45 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . it is better to make a <READ TABLE wt_table WITH KEY field1 = wv_field1 BINARY SEARCH> because the data research will be performed by dichotomy and not sequentially. Otherwise. The ASCENDING sort on the reading key is compulsory.Operations on internal tables : Read table binary search To use the <READ TABLE> instruction: The <BINARY SEARCH> option is compulsory.

APPEND wt_table2. MODIFY wt_table2 INDEX sy-tabix. (or loop from index. <READ TABLE> without <BINARY SEARCH> LOOP AT wt_table1. Treatment. ENDIF. ENDLOOP. loop where.Tips to use the <read table> instruction inside a loop. ENDIF. LOOP AT wt_table1. Page 46 WBEI Engagement Overview Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved . Treatment. Treatment2. ENDLOOP. MODIFY wt_table2 INDEX sy-tabix. Sort inside a loop. SORT wt_table2 BY key. Treatment1. READ TABLE wt_table2 WITH KEY key. ELSE. ELSE. do…) Treatment. APPEND wt_table2. READ TABLE wt_table2 WITH KEY key BINARY SEARCH. IF sy-subrc = 0. Treatment1. Treatment2. do…) Treatment. IF sy-subrc = 0. loop where. (or loop from index.

Treatment1. do…) Treatment. A good solution SORT wt_table2 BY key. Treatment. Treatment2. ELSE. ENDIF. INSERT wt_table2 INDEX sy-tabix. (if wt_table2 is not empty) LOOP AT wt_table1. Page 47 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . IF sy-subrc = 0. (or loop from index. READ TABLE wt_table2 WITH KEY key BINARY SEARCH.Tips to use the <read table> instruction inside a loop. MODIFY wt_table2 INDEX sy-tabix. loop where. ENDLOOP.

At the beginning of each control break on the specified zone after the word <NEW>. Control breaks The instructions between <AT …> and <ENDAT> are executed only: AT FIRST AT LAST AT NEW AT END OF In the first pass within the <LOOP>.Working with internal tables. At the end of each control break on the specified zone after the word <END OF>. Declaration of an internal table: During the declaration of the internal table the <OCCURS> perimeter must usually be equal to 0 except if we know the total number of records of the internal table. If this amount multiplied by the record size is less than 8000. Loop : Control break statement. Page 48 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . it is advised to take the value of the total number of records. At the end of the treatment for the last record on the internal table.

Loop : Control break statement. Page 49 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . For SAP. The tables must be sorted before using <AT NEW> and <AT END OF> instructions. the <AT NEW / AT END OF> instructions are executed only if there is at least a byte which changes its value between the first byte of the buffer in the table and the last byte of the specified zone in the <AT NEW/AT END OF>.

Nested LOOP with the WHERE clause LOOP AT wt_table1. ENDLOOP. LOOP AT wt_table Treatment. ENDLOOP. Basic nested LOOP LOOP AT wt_table1. ENDLOOP.Nested loops. ENDLOOP. Treatment. Page 50 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . LOOP AT wt_table2 WHERE field = wt_table1-field.

SORT wt_table1 BY field1. field1 LIKE wt_table1_key. wv_index = 1. LOOP AT wt_table2 FROM wv_index. Treatment. field7 LIKE table7-field7. field1 LIKE wt_table1_key. field4 LIKE table4-field4. ENDIF. field5 LIKE table5-field5. ENDLOOP. DATA: BEGIN OF wt_table1 OCCURS 0. DATA: BEGIN OF wt_table2 OCCURS 0. field6 LIKE table6-field6. Page 51 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . SORT wt_table2 BY field1. Treatment. IF wt_table2-field1 > wt_table1-field1. wv_index = sy-tabix. LOOP AT wt_table1. END OF wt_table2. Must condition : Sort on itab1 and itab2 should be on same fields .Nested LOOP FROM index with a key-field (Parallel cursor). ELSE IF wt_table2-field1 = wt_table1-field1. ENDLOOP. Remember the last position read . Nested LOOP FROM index with one key field Principle : Donot read the entire second itab . Treatment. EXIT. END OF wt_table1. field8 LIKE table8-field8.

END OF wt_table1. Must condition : Sort on itab1 and itab2 should be on same fields . END OF ws_struct1. field5 LIKE … . IF ws_struct1 = ws_struct2. field2 LIKE … . field6 LIKE … . END OF wt_table2. field2 LIKE wt_table1-field4. ws_struct1-field2 = field4. SORT wt_table1 BY field2 field4 field6. ws_struct1-field1 = field2. DATA: BEGIN OF ws_struct1. LOOP AT wt_table2 FROM wv_index. Page 52 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . LOOP AT wt_table1. ws_struct2-field3 = field6. field3 LIKE wt_table2-field6. ws_struct2-field2 = field4. Use1 : ws_struct1 and ws_struct2 form the key for search and there are unique entries with this combination in itab2. wv_index = sy-tabix. Remember the last position read . ws_struct2-field1 = field2. field5 LIKE … .Nested LOOP FROM index with several key-fields(Parallel cursor). Nested LOOP FROM index with several key fields . field1 LIKE wt_table2-field2. ELSEIF ws_struct2 > ws_struct1. field1 LIKE … . DATA: BEGIN OF wt_table1. field2 LIKE wt_table2-field4. field3 LIKE wt_table1-field6. DATA: BEGIN OF ws_struct2. Principle : Donot read the entire second itab . field4 LIKE … . ENDIF. field3 LIKE … . field2 LIKE … . ENDLOOP. END OF ws_struct2. DATA: BEGIN OF wt_table2. field3 LIKE … . field4 LIKE … . wv_index = 1. ws_struct1-field3 = field6. Treatment. SORT wt_table2 BY field2 field4 field6. field1 LIKE wt_table1-field2. ENDLOOP. Exit. field6 LIKE … . field1 LIKE … .

DATA: BEGIN OF wt_table2 OCCURS 0. ws_struct1 (and ws_struct2) must be the key of the table. field1 LIKE table1-field1. wv_index = 1. Must condition : Sort on itab1 and itab2 should be on same fields . if several records can have the same value for the field of the FROM clause. field7 LIKE table7-field7. wv_index_tmp = sy-tabix. tab1_key LIKE wt_table1_key. LOOP AT wt_table1. SORT wt_table2 BY tab1_key. ENDIF. field3 LIKE table3-field3. ELSEIF wt_table2-tab1_key > wt_table1-tab1_key. Page 53 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . ENDLOOP. EXIT. Treatment. DATA: BEGIN OF wt_table1_key. IF wt_table2-tab1_key = wt_table1-tab1_key. DATA: BEGIN OF wt_table1 OCCURS 0. field6 LIKE table6-field6. field2 LIKE table2-field2. we must write: Nested LOOP FROM index with several key fields. Treatment. END OF wt_table2. END OF wt_table1_key. wv_index = wv_index_tmp. LOOP AT wt_table2 FROM wv_index. temporary variable. field8 LIKE table8-field8. Treatment.Nested LOOP FROM index with several key fields. ENDAT. SORT wt_table1 BY tab1_key. field4 LIKE table4-field4. But. The index is here memorized in a ENDLOOP. AT END OF tab1_key. END OF wt_table1. tab1_key LIKE wt_table1_key. The variable index is only updated when all the records having the same value for the field tab1-tab1_key have been processed. Use2 : Refering to last slide . field5 LIKE table5-field5.

Use: wt_table1 is not sorted by field1. Treatment. ENDIF. LOOP AT wt_table1. EXIT. Treatment. ELSE. ENDLOOP. IF wt_table2-field = wt_table1-field1. IF sy-subrc = 0. LOOP AT wt_table2 FROM sy-tabix. Treatment. ENDIF. SORT wt_table2 BY field. ENDLOOP. READ TABLE wt_table2 WITH KEY field = wt_table1-field1 BINARY SEARCH TRANSPORTING NO FIELDS.Nested LOOP with READ TABLE BINARY SEARCH: Nested LOOP FROM … READ TABLE BINARY SEARCH . Page 54 WBEI Engagement Overview Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved .

So it is not recommended to use this type of table. the <LOOP WHERE> instruction is optimized in the same way as the <READ BINARY SEARCH> method. insertions are very costly because the new records must be correctly positioned so that the table remains sorted. It is necessary to use the instruction <INSERT TABLE wt_table2> to keep the table sorted and not to have a duplicate key. In this case.Nested LOOP with a table of <sorted> Type (only up from 4.0 Release) This method is similar to the previous one. Append table can lead to short dump. Performance Deletions and modifications on a table of type <SORTED> are good in term of performance (as a <READ TABLE BINARY SEARCH>). the coding is easier to read and SAP generates an error if the table is not correctly sorted. Page 55 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . It is better to use a standard table with a <READ TABLE BINARY SEARCH> statement for example. The wt_table2 table being defined as sorted by field1 and field2 fields. But.

This instruction is more costly than a classical <LOOP … MODIFY … ENDLOOP> because it requires a time to maintain the table.6 release it is possible to use the <LOOP AT itab ASSIGNING <fs> instruction. LOOP AT wt_table1 ASSIGNING <fs_tab1>. This solution is good to use only if lots of data must be updated in the table.6 Release) Up from the 4. field1 LIKE table1-field1.LOOP AT wt_table ASSIGNING <fs> (only up from 4. DATA: BEGIN OF wt_table1 OCCURS 0.6 release. It is therefore possible to directly modify a record without copying it in the header line and update it afterwards. <fs_tab1>-field2 = value2. Treatment. END OF wt_table1. The READ instruction has a similar coding: READ TABLE wt_table … ASSIGNING <fs>. field3 LIKE table3-field3. Page 56 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . In this case. ENDLOOP. FIELD-SYMBOLS: <fs_tab1> LIKE wt_table1. <fs_tab1>-field1 = value1. field4 LIKE table4-field4. Up from the 4. the field symbol directly points to the line of the table. field2 LIKE table2-field2.

The execution time is exponential to the number of records processed. 7=low) 1 Commentary Nested LOOP FROM index with one key field The execution time is proportional to the number of records processed. 5 The table is defined as a sorted table. Use: one field composes the common key of the tables. Not recommended. Use: A table cannot be sorted. Forbidden. Forbidden. Use: several fields compose the common key of the two tables.Comparative study of performance : Type of loop Performance (1=high. WBEI Engagement Overview 6 7 Page 57 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved . Nested LOOP with several key fields Nested LOOP with READ TABLE BINARY SEARCH LOOP … AT NEW … READ TABLE BINARY SEARCH Nested LOOP WHERE with SORTED TABLE Nested LOOP … WHERE Basic Nested LOOP 2 3 4 Use: no common fields to sort the tables.

LOOP AT wt_table. Page 58 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . Therefore the conversion is important. FORM add1 USING param TYPE i. ENDLOOP. ADD 1 TO param. ADD 1 TO param. The <ANY> type is not a real type. easier to maintain. Treatment. ENDFORM. with appropriate names easier to understand. it is advised to standardize the different parameters as often as possible in order to avoid the conversions that are very costly in time for the SAP processor. The <FORM> instruction permits to add on parameters during the function call. FORM add1 USING param. DATA: wv_var TYPE i. PERFORM add1 USING wv_var. A parameter is a variable. PERFORM add1 USING wv_var. DATA: wv_var TYPE i.Form/ Perform: The use of <FORM> enables to organize the program to make it more readable. ENDLOOP. LOOP AT wt_table. if a <FORM> is used and has parameters. Treatment. ENDFORM. That is why.

-> In the calling program handle all the exceptions . -> Define sufficient exceptions in function modules and raise them as needed. Page 59 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview .Exception in function modules: Mechanism to handle error situations and discontinue further processing. still handle the others exception . ->Exceptions when raised . ->If no exceptions are available in function module . if not handled in calling program result in short dump .

Hashed tables. 2. Aggregate functions. PERFORM. 6. 5. Buffering. 4. 3. Parallel processing. Indexing.Special topics: 1. Page 60 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview .

Therefore hashed tables are very useful for voluminous tables which are often accessed for reading. But the time for reading a hashed table is less than or equal to the time required for a standard table. It is impossible to read/ insert/ modify a record in a hashed table by using an index.Hashed tables: Principle : The access to a hashed table is only made by a unique key thanks to a hashed algorithm. Page 61 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . Sorting a hashed table is impossible. The reading of a hashed table is carried out by the <READ TABLE wt_table WITH TABLE KEY>. otherwise there is a not lot of uses. Advantage : The time required to read a hashed table is independent of the number of read records. A hashed table is quick in reading. The key must be complete when we do a research. Example DATA wt_table TYPE HASHED TABLE OF ws_struct WITH UNIQUE KEY key [INITIAL SIZE n] [WITH HEADER LINE] The cost of a <SELECT INTO TABLE> is the same with a standard internal table or a hashed table.

we can use it. However. AND a lot of queries to the table. -> Table should not appear in a view if buffering is allowed.Buffering : Buffering : Buffering a table is forbidden in most of the cases. in some cases and when a good optimization is important. The particular case is the following one: pool table. -> Should not be buffered if secondary index exists on the table. AND access to the table by reading. Page 62 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . AND access by primary key (or a part of it). Transparent tables are prone to frequent changes and master data tables are huge in size to be buffered. -> Usually customizing tables are buffered. AND few data updating.

Indexing & Perform: Indexing : In most of the cases. the creation of an index is not recommended because we must be very careful. it is advised to avoid using <PERFORM>. PERFORM : The use of <PERFORM> is very important for the program structure. if there are big performance problems. An index can be created when there is no more solution. Page 63 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . Therefore. But it is costly. In general PERFORM increases readability of source code and is good to use. Each creation must be studied individually.

FM should be RFC enabled. Messages received back from task could be processed via a subroutine in calling program. Server must have atleast 3 dialog work processes. Dispatcher queue less than 10% full. Will cause problem if there is a dependency between the data which is processed in two different dialog tasks. you should not use SUBMIT or CALL TRANSACTION in such a report after using CALL FUNCTION STARTING NEW TASK. As the processing happens in a dialog work process which has a auto logout time limitation. So the entire data processing can be split into different dialog tasks . at least one dialog work process free for processing tasks from the parallel job. Faster processing compared to serial processing as it uses asynchronous processing . The calling program should not change to a new internal session after making an asynchronous RFC call. That is. Concept Advantages Disadvantages Prerequisites Page 64 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview .Parallel processing : Call function STARTING NEW TASK DESTINATION IN GROUP allocates the processing to a new Dialog task.

functioncall1 = done. endform. constants: done(1) type c value 'X'. *Receive remaining asynchronous replies wait until functioncall1 = done.Parallel processing : Example data: functioncall1(1) type c. write:/ cstgdetail1. call function 'BAPI_CUSTOMER_GETDETAIL2' starting new task 'FUNC1' destination 'NONE' performing set_function1_done on end of task exporting customerno = p_kunnr. receive results from function 'BAPI_CUSTOMER_GETDETAIL2' importing customergeneraldetail = cstgdetail1. start-of-selection. cstgdetail1 type bapicustomer_kna1. Page 65 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . **************************************************** **** * FORM FUNCTION1_DONE **************************************************** **** form set_function1_done using taskname.

COUNT . MIN . Reduces coding effort . Disadvantage To be avoided when data records that are worked upon in aggregate functions are big in number. Reduces performance as aggregation is done at database level. AVG . Can be used in aggregate functions involving small number of data records . So no further treatment is necessary on ABAP layer.Aggregate functions: SUM . Page 66 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview . Aggregation is done on Database level. MAX Advantage Data fetched from database to ABAP layer is less .

Tools available to validate source code: SLIN Extended syntax check : Tcode – SLIN Does advanced syntax check and lists out all potential coding errors that can lead to runtime breakage of program Page 67 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview .

Tools available to validate source code: SLIN Page 68 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview .

Naming conventions used for objects can be checked against standards. You can create your own project specific variants or can use a temporary definition .Tools available to validate source code: SCII Code Inspector : Tcode – SCII Code inspector lists potential flaws in the program that can affect the performance of the program. Page 69 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview .

Tools available to validate source code: SCII Page 70 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview .

Tools available to measure performance : Run time analysis : SE30 (ABAP V/S Database) Page 71 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview .

Page 72 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview .Run time analysis : SE30 ( Costly database statements ).

Page 73 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview .SQL trace tool : ST05 .

SQL trace tool : ST05 Page 74 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview .

SQL trace tool : ST05 Page 75 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview .

SQL trace tool : ST05 Page 76 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview .

The End. . Page 77 Proprietary and confidential © Copyright Capgemini 2007 All Rights Reserved WBEI Engagement Overview .ABAP performance and best practices .

Sign up to vote on this title
UsefulNot useful