PERFORMANCE TUNING

For all entries Nested selects Select using JOINS Use the selection criteria Use the aggregated functions Select with view Select with index support Select « Into table Select with selection list Key access to multiple lines Copying internal tables Modifying a set of lines Deleting a sequence of lines Linear search vs. binary Comparison of internal tables Modify selected components Appending two internal tables Deleting a set of lines Tools available in SAP to pin-point a performance problem Optimizing the load of the database Other General Tips & Tricks for Optimization For all entries The for all entries creates a where clause, where all the entries in the driver table are combined with OR. If the number of entries in the drivertable is larger than rsdb/max_blocking_factor, several similar SQL statements are executed to limit the length of the WHERE clause. The plus y y y y Large amount of data Mixing processing and reading of data Fast internal reprocessing of data Fast

The Minus y y Difficult to program/understand Memory could be critical (use FREE or PACKAGE size)

Some steps that might make FOR ALL ENTRIES more efficient: y y y y y y Removing duplicates from the the driver table Sorting the driver table If possible, convert the data in the driver table to ranges so a BETWEEN statement is used instead of and OR statement: FOR ALL ENTRIES IN i_tab WHERE mykey >= i_tab-low and mykey <= i_tab-high.

Nested selects The plus:

SELECT MAX( MSGNR ) FROM T100 INTO C4A WHERE SPRSL = 'D' AND ARBGB = '00'. CHECK: T100-MSGNR > C4A. Use the aggregated functions C4A = '000'. C4A = T100-MSGNR. ENDSELECT. SELECT * FROM SBOOK WHERE CARRID = 'LH' AND CONNID = '0400'. SELECT * FROM T100 WHERE SPRSL = 'D' AND ARBGB = '00'. ENDSELECT. SELECT SINGLE * FROM DD01T WHERE DOMNAME = DD01L-DOMNAME AND AS4LOCAL = 'A' . CHECK: SBOOK-CARRID = 'LH' AND SBOOK-CONNID = '0400'. ENDSELECT.and understand The minus: y y y Large amount of data when mixed processing isn¶t needed Performance killer no.y y y Small amount of data Mixing processing and reading of data Easy to code . Select with view SELECT * FROM DD01L WHERE DOMNAME LIKE 'CHAR%' AND AS4LOCAL = 'A'.when the accesses are planned by the programmer In some cases the fastest Not so memory critical The minus y y Very difficult to program/understand Mixing processing and reading of data not possible Use the selection criteria SELECT * FROM SBOOK. 1 Select using JOINS The plus y y y y Very large amount of data Similar to Nested selects .

ENDLOOP. " . . SELECT * FROM T006 INTO X006. ENDSELECT. ENDSELECT SELECT DOMNAME FROM DD01L INTO DD01L-DOMNAME WHERE DOMNAME LIKE 'CHAR%' AND AS4LOCAL = 'A'. SELECT * FROM T100 WHERE SPRSL = T002-SPRAS AND ARBGB = '00' AND MSGNR = '999'. " . Select with selection list SELECT * FROM DD01L WHERE DOMNAME LIKE 'CHAR%' AND AS4LOCAL = 'A'.. LOOP AT TAB WHERE K = KVAL. ENDSELECT. ENDSELECT. SELECT * FROM DD01V WHERE DOMNAME LIKE 'CHAR%' AND DDLANGUAGE = SY-LANGU. ENDLOOP.. SELECT * FROM T002.AND AS4VERS = DD01L-AS4VERS AND DDLANGUAGE = SY-LANGU.. APPEND X006. ENDSELECT Key access to multiple lines LOOP AT TAB. ENDSELECT SELECT * FROM T006 INTO TABLE X006.. Select « Into table REFRESH X006. CHECK TAB-K = KVAL. ENDSELECT. ENDSELECT. Select with index support SELECT * FROM T100 WHERE ARBGB = '00' AND MSGNR = '999'.

LOOP AT TAB1. IF TAB-FLAG IS INITIAL. IF TAB1 <> TAB2. TAB-FLAG = 'X'. TAB_DIFFERENT = 'X'. ENDLOOP. APPEND TAB_DEST. MODIFY TAB. ELSE. DELETE TAB_DEST FROM 450 TO 550. Comparison of internal tables DESCRIBE TABLE: TAB1 LINES L1. . TAB2 LINES L2. ENDLOOP. binary READ TABLE TAB WITH KEY K = 'X'. READ TABLE TAB2 INDEX SY-TABIX. EXIT. DELETE TAB_DEST INDEX 450. TAB_DIFFERENT = SPACE.Copying internal tables REFRESH TAB_DEST. TAB_DIFFERENT = 'X'. IF L1 <> L2. ENDIF. Linear search vs. READ TABLE TAB WITH KEY K = 'X' BINARY SEARCH. MODIFY TAB TRANSPORTING FLAG WHERE FLAG IS INITIAL. Deleting a sequence of lines DO 101 TIMES. ENDLOOP. Modifying a set of lines LOOP AT TAB. ENDIF. ENDIF. TAB_DEST[] = TAB_SRC[]. IF TAB_DIFFERENT = SPACE. ENDDO. TAB-FLAG = 'X'. LOOP AT TAB_SRC INTO TAB_DEST.

DELETE TAB_DEST. ENDLOOP APPEND LINES OF TAB_SRC TO TAB_DEST. TAB-DATE = SY-DATUM. LOOP AT TAB. WA-DATE = SY-DATUM. APPEND TAB_SRC TO TAB_DEST. MODIFY TAB FROM WA TRANSPORTING DATE. Note that in some cases a stament can not be used with a buffered table. ENDIF. MODIFY TAB. IF TAB1[] = TAB2[]. ENDLOOP. ENDLOOP DELETE TAB_DEST WHERE K = KVAL. Deleting a set of lines LOOP AT TAB_DEST WHERE K = KVAL.. " . ENDLOOP. ENDIF.. Modify selected components LOOP AT TAB. Tools available in SAP to pin-point a performance problem y y y y The runtime analysis (SE30) SQL Trace (ST05) Tips and Tricks tool The performance database Optimizing the load of the database Using table buffering Using buffered tables improves the performance considerably.. Appending two internal tables LOOP AT TAB_SRC." .. These staments are: y y y y Select DISTINCT ORDER BY / GROUP BY / HAVING clause Any WHERE clasuse that contains a subquery or IS NULL expression JOIN s . so when using these staments the buffer will be bypassed.

Use SELECT A B C INTO instead.000 records). it could be better to use the ABAP SORT stament to sort the data in an internal table. CPU activity can be reduced by careful program design. Log into your productive system. like 1200-1800. The datbase server will usually be the bottleneck. rather than repeated operations that result from a y y y y y y y y y . The GET RUN TIME has problems under multiple CPUs. If you are not sorting by the primary key ( E. especially in tables that have a lot of fields. Then you can decide whether to write the data to memory or swap space. where NUM_RECS is the number of records you expect to be accessing. over 50. Go To Utilities -> Table Contents to query the table contents and see the number of records. Field-groups can be useful for multi-level sorting and displaying. if you only access the transactions for one month. may have to be written to swap space on the hard disk. use the BYPASS BUFFER addition to the SELECR clause. Use SELECT A B C INTO TABLE ITAB whenever possible. For this reason. enter the table name you want to see. Generally. try to reduce I/O first. Using this tool can help you know what is effective. TIPS & TRICKS FOR OPTIMIZATION y Use the GET RUN TIME command to help evaluate performance. Try to make the user interface such that the program gradually unfolds more information to the user. Then use a SELECT A B C INTO TABLE ITAB statement. Instead use ABAP SORT + DELETE ADJACENT DUPLICATES on an internal table. Declare your internal tables using OCCURS NUM_RECS. to delete duplciate rows. Whenever possible.. I/O operations that read/write to hard disk are always the most expensive operations. This will read all of the records into the itab in one operation. for the number of transactions inputted within that month). Use the ABAP SORT Clause Instead of ORDER BY The ORDER BY clause is executed on the database server while the ABAP SORT statement is executed on the application server. and press Display. If you have large lists. so that fields are only read if they are used.. Avoid 'SELECT *'. if some of the fields are not part of an index. they write their data to the system's paging space. so sometimes it is better to move thje sort from the datsbase server to the application server. Get a good idea of how many records you will be accessing. you should work with the systems administrator to decide the maximum amount of RAM your program should use. the data will be kept in swap space (not memory). design the program to access a relatively constant number of records (for instance. using the ORDER BY PRIMARY key statement) but are sorting by another key. so you should use it to test small pieces of your program. rather than the whole program. and from that. Memory. This is extremely useful in optimizing a program's memory allocation.g. then memory. and by using commands such as SUM (SQL) and COLLECT (ABAP/4). and use SE80 -> Dictionary Objects (press Edit).y A SELECT. then CPU activity. rather than giving a huge list of information all at once to the user. which therefore increases your I/O read/writes to disk. It's hard to know whether that optimization technique REALLY helps unless you test it out. If the number of records exceeds NUM_RECS. if not controlled. This can make a very big difference. then there probably will be a reasonable range. Avoid ther SELECT DISTINCT Statement As with the ORDER BY clause it could be better to avoid using SELECT DISTINCT.calculate how much space your lists will use. However. Note however that for very large result sets it might not be a feasible solution and you would want to let the datbase server sort it. FOR UPDATE If you wnat to explicitly bypass the bufer. Use as many table keys as possible in the WHERE part of your select statements. field-groups are only appropriate for processing large lists (e. under what kinds of conditions.g. rather than to memory (internal tables use memory).

Use these avoid wasting resources by calculating a total that has already been calculated and stored.. The ABAP/4 processor controls the execution of an event. wellstructured and powerful programming language. data-structures and data-model . Know how to use the 'collect' command. Test extensively with GET RUN TIME when using this method. For instance. CPU consumption on the database and application servers Network communication . Information on the SAP R/3 ABAP/4 Development Workbench programming tools and its features can be found on the SAP Public Web-Server. Because the ABAP/4 language incorporates many "event" keywords and these keywords need not be in any specific order in the code. you may be able to break it into chunks of relatively constant size. SAP-recommended customer-specific ABAP/4 development guidelines can be found in the SAP-documentation. Memory consumption of the database resources eg. This can be a potential bottle neck. etc. Make sure that ITAB is declared with OCCURS NUM_RECS.not critical for little data volumes. Use the SELECT SINGLE command whenever possible. Policies and procedures can also be put into place so that every SAP-customer development object is thoroughly reviewed (quality ± program correctness as well as code-efficiency) prior to promoting the object to the SAPproduction system. buffers. This page contains some general guidelines for efficient ABAP/4 Program Development that should be considered to improve the systems performance on the following areas:Physical I/O . it is wise to implement in-house ABAP/4 coding standards. If the number of records you are reading is constantly growing. becomes a bottle neck when large volumes are transferred. It can be very efficient. -------------------------------------------------------------------------------CLASSIC GOOD 4GL PROGRAMMING CODE-PRACTICES GUIDELINES Avoid dead-code Remove unnecessary code and redundant processing Spend time documenting and adopt good change control practices Spend adequate time anayzing business requirements. A well configured system always runs 'I/O-bound' .y y y y SELECT A B C INTO ITAB. ENDSELECT statement.. where NUM_RECS is the number of records you expect to access. "top-down". you can break it into quarters. Many tables contain totals fields (such as monthly expense totals). and read all records one quarter at a time. if you have to read all records from 1991 to present.data must be read from and written into I/O devices. process flows.the performance of the I/O dictates the overall performance. This will reduce I/O operations. ABAP/4 Development Code Efficiency Guidelines ABAP/4 (Advanced Business Application Programming 4GL) language is an "event-driven".

"NOT" and value range tables (INTTAB) that are used inappropriately in Open SQL statements. If there is no WHERE clause the DBMS has no chance to make optimizations. As a general rule of thumb. always specify all known conditions in the WHERE clause (if possible). The database system can also potentially make use a database index (if possible) for greater efficiency resulting in less load on the database server and considerably less load on the network traffic as well.Quality assurance is key: plan and execute a good test plan and testing methodology Experience counts -------------------------------------------------------------------------------SELECT * FROM <TABLE> CHECK: <CONDITION> ENDSELECT vs. SELECT SINGLE * If you are interested in exactly one row of a database table or view. avoid using SELECT+CHECK statements wherever possible. use the SELECT SINGLE statement instead of a SELECT * statement. . -------------------------------------------------------------------------------SELECT * FROM <TABLE> INTO <INT-TAB> APPEND <INT-TAB> ENDSELECT vs. and analyze the SQL-statement for the optimum path the database optimizer will utilize via SQL-trace when necessary. Also. Always specify your conditions in the Where-clause instead of checking them yourself with check-statements. ensure careful usage of "OR". -------------------------------------------------------------------------------SELECT * vs. it is important to use EQ (=) in the WHERE clause wherever possible. SELECT * FROM <TABLE> WHERE <CONDITION> ENDSELECT In order to keep the amount of data which is relevant to the query the hit set small. SELECT SINGLE requires one communication with the database system whereas SELECT * requires two. Also.

use a SELECT-ENDSELECT loop instead of collecting data in an internal table with SELECT . use a select list with aggregate functions instead of computing the aggregates within the program. The RDBMS is responsible for aggregated computations instead of transferring large amount of data to the application. ENDSELECT vs. Overall Network.. SELECT using aggregate function If you want to find the maximum. INTO TABLE. Select with view SELECT * FROM <VIEW> ENDSELECT . minimum. Internal table handling takes up much more space -------------------------------------------------------------------------------Nested SELECT statements: SELECT * FROM <TABLE-A> SELECT * FROM <TABLE-B> ««. ENDLOOP... ENDSELECT. vs.SELECT * FROM <TABLE> INTO TABLE <INT-TAB> It is usually faster to use the INTO TABLE version of a SELECT statement than to use APPEND statements -------------------------------------------------------------------------------SELECT . Application-server and Database load is also considerably less. ENDSELECT If you process your data only once.. LOOP AT <INT-TAB>. SELECT * FROM <TABLE> «««. sum and average value or the count of a database column. -------------------------------------------------------------------------------SELECT INTO TABLE <INT-TAB> + LOOP AT T «««« SELECT * FROM <TABLE> INTO TABLE <INT-TAB>. WHERE + CHECK vs..

· SELECT .. FORM ALL ENTRIES · Explicit cursor handling (for more information. Similarly. all interesting records from 'pers' are read into an internal table. In the lower version the new Open SQL statement FOR ALL ENTRIES is used. SELECT persnr FROM pers INTO TABLE ipers WHERE cond.. process . Prior to the call. fewer data must be transferred if another technique would be used eg. join implemented as a view in ABAP/4 Repository. use a view wherever possible instead of nested SELECT statements. The second SELECT statement results in a call looking like this (ipers containing: P01. If only certain fields are needed then only those fields should be read from the database. P02.. P03): (SELECT * FROM persproj WHERE person = 'P01') UNION (SELECT * FROM persproj WHERE person = 'P02') UNION (SELECT * FROM persproj WHERE person = 'P03') In case of large statements. ENDSELECT. the R/3's database interface divides the statement into several parts and recombines the resulting set to one. ENDSELECT..To process a join. In addition.. The inner select statement is executed several times which might be an overhead..««««« ENDSELECT. The advantage here is that the number of transfers is minimized and there is minimal restrictions due to the statement size (compare with range tables). Using nested selects is a technique with low performance. goto Transaction SE30 ± Tips & Tricks) -------------------------------------------------------------------------------Nested select: SELECT * FROM pers WHERE condition. Overall database and network load is . -------------------------------------------------------------------------------SELECT * FROM <TABLE> vs. «««.. SELECT <column(s)> FROM <TABLE> Use a select list or a view instead of SELECT *. if you are only interested in specific columns of the table. process . SELECT * FROM persproj WHERE person = pers-persnr. .. vs. the number of columns can also be restricted by using a view defined in ABAP/4 Dictionary. SELECT * FROM persproj FOR ALL ENTRIES IN ipers WHERE person = ipers-persnr «««.

use array operations instead of single-row operations to modify the database tables.considerably less. -------------------------------------------------------------------------------Single-line inserts LOOP AT <INT-TAB> INSERT INTO <TABLE> VALUES <INT-TAB> ENDLOOP vs.ENDDO loop with Field-Symbol vs. do attempt to use SAP-buffering for eimproved performance response times. Array inserts Whenever possible. This would reduce the overall Database activity and Network traffic... read-only(few updates) tables. . Frequent communication between the application program and database system produces considerable overhead. -------------------------------------------------------------------------------Single-line updates SELECT * FROM <TABLE> <COLUMN-UPDATE STATEMENT> UPDATE <TABLE> ENDSELECT vs. -------------------------------------------------------------------------------SELECT without table buffering support vs. SELECT with table buffering support For all frequently used. use column updates instead of single row updates to update your database tables -------------------------------------------------------------------------------DO. Column updates UPDATE <TABLE> SET <COLUMN-UPDATE STATEMENT> Wherever possible..

TO... and should be replaced by ABAP statements or functions STRING_CONCATENATE.. CA.. Use of SPLIT statement Use the SPLIT statement instead of programming a string split yourself -------------------------------------------------------------------------------Shifting by SY-FDPOS places vs . ---> SPLIT STRING_LENGTH. Use of the CONCATENATE statement Use the CONCATENATE statement instead of programming a string concatenation of your own -------------------------------------------------------------------------------Use of SEARCH and MOVE with offset vs. ---> strlen() STRING_CENTER...CENTERED STRING_MOVE_RIGHT ---> WRITE.... ---> WRITE.. CPU consumprion can rise substantially -------------------------------------------------------------------------------Use of a CONCATENATE function module vs. ---> CONCATENATE STRING_SPLIT. Use of a CONCATENATE statement Some function modules for string manipulation have become obsolete....RIGHT-JUSTIFIED -------------------------------------------------------------------------------Moving with offset vs.Using CA operator Use the special operators CO. . CS instead of programming the operations yourself If ABAP/4 statements are executed per character on long strings.TO..

If you want ot delete the leading spaces in a string use the ABAP/4 statements SHIFT. Other constructions (with CN and SHIFT..LEFT DELETING LEADING... when determinating a check-sum .. BY SY-FDPOS PLACES.....LEFT DELETING LEADING....) are not as fast -------------------------------------------------------------------------------Get a check-sum with field length vs Get a check-sum with strlen () Use the strlen () function to restrict the DO loop to the relevant part of the field..Using SHIFT. with CN and ASSIGN CLA+SY-FDPOS(LEN) . with CONDENSE if possible. eg.

Sign up to vote on this title
UsefulNot useful