Presentation is loading. Please wait.

Presentation is loading. Please wait.

 / 1 Optimize Database Access From ABAP QL.  / 2 Copyright Please note: No part of this Training Session may be reproduced or transmitted in any form.

Similar presentations


Presentation on theme: " / 1 Optimize Database Access From ABAP QL.  / 2 Copyright Please note: No part of this Training Session may be reproduced or transmitted in any form."— Presentation transcript:

1  / 1 Optimize Database Access From ABAP QL

2  / 2 Copyright Please note: No part of this Training Session may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or information storage and retrieval systems, for any purpose without the express written permission of SAP America. © SAP America, 2000. All rights reserved. SAP Virtual Classroom™ is a registered trademark of SAP America.

3  / 3 Course Objectives At the conclusion of this course, you will know: Five Rules for Writing an ABAP Program with Efficient Database Access. Performance Check List.

4  / 4 SQL Performance of Business Transactions R/3 and DBMS Architecture Efficient Database Programming in ABAP ABAP Open SQL Overview

5  / 5 General rule: The performance of a business transaction is primarily determined by its DB accesses. DB Application GUI Performance of Business Transactions

6  / 6 SQL Performance of Business Transactions R/3 and DBMS Architecture Efficient Database Programming in ABAP ABAP Open SQL Overview

7  / 7 R/3 Architecture Central DB (stores all data and application programs) Application server Communication from / to user Data transfer between database and application server Presentation server (workstation) Database server DBMS processes DB cache... Local data WP

8  / 8 Rule-based Cost-based Execution plan The Optimizer

9  / 9 SQL Performance of Business Transactions R/3 and DBMS Architecture Efficient Database Programming in ABAP ABAP Open SQL Overview

10  / 10 ABAP SQL Open SQLNative SQL DB interface Embedded SQL SQL Database

11  / 11 SELECT field1 field2 field3 field4 FROM ( table1 INNER JOIN table2 ON table1~field1 = table2~field1 ) WHERE... AND field1 IN ('A','B','C') AND field3 LIKE 'T%' SELECT clause WHERE clause FROM clause Search area Data to be transferred Relevant data - the hit list One or more tables Data to be searched - only limited by index access Solution Set of a SQL Command

12  / 12 SQL Performance of Business Transactions R/3 and DBMS Architecture Efficient Database Programming in ABAP ABAP Open SQL Overview

13  / 13 5 Rules Should be as independent of the DBMS as possible Rules for Open SQL Programming

14  / 14 Rule 1: Keep the hit list small Keep the Hit List Small

15  / 15 Use a WHERE clause wherever possible SELECT * FROM sflight INTO wa. CHECK wa-fldate(4) = ´1998´. WRITE: / wa-carrid, wa-connid,... ENDSELECT. SELECT * FROM sflight INTO wa WHERE fldate LIKE ´1998%´. WRITE: / wa-carrid, wa-connid,... ENDSELECT. Use instead Performance SELECT *... CHECK. ENDSELECT. 300,000 msec (for 400 recs.) advantage:SELECT... WHERE. 3,700 msec Keep the Hit List Small

16  / 16 Effects of Rule 1

17  / 17 Rule 2: Keep the set of data to be transferred between the database and the application small Keep the Transferred Dataset Small

18  / 18 Keep the Transferred Dataset Small SELECT * FROM sbook INTO wa WHERE carrid NE 'BA'. WRITE: / wa-customid, wa-class. ENDSELECT. SELECT customid class FROM sbook INTO (wa-customid, wa-class) WHERE carrid NE 'BA'. WRITE: / wa-customid, wa-class. ENDSELECT. Performance SELECT column.... ENDSELECT. 2.2 sec (for 21500 records) advantage:SELECT *... ENDSELECT. 6.7 sec Field list vs. SELECT * Use instead

19  / 19 Keep the Transferred Dataset Small SELECT carrid connid FROM sbook INTO (wa-carrid, wa-connid) WHERE carrid = 'LH'. IF sy-dbcnt > 10. EXIT. ENDIF. WRITE: / wa-carrid, wa-connid ENDSELECT. SELECT carrid connid FROM sbook UP TO 10 ROWS INTO (wa-carrid, wa-connid) WHERE carrid = 'LH'. WRITE: / wa-carrid, wa-connid,... ENDSELECT. Performance SELECT column... EXIT. ENDSELECT 300,000 msec advantage:SELECT column... UP TO 10 ROWS. 34,000 msec Use data selectively Use instead

20  / 20 SELECT * FROM SBOOK REPORT XYZ Data transfer from DB server to appl. server 10000 SBOOK records each with 97 bytes required 30 transfers of 32K packages from the DB server to the appl. server Report result Appl. server Data- base server Appl. server Keep the Transferred Dataset Small Data transfer

21  / 21 SELECT column1.. FROM SBOOK REPORT XYZ 10.000 SBOOK records each with 9 bytes required 3 transfers of 32K packages from the DB server to the appl. server Data transfer of DB server to appl. server Report result Data- base server Appl. server Appl. server Keep the Transferred Dataset Small Data transfer

22  / 22 Referencing of table fields Instead of this, use: SELECT * FROM sflight INTO wa WHERE carrid ='LH'. wa-seatsocc = wa-seatsocc + 1. UPDATE sflight FROM wa. ENDSELECT. UPDATE sflight SET seatsocc = seatsocc + 1 WHERE carrid = 'LH'. Performance advantage: SELECT *... ENDSELECT. 500.000 ms (for 100 records) UPDATE... SET... 56.700 ms Keep the Transferred Dataset Small

23  / 23 Use aggregate functions sum = 0. SELECT loccuram FROM sbook INTO wa-loccuram WHERE fldate LIKE '1998%'. sum = sum + wa-loccuram. ENDSELECT. WRITE: / sum. Instead of this, use: SELECT sum( loccuram ) FROM sbook INTO sum WHERE fldate LIKE '1998%'. WRITE: / sum. Performance advantage: SELECT... sum = sum +... ENDSELECT. 3.6 sec (with 21,500 records) SELECT sum ( price ) INTO 1.1 sec Keep the Transferred Dataset Small

24  / 24 Databases can calculate roundings in a different manner to the ABAP runtime system. Databases can recognize the NULL value, ABAP cannot. AVG (1, 3, 0, 0) = 1 AVG (1, 3, NULL, NULL) = 2 Select the right data type for the target field. For AVG use data type 'F' (Floating Point). For SUM use a data type which is large enough to incorporate the total so as to avoid an overflow of figures. Aggregate functions – proceed with care Keep the Transferred Dataset Small

25  / 25 The "Having" clause SELECT carrid connid fldate MAX( luggweight ) INTO (carrid, connid, fldate, max) FROM sbook GROUP BY carrid connid fldate HAVING MAX( luggweight ) > 20. WRITE: / carrid, connid, fldate, max. ENDSELECT. SELECT carrid connid fldate MAX( luggweight ) INTO (carrid, connid, fldate, max) FROM sbook GROUP BY carrid connid fldate. CHECK max > 20. WRITE: / carrid, connid, fldate, max. ENDSELECT. Keep the Transferred Dataset Small

26  / 26 Effects of Rule 2

27  / 27 Rule 3: Keep the number of roundtrips between the database and application small Keep the Number of Roundtrips Small

28  / 28 LOOP AT itab. INSERT INTO dbtab VALUES itab. ENDLOOP. INSERT dbtab FROM TABLE itab. Instead of this, use: * If double lines can appear: INSERT dbtab FROM TABLE itab ACCEPTING DUPLICATE KEYS. IF sy-subrc = 4.... Error handling... ENDIF. Array operations – for example, with INSERT Keep the Number of Roundtrips Small

29  / 29 JOINs implemented as views in the ABAP Dictionary JOINs in ABAP Open SQL SELECT... FOR ALL ENTRIES SELECT * FROM t1 WHERE... SELECT * FROM t2 WHERE... SELECT * FROM t3 WHERE... SELECT * FROM t4 WHERE... SELECT * FROM t5 WHERE...... ENDSELECT. SELECT * FROM t6 WHERE... SELECT * FROM t7 WHERE... SELECT * FROM t8 WHERE...... ENDSELECT. Avoid multi-way SELECTs Keep the Number of Roundtrips Small

30  / 30 View in the ABAP Dictionary SELECT * FROM sflight. SELECT * FROM sbook WHERE carrid = sflight-carrid AND connid = sflight-connid AND fldate = sflight-fldate. IF SY_SUBRC NE 0. WRITE: sbook-carrid, sbook-bookid,... ENDIF. ENDSELECT. SELECT * FROM sflightsbookview. * View im ABAP-Dictionary WRITE: / sflightsbookview-price, sflightsbookview-paymentsum,... ENDSELECT. Instead of this, use: Keep the Number of Roundtrips Small

31  / 31 SELECT f~carrid f~connid b~bookid INTO (carrid, connid, bookid) FROM sflight AS f INNER JOIN sbook AS b ON f~carrid = b~carrid AND f~connid = b~connid AND f~fldate = b~fldate. WRITE: / carrid, connid, bookid. ENDSELECT. INNER JOIN in the FROM clause SELECT * FROM sflight INTO wa_sflight. SELECT * FROM sbook INTO wa_sbook WHERE carrid = wa_sflight-carrid AND connid = wa_sflight-connid AND fldate = wa_sflight-fldate. WRITE: / wa_sflight-carrid, wa_sflight-connid, wa_sbook-bookid. ENDSELECT. Instead of this, use: Keep the Number of Roundtrips Small

32  / 32 Inner Join AA LH LH QF... SFLIGHT: CARRID CONNID... DISTANCE SBOOK: CARRID CONNID... BOOKID Join operator AA0017 2.572 AA 0017 1 AA0017 2.572 AA 0017 2 Join result table on the database CARRID CONNID... DISTANCE CARRID CONNID... BOOKID QF0598 1.689 QF 0598 10 0017 0400 0402 0598... 2.572 6.658 3.162 1.689... AA AA AA QF... 0017 0017 0018 0598... 1 2 17 10... Please note: The resulting quantity does not contain any entries for the airline company LH. Keep the Number of Roundtrips Small

33  / 33 SELECT f~carrid f~connid f~fldate b~bookid INTO (carrid, connid, fldate, bookid) FROM sflight AS f LEFT OUTER sbook AS b ON f~carrid = b~carrid AND f~connid = b~connid AND f~fldate = b~fldate. WRITE: / carrid, connid, fldate, bookid. ENDSELECT. SELECT * FROM sflight INTO wa_sflight. SELECT * FROM sbook INTO wa_sbook WHERE carrid = wa_sflight-carrid AND connid = wa_sflight-connid AND fldate = wa_sflight-fldate. WRITE: / wa_sflight-carrid, wa_sflight-connid, wa_sbook-bookid. ENDSELECT. IF sy-dbcnt = 0. CLEAR wa_sbook-bookid. WRITE: / wa_sflight-carrid,... wa_sbook-bookid. ENDIF. ENDSELECT. Instead of this, use: Keep the Number of Roundtrips Small LEFT OUTER JOIN in the FROM clause

34  / 34 AA LH LH QF... SFLIGHT: CARRID CONNID... DISTANCE SBOOK: CARRID CONNID... BOOKID Join operator AA0017... 2.572 AA 0017... 1 AA0017... 2.572 AA 0017... 2 LH0400... 6.658 NULL NULL... NULL LH0402... 3.162 NULL NULL... NULL QF0598... 1.689 QF 0598... 10 Join result table on the database CARRID CONNID... DISTANCE CARRID CONNID... BOOKID 0017 0400 0402 0598... 2.572 6.658 3.162 1.689... AA AA AA QF... 0017 0017 0018 0598... 1 2 17 10... Left Outer Join Keep the Number of Roundtrips Small Please note: The resulting set always contains the complete ‘outer' table.

35  / 35 Effects of Rule 3

36  / 36 Rule 4: Keep the costs of the search down Keep the Search Costs Down

37  / 37 TableIndex A Block 2 Block 3 Block 1 Tables and indices 357357 4545 123123 6767 Record 4 Record 2 Record 5 Record 1 Record 6 Record 7 Record 3 Index B BEGBEG CDECDE ABAB FGFG Keep the Search Costs Down

38  / 38 SELECT * FROM sbook WHERE carrid = 'AA' AND connid = '0017' AND fldate = '19981205'.... Processing... ENDSELECT. Keep the Search Costs Down Use as many EQs as possible

39  / 39 SELECT carrid connid fldate bookid custtype orderdate FROM sbook INTO... WHERE carrid = 'LH' AND fldate = '19981119' AND orderdate = '19981118'.... Verarbeitung... ENDSELECT. SELECT f~carrid f~connid f~fldate b~bookid b~custtype b~orderdate INTO (carrid, connid, fldate, bookid, custtype) FROM sflight AS f INNER JOIN sbook as b ON f~carrid = b~carrid AND f~connid = b~connid AND f~fldate = b~fldate WHERE f~carrid = 'LH‘ AND fldate = '19981119' AND orderdate = '19981118'.... Verarbeitung... ENDSELECT. SBOOK Key: mandt carrid connid fldate bookid Use as many EQs as possible Keep the Search Costs Down

40  / 40 Keep the Search Costs Down 1234 Key to the SBOOK table Primary index Mand CarridConnidFldate Mand CarridConnidFldate Secondary index Carrid FldateOrderdate

41  / 41 f0 = x1 AND (f1 = y1 OR f1 = y2 OR f1 = y3). Replace the inner OR with an IN operator f0 = x1 AND f1 IN (y1, y2, y3). Keep the Search Costs Down

42  / 42 Place fields that are effective in the selection process at the beginning The following fields are not effective in the selection process: MANDT, BUKRS, GJAHR. The following are effective: BUCHUNGSNUMMER, BELNR, MATNR, KUNNR, Create small indices Avoid overlaps (disjunct indices) Up to 3 indices in each table do not have to be critical Avoid using complex WHERE clauses with IN and OR operators for index fields You cannot process NOT operators in SELECT using an index Verify the use of indices (for example, SQL trace) Index design Keep the Search Costs Down

43  / 43 R/3 work process DB work process Database cache Database Service processes Operating system Database files R/3 work process DB work process Network communication DB CPU consumption DB memory consumption Physical I/O Effects of Rule 4

44  / 44 Rule 5: Remove the load from the database Remove the Load from the Database

45  / 45 Buffer tables Avoid repeated reading of data Is a SELECT needed before a change is made? ORDER BY vs. SORT Use the “right” logical database ==> More s c a la b l e Remove the Load from the Database

46  / 46 What is table buffering? When should you buffer tables? What should you bear in mind when programming SQL accesses to buffered tables? ABAP Open SQL and table buffering Remove the Load from the Database

47  / 47 SELECT col1.......... FROM T001 PROGRAM XYZ RESULT Transfer data from the database server to the application server and the table buffer 2 1 3 4 5 Read data from database if it is not in the table buffer Table buffering – the concept Remove the Load from the Database Data- base server Table buffer Appl. server

48  / 48 Why buffer tables? Appl. server Data- base server SELECT SINGLE col1.. FROM T001 PROGRAMM XYZ Table buffer 8 - 600 ms 0.2 - 1 ms Remove the Load from the Database

49  / 49 Buffering types key1key2key3data key1 key2key3data key1key2 key3data key1key2key3 data Full buffering (100%) Generic buffering 1 key field Generic buffering 2 key fields Single-record buffering (partial) 001 002 003 001 002 003 001 002 003 A A B B A A B B B C D C A A A B B C C C D D D A A B B B A A A A B B B C D C A A A B B C C C C D D D D 4 2 3 1 5 1 3 6 8 1 2 3 0 5 3 2 3 6 2 4 2 3 5 8 1 2 3 4 Remove the Load from the Database

50  / 50 Buffer synchronization I Application server A Table buffer Communication system Database management system The database is up to date Local buffer is up to date... DDLOG UPDATE T001... Database R/3 DB interface Remove the Load from the Database

51  / 51 Buffer synchronization II Application server A Table buffer Communication system Database management system Application server B Buffer is NOT up to date... DDLOG UPDATE T001... Database Table buffer R/3 DB interface DDLOG INSERT DDLOG The database is up to date Local buffer is up to date Remove the Load from the Database

52  / 52 Application server AApplication server B Buffer is invalidated... SELECT DDLOG Buffer synchronization: every 1-2 minutes R/3 DB interface Buffer synchronization III Table buffer DDLOG Database DDLOG Database management system Communication system The database is up to date Local buffer is up to date Remove the Load from the Database

53  / 53 key1key2key3data key1 key2key3data key1key2 key3data key1key2key3 data Full buffering (100%) Generic buffering 1 key field Generic buffering 2 key fields Single-record buffering (partial) 001 002 003 001 002 001 002 Invalidation of the buffer Any change invalidates the buffer Changes invalidate the corresponding generic areas If there is a change in only one work area, only the one record is invalidated. Other changes invalidate the entire table. Remove the Load from the Database A A B B A A B B B C C A A C 3 3 2

54  / 54 When should you buffer a table? You should buffer a table if: It is read frequently It is relatively small It is read much more frequently than it is changed Possible candidates for buffering Control tables and customizing tables “Small” master data tables (100 data records for material master data -> few changes) Remove the Load from the Database

55  / 55 How is a table buffered? Where? In the technical settings in the maintenance routine of a table in the ABAP Dictionary. How? A decision that is based on the buffering type:  Fully buffered  Generic (by specifying the number of key fields for the generic key)  Single-record buffering Remove the Load from the Database

56  / 56 SQL statements that bypass the table buffer SELECT... BYPASSING BUFFER SELECT... DISTINCT SELECT... COUNT, SUM, AVG, MIN, MAX SELECT... ORDER BY f1... fn SELECT... GROUP BY / HAVING SELECT... FOR UPDATE SELECT... JOIN WHERE clause contains IS NULL statement WHERE clause contains subquery Native SQL statements (EXEC SQL.... ENDEXEC) Remove the Load from the Database

57  / 57 This should be avoided because: It places an unnecessary burden on the database Different results could be read Repeated reading of data Remove the Load from the Database

58  / 58 ORDER BY... does not necessarily mean that certain indexes will be used in the database... often results in large overhead for the database. Is the data really needed in a certain order? ORDER BY vs. SORT Remove the Load from the Database

59  / 59 Pre-defined effective table access Simplified list creation Optimization is performed centrally But use the “right” LDB! Logical databases Remove the Load from the Database

60  / 60 R/3 work process DB work process Database cache Database Service processes Operating system Database files R/3 work process DB work process Network communication DB CPU consumption DB memory consumption Physical I/O Effects of Rule 5

61  / 61 Performance Check List Is the program using SELECT * statements? Convert them to SELECT column 1 column 2 or use projection views. Are the fields within the SELECT..WHERE normalized to same domain? Convert data fields in where clause to same field level domain. Ex. ( SELECT FROM MARA…WHERE matnr = fld2… DATA: fld2 like mara-matnr)

62  / 62 Performance Check List Are Check Statements for table fields embedded in a SELECT … ENDSELECT loop? Incorporate the CHECK statements into the WHERE clause of the SELECT statement. Do SELECTS on non-key fields use an appropriate DB index or is the table buffered? Create index for the table in the data dictionary or buffer tables if they are read only or read mostly. SELECT into ITABs on key fields, LOOP ITAB or DELETE to filter on remaining criteria.

63  / 63 Performance Check List Is Program Using nested selects to retrieve data ?. Convert nested SELECT to database views or Inner joins. Are there SELECTs without WHERE condition against files that grow constantly?. Work to get as many key and non-key fields for each DB file. Select into ITAB on keys, then LOOP or DELETE ITAB to filter remaining non-key fields.

64  / 64 Performance Check List Is the program using Select … Append ITAB…. ENDSELECT technique to fill Internal Table ?. Change the processing to read the data immediately into an internal table. (SELECT VBELN AUART… INTO TABLE IVBAK…) Is the program using SELECT..ORDER BY statement?. Data should be read into an internal table first and then sorted, unless there is an appropriate index for the order by field.

65  / 65 Performance Check List Is the programming doing calculations/summations that can be done on the database via SUM, AVG, MIN, MAX functions for the SELECT statement?. Use the calculation capabilities of the database via SELECT SUM… Is the program inserting/updating or deleting data in dialog mode (not via an update function module)?. Make sure that the program issues COMMIT WORK statements when one or more logical units of work (LUWs) have been processed.

66  / 66 Course Objectives Five Rules for Writing an ABAP Program with Efficient Database Access. Performance Check List.


Download ppt " / 1 Optimize Database Access From ABAP QL.  / 2 Copyright Please note: No part of this Training Session may be reproduced or transmitted in any form."

Similar presentations


Ads by Google