Sunteți pe pagina 1din 8

Book: TAW 10_1B Unit: 11 Title: ABAP Open SQL: Optimizing statements.

SQL Statements: Basics Optimization of application logic through: y y y y y y Optimization o SELECT clause (columns, ) Optimization of WHERE clause Accesses to several tables (bundling) Buffering of read data in itabs: avoids identical SELECT commands (structure/content) Changing SQL accesses (mass accesses) Use of SAP table buffering

Correct use of the SELECT command is critical to optimizing the performance of application logic.

Avoid identical (or identical structure) SELECTs SELECT * FROM spflight INTO wa WHERE carrid = AA . ENDSELECT.

SELECT * FROM sflight INTO wa WHERE carrid = UA . ENDSELECT.

SELECT * FROM sflight INTO wa WHERE carrid = AA . ENDSELECT *** using buffering techniques SELECT * FROM sflight INTO TABLE itab WHERE carrid IN ( AA , UA ).

LOOP AT itab INTO wa WHERE

LOOP AT itab INTO wa WHERE

LOOP AT itab INTO wa WHERE

Using identical SELECT statements is sub-optimal, because the same data records have to be read repeatedly. In such cases, you should use buffering strategies, as the above example. In the example, part of database table SFLIGHT could be buffered in an internal table, ans subsequent accesses of the result set could be served from that internal table. BAD PRAXIS SELECT * FROM sbook INTO wa. ENDSELECT. SELECT * FROM sbook INTO wa. CHECK wa-customid = 00000022 . INSERT wa INTO TABLE itab, ENDSELECT. Use a WHERE clause whenever possible: SELECT * FROM sflight ITO wa WHERE customid = 00000022 . ENDSELECT. In the first example, an SQL statement is specified without a WHERE condition. Statement like this can have dramatic effects on the overall performance of an SAP system. SQL statements involving master data and transaction data should never be formulated without a selective WHERE condition. In the second example, the CHECK statement is used to filter data records, but after they have been transferred to the application server. This is very processing-intensive for the network, the database, and overall system performance. To reduce the number of data records, you should specify a sufficiently selective WHERE clause in each SQL statement. You have to make sure that the number of selected records does not become too large, or even remains constant over time. SELECT statements that return approximately the same amount of data over time are also called stable WHERE conditions. If the number of read data records grows quickly over time, the statement is considered to be unstable. Avoid gaps in the index (from the left side) SELECT * FROM sbook INTO wa WHERE CONNID = 0017 . ENDSELECT. 2

In this case, the index search string will be 802______0017% Favor the use of indexes SELECT * FROM sbook INTO wa WHERE carrid = AA AND connid = 0017 . ENDSELECT. In this case, the index search string will be 802AA0017% If index fields are filled with values from left to right, the database can form the result set faster, because less search effort is required. Gaps at the end of the index are less critical tan gaps at the beginning. If the index is missing important fields, the system has t oread a large number of index/table blocks, which will worsen performance significantly. In some cases, the database optimizer maye ven skip the index search, and read the entire database table sequentially instead (FULL TABLE SCAN). BAD PRAXIS SELECT * FROM sbook INTO wa WHERE . ENDSELECT. Select the required columns! SELECT carrid connid fldate bookid smoker FROM sbook INTO CORRESPONDING-FIELDS OF TABLE itab1. ENDSELECT. SELECT carrid connid fldate bookid smoker FROM sbook INTO TABLE itab2. ENDSELECT. When you use SELECT *, more data tan is necessary is often transferred from the database server to the application server, In the first example, you read all the columns in the table and pass them on to the application server, although only some of the columns are needed. The INTO CORRESPONDING FIELDS OF addition in the second example copies the field contents to target fields with the same names. This copy operation is more performance-intensive tan left-aligned copying into a suitable work rea or internal table using INTO or INTO TABLE. The same applies to APPENDING CORRESPONDING FIELDS OF and APPENDING TABLE. ARRAY-FETCH to fill tables SELECT * FROM sbook INTO TABLE itab. 3

SELECT * FROM sbook INTO wa. INSERT wa INTO TABLE itab. ENDSELECT. The same number of data records has to be transferred from the database server to the application server for both a SELECT loop and an array fetch. Because the transfer is always made in blocks of 32K each, the same transfer effort (number of fetches) is required for both variants. Still, the array fetch is preferable to the SELECT loop, because the records are transferred by record from the database interface to the ABAP program in the SELECT loop, whereas the record are passed on to the ABAP program in a block with array fetch. SELECT * FROM spflight INTO wa WHERE carrid = AA . IF sy-dbcnt > 1000, EXIT. ENDIF. COMPUTE result = wa-seatsocc ENDSELECT. Use UP TO n ROWS SELECT * FROM spflight INTO wa UP TO 1000 ROWS WHERE carrid = AA . COMPUTE result = wa-seatsocc ENDSELECT. There are two ways to read a fixed number of records: 1. Transfer the records to the application server and discard the ones you do not needed in the SELECT loop. 2. The better method is to use SELECT UP TO n ROWS and only transfer the desired number of data records. In the first example, system field SY-DBCTN is required within the SELECT loop. The last transferred 32K block may unnecessarily be transferred completely. In the second versin, if you explicity especify UP TO n ROWS, only the required entries are read and transferred. Reduce database load

SUM = 0 SELECT carrid loccurkey loccuram FROM sbook INTO TABLE itab WHERE fldate = 20051101 . 4

LOOP AT itab INTO wa. COMPUTE ENDLOOP.

Using Aggregates SELECT carrid loccurkey avg( loccuram ) INTO wa FROM sbokk WHERE fldate = 20051101 GROUP BY carrid loccurkey.

You can use aggregation functions (COUNT, SUM, MAX, MIN, AVG) to perform calculations in the database if it significantly reduces the number of data records to transfer. In the first example, an ABAP LOOP statement runs overal an internal table for aggregation. Every record that fulfills the WHERE condition has to be read from the databae first. The result is then calculated in the LOOP. In the second example, the result is calculated directly in the database, and onlye the results records are transferred. You will have to decided which one is better on a case-by-case basis. Aggregation functions always bypass SAP table buffers, a disadvantage wgen buffered tables are involved. When you use aggregation function AVG, you should use data type F for the target field. Function SUM use a data type for the target field that is longer tan the data type of the source field, to deal with overflows. Accesing Multiple Tables JOIN and VIEW are definitely the most important. JOIN and VIEW. T oread data across multiple tables, you have to crate a links between the functionally dependent tables called JOIN. You can use VIEWS in the ABAP Dictionary or ABAP joins to implement a JOIN. Remember, a record is read from the inner table for each record in the outer table. Use View sor ABAP JOINS SELECT * FROM dic_view INTO TABLE itab WHERE . SELECT sbook~carrid scustom~name INTO FROM sbook INNER JOIN scustom ON sbook~customid = scustom~id WHERE Instead 5

SELECT * FROM t1 WHERE . SELECT * FROM t2WHERE . SELECT * FROM t3WHERE ENDSELECT. ENDSELECT. ENDSELECT. Attributes and benefits of Database Views y y y y y y

You can use views in other programs too. There are where-used lists and search functions (SE84 / SE81) to find existing views quickly. Like database tables, you can buffer views. Field common to both tables (join fields) are only transferred from the database to the application server once. The views is implemented in the ABAP Dictionary as an inner joint. This means no data is transferred if the inner table does not containt any entries that correspond to the outer table. If you do not want to use an inner join to read from a text table, use an ABAP left outer join.

And INNER JOIN corresponds to the result set that only considers the records from the outer table for which suitable data records exist in the inner table. A LEFT OUTER JOIN corresponds to the result set that contain all the records from the outer table, regardless of whether or not suitable records exists in the inner table. If no suitable records exist in the inner table, the fields of the inner table are set to ZERO values in the result set. The tables involved in a JOIN are called base tables. Restrictions for Outer Join y y y y You can only have a tableo r a view to the right of the JOIN operator; you cannot have another JOIN expression. Only AND can be used as a logical operator in an ON condition. Every comparison in the ON condition must contain a field from the table on the right. None of the fields in the tableo n the right can appear in the WHERE conditions of the LEFT OUTER JOIN.

Example: ABAP INNER Join SELECT <fieldlist> INTO <target> FROM <dbtab1> [AS <alias1>] INNER JOIN <dbtab2> [AS <alias2>] ON <alias1>~<dbtab1-field1> = <alias2>~<dbtab2-field1> AND <alias1>~<dbtab1-field2> = <alias2>~<dbtab2-field2> AND WHERE ENDSELECT. REPORT zselect_view. SELECT f~carrid b~connid INTO ( ) FROM scarr AS f INNER JOIN spfli as b ON f~carrid = b~carrid WHERE 6

ENDSELECT. A disadvantage of using ABAP Joins is that the statement is more complex tan a Dictionary view. SUBSELECT and SUBQUERY SELECT carrid connid fldate seatsocc INTO (wa-carrid , wa-connid, wa-fldate, wa-seatsocc) FROM sflight WHERE seatsocc = ( SELECT MAX( seatsocc ) FROM sflight ). WRITE: wa-carrid, wa-connid, . ENDSELECT. SELECT carrid carrname INTO (wa-carrid, wa-carrname) FROM scarr WHERE carrid IN ( SELECT DISTINCT carrid FROM spfli WHERE cityfrom = FRANKFURT ). WRITE: wa-carrid, wa-carrname. ENDSELECT. A subquery es a query within a SELECT, UPDATE or DELETE statement. It is formulated in the WHERE or HAVING clause to check whether the data in various database tables or views posses certain attributes. A SELECT statement with a subquery has a more restricted sntax than a SELECT statement without a subquery. If the subquery returns exactly one value, the usual comparison operator aside from LIKE and BETWEEN can be used. If a subquery is used with comparison operator instead of with EXISTS, then the SELECT clause of the subquery can only contain a single column, which can be a fiel in the database tableo r an aggregate expression. In the second example above, the subquery es supposed to return several lines, each with one value. If you want to compare all the returned values, you have to use IN clause. Subqueries whose WHERE condition contains fields from the main query are called correlated subqueries. If subqueries are nested, each subquery can use all the fields from the higher-level subqueries in the hierarchy. Read on Demand and Buffering SELECT FROM sbook INTO wa_sbook WHERE PERFORM read_scustom .

ENDSELECT. FORM read_scustom. READ TABLE itab WITH TABLE KEY id = wa_sbook-customid INTO wa_scustom. 7

IF sy-subrc NE 0. SELECT SINGLE FROM scustom INTO (wa_scustom-id, ) WHERE id = wa_sbook-customid. IF sy-subrc EQ 0. wa_scustom-exists = X . ELSE. wa_scustom-id = wa_sbook-customid. wa_custom-exists = . ENDIF. APPEND wa_scustom TO itab. ENDIF. ENDFORM. In the above example, the table contents read from SCUSTOM are buffered in an internal table. The system checks whether the corresponding table entry has already been read. The information that no exists in the database is also buffered in the internal table. If the information will be buffered within the program and does not have to be reused, the read routine can be a subroutine. If you want to buffer in an internal sesin for multiple programs, you should use a function module. Addition FOR ALL ENTRIES (Join between itab and DB Tables)

SELECT * FROM scustom INTO FOR ALL ENTRIES IN outer_itab WHERE id = outer_itab-customid. ENDSELECT. SELECT FOR ALL ENTRIES was created in OPEN SQL at a time when it was not yet possible to perform database JOINs. If you use FOR ALL ENTRIES, you have to make sure that the driving table is not blank and does not contain any duplicate entries. If you want t oread large data volumes, only in exceptional cases should you use FOR ALL ENTRIES.

S-ar putea să vă placă și