Vous êtes sur la page 1sur 73

Performance Optimization Workshop

Kim, Ji Hoon SAP - Active Global Support February, 2008

Agenda

Analyzing Tools

Database Access Optimization

Internal Table Optimization

Case Study

SAP AG 2005, SAP Customer Program Optimization / 2

Analyzing Tools
Analyzing transaction steps SQL performance trace ABAP runtime analysis Performance Optimization Examples in SAP system

Analyzing Transaction Steps: Overview


Presentation server

Network
Dispatcher Queue

1 2 3 4
R/3 work process
User context

Dispatcher
13

Application server

Roll buffer

12

...
R/3 database interface

5 11

R/3 table buffer

Roll file Network

10

Database process Database buffer


SAP AG 2005, SAP Customer Program Optimization / 4

Database server

9 7 8

Analyzing Transaction Steps: R/3 Workload Monitor

SAP AG 2005, SAP Customer Program Optimization / 5

Analyzing Transaction Steps: Response Time

SAP AG 2005, SAP Customer Program Optimization / 6

Analyzing Tools
Analyzing transaction steps SQL performance trace ABAP runtime analysis Performance Optimization Examples in SAP system

SQL Performance Trace: Initial Screen

SAP AG 2005, SAP Customer Program Optimization / 8

SQL Performance Trace: SQL Trace Overview


Application server
ABAP Program

SELECT <field list> FROM vvbak WHERE ... SQL Trace File R/3 database interface R/3 table buffering

data records Database server

Database files
SAP AG 2005, SAP Customer Program Optimization / 9

Database process

Database buffer

SQL Performance Trace: Trace List

SAP AG 2005, SAP Customer Program Optimization / 10

SQL Performance Trace: Database Operations


Application server
ABAP Program

SELECT Fieldlist FROM vvbak WHERE vbeln = 0000000100.

SAP cursor cache

DB cursor available, no or yes?

Database Interface
PREPARE in trace Database server DB SQL cache SELECT mandt ... FROM VVBAK WHERE vbeln = :A1 DB cursor cache A1 Data buffer OPEN in trace FETCH in trace REOPEN in trace FETCH in trace

Data records

Shared memory of the database server


SAP AG 2005, SAP Customer Program Optimization / 11

SQL Performance Trace: Explain SQL


Table and Index Info for VVBAK Show Execution Plan for SQL Statement System Help Table VVBAK 01.09.1999 Compute 1.000 626 15 7.328 0 454

Last statistics date Analyze Method Number Explain with hint... Optimizer Trace Analyze... of rows SQL Statement Number of blocks allocated Number of empty blocks SELECT Average space Chain count "VBELN" , "AUART" FROM Average row length "VVBAK" WHERE UNIQUE Index VVBAK~0 "MANDT" = :A0 AND "VBELN" BETWEEN :A1 AND :A2

Column Name Execution Plan MANDT VBELN SELECT STATEMENT ( Estimated Costs = 3 , Estimated #Rows = 3 ) Index Statistics Analyze... TABLE ACCESS BY INDEX ROWID INDEX RANGE SCAN VVBAK~0 VVBAK

#Distinct 1 1.000

1 2
SAP AG 2005, SAP Customer Program Optimization / 12

SQL Performance Trace: ABAP Display


Basic SQL list - sorted by PID Trace Edit Goto System Help DDIC info Explain SQL ABAP display Extended list Transaction = Duration 41 805 41 785 41 788 40 1.061 1.327 85 6.919 826 41 5.924 800 46 5.704 217 Object name Oper Rec PID = RC Replace var. Sort

9551 Ptype = DIA Client = 100 User Statement

VVBAP REOPEN 0 0 SELECT WHERE "MANDT" = '100' AND "VBEL VVBAP FETCH 5 1403 VVBAP REOPEN 0 0 SELECT WHERE "MANDT" = '100' AND "VBEL ABAP-Editor: Display Program SAPBC490D_SQL_LOGICAL_VIEWS VVBAP FETCH 5 1403 VVBAP REOPEN 0 0 SELECT WHERE "MANDT" = '100' AND "VBEL VVBAP FETCH 5 1403 VVBAK REOPEN 0 0 Note marked line "MANDT" = '100' AND "VBEL SELECT WHERE VVBAK 10 1403 119 FETCH SELECT vbeln auart FROM vvbak VVBAP 0 SELECT WHERE "MANDT" = :A0 120 PREPARECORRESPONDING0 FIELDS OF TABLE g_itab_vvbakAND "VBELN" INTO VVBAP 0 0 SELECT WHERE "MANDT" = '100' AND "VBEL 121 OPEN WHERE vbeln IN g_vbeln. VVBAP 49 1403 122 FETCH VBAK_VBAP PREPARE 0 123 IF sy-subrc NE 0. 0 SELECT WHERE "MANDT" = :A0 AND the WHERE EXIT. ENDIF. " Necessary for "VBELN" VBAK_VBAP OPEN 0 0 SELECT WHERE "MANDT" = '100' AND "VBEL 124 VBAK_VBAP FETCH 49 1403 125 SELECT vbeln posnr matnr FROM vvbap VVBAK PREPARE 0 0 SELECT WHERE ( T_01 . "MANDT" = :A0 A 126 INTO CORRESPONDING FIELDS OF TABLE g_itab_vvbap VVBAK OPEN 0 0 SELECT WHERE ( T_01 . "MANDT" = '100' 127 FOR ALL ENTRIES IN g_itab_vvbak VVBAK FETCH 49 1403 128 WHERE vbeln = g_itab_vvbak-vbeln. VVBAK REOPEN 0 0 SELECT WHERE "MANDT" = '100' AND "VBEL Active 119 line SAP AG 2005, SAP Customer Program Optimization / 13

SQL Performance Trace: Compressed Summary

SAP AG 2005, SAP Customer Program Optimization / 14

SQL Performance Trace: Identical selects

Identical selects System Help Continue Executions 4 2 2 2 2 2 2 2 2 2 2 2 Cancel Object name VVBAK VVBAP VVBAP VVBAP VVBAP VVBAP VVBAP VVBAP VVBAP VVBAP VVBAP VVBAP WHERE clause "MANDT" "MANDT" "MANDT" "MANDT" "MANDT" "MANDT" "MANDT" "MANDT" "MANDT" "MANDT" "MANDT" "MANDT" = = = = = = = = = = = = '100' '100' '100' '100' '100' '100' '100' '100' '100' '100' '100' '100' AND AND AND AND AND AND AND AND AND AND AND AND "VBELN" "VBELN" "VBELN" "VBELN" "VBELN" "VBELN" "VBELN" "VBELN" "VBELN" "VBELN" "VBELN" "VBELN" BETWEEN '000000000 = '0000000001' = '0000000002' = '0000000003' = '0000000004' = '0000000005' = '0000000006' = '0000000007' = '0000000008' = '0000000009' = '0000000010' = '0000000001' OR

SAP AG 2005, SAP Customer Program Optimization / 15

Analyzing Tools
Analyzing transaction steps SQL performance trace ABAP runtime analysis Performance Optimization Examples in SAP system

ABAP Runtime Analysis: Overview


Application server
ABAP runtime environment

SET RUNTIME ANALYZER ON


ABAP Trace File LOOP AT itab ENDLOOP SET RUNTIME ANALYZER OFF

The three phases of the ABAP runtime analysis: Local Trace File
Limiting the analysis Obtaining the run data Evaluating the results

SAP AG 2005, SAP Customer Program Optimization / 17

ABAP Runtime Analysis: Initial Screen

Runtime analysis: Initial Screen Runtime analysis Edit Goto Utilities Settings System Help

Tips & Tricks Measurement Short description In the current user session Transaction Program Function module Execute Limit analysis Variant DEFAULT Standard variant

In parallel mode On / Off

Performance file Runs

the object in the Other file current user session

"On" causes the data to be recorded in a separate user session

SAP AG 2005, SAP Customer Program Optimization / 18

ABAP Runtime Analysis: Aggregation

Full aggregation Full aggregation Program part X Program part X Statement Y Statement Y DO 1000 TIMES DO 1000 TIMES Program part Program part X Program part X Statement Y Statement Y ENDDO ENDDO

=> 1 Entry => 1 Entry => 1 Entry => 1 Entry

statements

Duration & type

Maximum size of file Maximum execution time Aggregation Full Per calling position None

1000 1800

KB Seconds

Aggregation per calling item Aggregation per calling item Program part X Program part X Statement Y Statement Y DO 1000 TIMES DO 1000 TIMES Program part X => 2 Entries Program part X => 2 Entries Statement Y => 2 Entries Statement Y => 2 Entries ENDDO ENDDO

No aggregation Program part X Statement Y DO 1000 TIMES Program part X Statement Y ENDDO
SAP AG 2005, SAP Customer Program Optimization / 19

=> 1001 Entries => 1001 Entries

ABAP Runtime Analysis: Evaluating the Results

Initial screen

Select data file Save data file locally Evaluate the data

ABAP DB R/3

Results display screen

Statistics records: Conversions Loaded programs

Calls Details view

Number

Gross

...

Runtime analysis LOOP ...

1 3

80,610 20,113

SAP AG 2005, SAP Customer Program Optimization / 20

10

ABAP Runtime Analysis: Evaluating the Results (2)

SAP AG 2005, SAP Customer Program Optimization / 21

ABAP Runtime Analysis: Evaluating the Results (3)

. . . SELECT . . . PERFORM . . READ TABLE . . . . . .

Gross

Net

SAP AG 2005, SAP Customer Program Optimization / 22

11

Analyzing Tools
Analyzing transaction steps SQL performance trace ABAP runtime analysis Performance Optimization Examples in SAP system

Performance Optimization Example in SAP system (1)


In any ABAP editor transactions (SE37, SE38, SE24, SE80, ) Menu PATH :

SAP AG 2005, SAP Customer Program Optimization / 24

12

Performance Optimization Example in SAP system (2)

SAP AG 2005, SAP Customer Program Optimization / 25

Single transaction analysis (ABAP + SQL Trace)


Transaction Code : ST12

SAP AG 2005, SAP Customer Program Optimization / 26

13

Memory Analysis in ABAP debugger

SAP AG 2005, SAP Customer Program Optimization / 27

Database Access Optimization


Unsuitable Access Path Introduction to DB Indexes Introduction to DB Joins Changing the ABAP coding Changing the Index Design

14

Overview: Cost-Based Database Optimizer


Possible access paths
ROWID MAND T VBELN POSNR MATN R ... 001 0000 001 22 0003 0047 11 ... VBELN POSNR MATN R ... 001 0000 132 47 0003 0008 15 ... ... 4433 9 ... ROWID MAND T

Estimated costs
Table and index statistics SELECT STATEMENT TABLE ACCESS FULL Cost-based database optimizer VVBAK

... 4533 2

... ROWID MAND T VBELN POSNR MATN R ... 001 0000 001 22 0013 0012 34 ...

... 4587 6

...

ROWID MAND T ... ... 4689 4

VBELN POSNR MATN R ... 002 0000 965 8 0012 0001 23 ...

...

Full table scan ...


ROWID MAN DT VBELN ERDAT ERZET ERNA M ... 001

SELECT STATEMENT INDEX UNIQUE SCAN VVBAK~0

... 658 72 000 012 3 ...

SELECT STATEMENT Index access INDEX RANGE SCAN VVBAK~0

SAP AG 2005, SAP Customer Program Optimization / 29

Overview: DB SQL Cache


DBMS Database server memory

Data buffer

Data buffer (e.g. 8 KB blocks) Data blocks


SELECT * FROM MARA WHERE ...

Logical read accesses


DB work process

DB SQL Cache
SQL statements with access strategy

R/3 work process

Physical read accesses


Database Database files

...
SAP AG 2005, SAP Customer Program Optimization / 30

15

DB SQL Cache Analysis: Overview (2)


Suitable access path
Appl. Server
ABAP program R/3 DB interface

No suitable access path


Appl. Server
ABAP program R/3 DB interface

Many records Database system


Database buffer DBMS process

Few records Database system


Database buffer DBMS process

Transferred data records

Logical read accesses

Many blocks

Many blocks Physical read accesses Database Database

SAP AG 2005, SAP Customer Program Optimization / 31

Overview: Unsuitable Access Path

MANDT VBELN 001 001 001 001 001 001 001 001 001 002 002 002 002 000013245 000013246 000013247 000013248 000013249 000013250 000113245 000113251 000113252 000013249 000013250 000013251 000013252

OBJNR 654875454 697935435 673225747 216356543 687535435 258674802 983231365 522112486 325348654 241567685 968351332 874352321 544546546

ERNAM ERNIE FRED JAMES GUS ERNIE KURT MAX BERT BERT HUGO FRED MAX GUS

Narrowing the search

Data records corresponding to WHERE clause

Search range (area of table that needs to be searched for required records)

SAP AG 2005, SAP Customer Program Optimization / 32

16

Overview: Suitable Access Path (1)


Reducing columns to be transferred
MANDT VBELN OBJNR 654875454 697935435 673225747 216356543 687535435 258674802 983231365 522112486 325348654 241567685 968351332 874352321 544546546 ERNAM ERNIE FRED JAMES GUSS ERNIE KURT MAX BERT BERT HUGO FRED MAX GUSS Data records corresponding to WHERE clause Columns corresponding to SELECT clause

Reducing rows to be transferred

001 001 001 001 001 001 001 001 001 002 002 002 002

000013245 000013246 000013247 000013248 000013249 000013250 000113245 000113251 000113252 000013249 000013250 000013251 000013252

Search range (area of table that needs to be searched for required records)

SAP AG 2005, SAP Customer Program Optimization / 33

Overview: Suitable Access Path (2)

MANDT VBELN 001 001 001 001 001 001 001 001 001 002 002 002 002 000013245 000013246 000013247 000013248 000013249 000013250 000113245 000113251 000113252 000013249 000013250 000013251 000013252

OBJNR 654875454 697935435 673225747 216356543 687535435 258674802 983231365 522112486 325348654 241567685 968351332 874352321 544546546

ERNAM ERNIE FRED JAMES GUSS ERNIE KURT MAX BERT BERT HUGO FRED MAX GUSS

FETCH

Reducing data transfer

FETCH FETCH

:
FETCH

Data transfer to application server


SAP AG 2005, SAP Customer Program Optimization / 34

17

Introduction to DB Index : Overview (1)

SAP AG 2005, SAP Customer Program Optimization / 35

Introduction to DB Index : Overview (2)

SAP AG 2005, SAP Customer Program Optimization / 36

18

Introduction to DB Index : Index Unique Scan

SAP AG 2005, SAP Customer Program Optimization / 37

Introduction to DB Index : Index Range Scan (1)

SAP AG 2005, SAP Customer Program Optimization / 38

19

Introduction to DB Index : Index Range Scan (2)


POSNR

SAP AG 2005, SAP Customer Program Optimization / 39

Introduction to DB Index : Overview (3)

SAP AG 2005, SAP Customer Program Optimization / 40

20

Introduction to DB Index : Concatenation

SAP AG 2005, SAP Customer Program Optimization / 41

Full Table Scan

SAP AG 2005, SAP Customer Program Optimization / 42

21

Table Access Strategies

SAP AG 2005, SAP Customer Program Optimization / 43

DB2 example : Index Unique Scan

SAP AG 2005, SAP Customer Program Optimization / 44

22

Oracle example : Index Unique Scan

SAP AG 2005, SAP Customer Program Optimization / 45

DB2 example : Index Range Scan

SAP AG 2005, SAP Customer Program Optimization / 46

23

Oracle example : Index Range Scan

SAP AG 2005, SAP Customer Program Optimization / 47

DB2 example : Full Table Scan

SAP AG 2005, SAP Customer Program Optimization / 48

24

Oracle example : Full Table Scan

SAP AG 2005, SAP Customer Program Optimization / 49

DB2 example : Concatenation

SAP AG 2005, SAP Customer Program Optimization / 50

25

Oracle example : Concatenation

SAP AG 2005, SAP Customer Program Optimization / 51

Database Access Optimization


Unsuitable Access Path Introduction to DB Indexes Introduction to DB Joins Changing the ABAP coding Changing the Index Design

26

Introduction to DB Joins : Overview


Application
SELECT T1~K1 T1~K2 T1~F2 T2~F3 T2~F4 FROM tab1 AS T1 INNER JOIN tab2 AS T2 INNER JOIN ON T1~K1 = T2~K1 WHERE T1~F2 = value.

DBMS
T1.K1 T1.F1 T1.F2 T2.F3 T2.F4 T1.K1 T1.F1 T1.F2 T2.F3 T2.F4 T1.K1 T1.F1 T1.F2 T2.F3 T2.F4 T1.K1 T1.F1 T1.F2 T2.F3 T2.F4 T1.K1 T1.F1 T1.F2 T2.F3 T2.F4

Resulting set

Database

T1 K1 F1 F2

T2 K1 F3 F4

SAP AG 2005, SAP Customer Program Optimization / 53

Introduction to DB Joins : Nested Loop

SELECT t1~vbeln t2~vbobj INTO ... FROM vvbak AS t1 INNER JOIN vvbuk AS t2 ON t1~vbeln = t2~vbeln WHERE t1~objnr = '0000777'.

Nested loop
MANDT 001 001 001 001 001 001 ... VBELN 0000120 0000121 0000122 0000123 0000124 0000125 OBJNR ... 0000110 0000110 0000777 0000777 0000777 001 ... 0000123 ... MANDT VBELN 001 0000121 001 0000122 ... ... ...

VVBAK

VVBUK

SAP AG 2005, SAP Customer Program Optimization / 54

27

Nested Loop
The top input to the nested loop is the outer table The bottom input to the nested loop is the inner table For each outer row, searches for matching rows are in the inner input table Effective if the outer input is very small and the inner input is preindexed and very large Optimizer sometimes sorts the outer input to improve locality of the searches on the index over the inner input Best when search uses an index (indexes on join columns are used) Low memory requirement
SAP AG 2005, SAP Customer Program Optimization / 55

DB2 Nested Loop

SAP AG 2005, SAP Customer Program Optimization / 56

28

Oracle Nested Loop

SAP AG 2005, SAP Customer Program Optimization / 57

Introduction to DB Joins : Sort Merge Join


SELECT t1~vbeln t2~vbobj INTO ... FROM vvbap AS t1 INNER JOIN vvbak AS t2 ON t1~vbeln = t2~vbeln WHERE t1~cuobj = 12345 AND t2~objnr = 0000777.

Sort merge join


MANDT 001 001 001 001 ... 001 001 VBELN 0000121 0000122 0000123 0000123 0000123 0000456 CUOBJ 1 2 3 4 12345 12345 ... ... ... ... ... ... ... MANDT 001 001 001 001 001 001 ... VBELN 0000120 0000121 0000122 0000123 0000124 0000125 OBJNR ... 0000110 0000110 0000777 0000777 0000777

VVBAP
Selection and merging of table records
SAP AG 2005, SAP Customer Program Optimization / 58

VVBAK

29

Sort Merge Join


Both inputs should be sorted on the merge column keys An index on a correct set of columns is useful A many-to-many merge join uses a temporary table to store rows Very fast if the data that you want can be obtained presorted from existing B-tree indexes

SAP AG 2005, SAP Customer Program Optimization / 59

DB2 Sort Merge Join

SAP AG 2005, SAP Customer Program Optimization / 60

30

Oracle Sort Merge Join

SAP AG 2005, SAP Customer Program Optimization / 61

Important Access Strategies for DB Joins

Nested loop
Decide which tables to use for access Access to next innermost table using JOIN condition

Sort Merge Join


Evaluate WHERE clause on all relevant tables Sort according to JOIN condition Merge resulting set

SAP AG 2005, SAP Customer Program Optimization / 62

31

Database Access Optimization


Unsuitable Access Path Introduction to DB Indexes Introduction to DB Joins Changing the ABAP coding Changing the Index Design

Missing WHERE Conditions (1)

SELECT bukrs belnr gjahr specific index fields: MANDT, BELNR FROM bkkpf => gap in index search string INTO TABLE g_itab_bkkpf WHERE belnr = 0000000100. Index search string: 001____0000000100

ineffective use of index


SELECT bukrs belnr gjahr FROM bkkpf INTO TABLE g_itab_bkkpf specific index fields: MANDT, BUKRS, BELNR => index fields specified fully WHERE bukrs = 0001 AND belnr = 0000000100. Index search string: 00100010000000100

effective use of index


SAP AG 2005, SAP Customer Program Optimization / 64

32

Missing WHERE Conditions (2)


SELECT vbeln erdat FROM vvbak specific index fields: VBELN CLIENT SPECIFIED CLIENT SPECIFIED => index fields not specified INTO TABLE g_itab_vvbak Index search string: ___0000000100 WHERE vbeln = 0000000100.

ineffective use of index


SELECT vbeln erdat FROM vvbak INTO TABLE g_itab_vvbak WHERE vbeln = 0000000100. specific index fields: MANDT, * or VBELN SELECT vbeln erdat FROM vvbak => index fields specified fully CLIENT SPECIFIED Index search string: 0010000000100 INTO TABLE g_itab_vvbak WHERE mandt WHERE mandt = sy-mandt AND vbeln = 0000000100.

effective use of index


SAP AG 2005, SAP Customer Program Optimization / 65

Critical Operators (NOT and <>)

SELECT vbeln bstnk kunnr FROM vvbak INTO TABLE g_itab_vvbak specific index fields: MANDT, WHERE kunnr = '0000000050' KUNNR and FAKSK => ineffective AND NOT faksk = '00'. use of indexes

Negative formulation
SELECT vbeln bstnk kunnr FROM vvbak INTO TABLE g_itab_vvbak specific index fields: MANDT, WHERE kunnr = '0000000050' KUNNR and FAKSK => effective use AND faksk IN ('01','02','03', IN of indexes '04','05','08','09').

Positive formulation
SAP AG 2005, SAP Customer Program Optimization / 66

33

Critical Operators (BETWEEN)

SELECT vbeln erdat erzet FROM vvbak INTO TABLE g_itab_vvbak WHERE vbeln BETWEEN '0000000001' AND '0000000005'.

BETWEEN operator
SELECT vbeln erdat erzet FROM vvbak INTO TABLE g_itab_vvbak WHERE vbeln IN ('0000000001', '0000000002', '0000000003', '0000000004', '0000000005').

IN operator
Replace WHERE field LIKE value with WHERE field = value
SAP AG 2005, SAP Customer Program Optimization / 67

Omit WHERE field LIKE '%'

Sort at Database Level or in ABAP?

SAP AG 2005, SAP Customer Program Optimization / 68

34

Database Access Optimization


Unsuitable Access Path Introduction to DB Indexes Introduction to DB Joins Changing the ABAP coding Changing the Index Design

Prerequisites for Changing the Index Design

SQL statements from SAP programs:


Look for R/3 Notes

SQL statements from customer-owned programs:


Avoid additional indexes on transaction data Change existing indexes If necessary, delete existing indexes

No secondary indexes on R/3 basis tables


Tables with D010, D020, DD* or NAST

SAP AG 2005, SAP Customer Program Optimization / 70

35

Rules for Changing the Index Design


General rules
Prefer disjunctive indexes No unintentionally used indexes As few indexes as possible per table (as few as possible, but as many as necessary)

Selection of index fields


As few fields as possible in index As selective fields as possible in index Selective fields as near to the beginning as possible

Do not change SAP standard indexes


unless recommended by SAP (see SAP Note System)

SAP AG 2005, SAP Customer Program Optimization / 71

Selectivity Analysis: Semantic Typing

SAP AG 2005, SAP Customer Program Optimization / 72

36

Selectivity Analysis: Distinct Values


Tables and Indexes Columns of Table VVBAK Database analysis Goto Monitor System Help Back Table <-> Indices Extents Table columns Detailed analysis 30.09.1999 14:46:44 KP6 pwdf0024 VVBAK History Sort

Data from DBA_TAB_COLUMNS

Database Column MANDT VBELN ERDAT ERZET ERNAM ANGDT BNDDT Data type VARCHAR2 VARCHAR2 VARCHAR2 VARCHAR2 VARCHAR2 VARCHAR2 VARCHAR2 Last analyzed* 30.09.1999 30.09.1999 30.09.1999 30.09.1999 30.09.1999 30.09.1999 30.09.1999 Sample size* 220,780 220,780 220,780 220,780 220,780 220,780 220,780 Distinct values* 1 220,780 856 187,023 73 435 267

Updated by ORACLE STATISTICS UPDATE


SAP AG 2005, SAP Customer Program Optimization / 73

Selectivity Analysis: Histograms (1)

SAP AG 2005, SAP Customer Program Optimization / 74

37

DB05: Selectivity Analysis: Histograms (2)


Date/time of analysis: Analyzed table: Total number of rows: Requested bytes per row: Requested bytes to be buffered 100%: Current buffering mode: 02.08.1999 17:45:28 COSS 635,336 rows 31 bytes per row 19,695,416 bytes no buffering

Compare number of distinct values with total number of records

16,587 combinations of index fields return between 1 and 10 records

Rows per generic key Key fields USPOB GJAHR WRTTP

Distinct values

1 - 10

11 - 100

101 - 1.000

10.001 - 100.000

100.001 - 1.000.000

Number of areas that are specified by generic key and contain the given ... 4,506 7,172 9,195 16,607 3,234 5,715 7,195 16,587 1,011 1,072 1,487 1,536 210 333 457 428 0 5 8 8 2 1 0 0

VERSN

SAP AG 2005, SAP Customer Program Optimization / 75

DB05: Selectivity Analysis: Histograms (3)


Date/time of analysis: Analyzed table: Total number of rows: Requested bytes per row: Requested bytes to be buffered 100%: Current buffering mode: 02.08.1999 17:45:28 COSS 635,336 rows 31 bytes per row 19,695,416 bytes no buffering

8 combinations of index fields return between 10,001 and 100,000 records

Rows per generic key Key fields USPOB GJAHR WRTTP

Distinct values

1 - 10

11 - 100

101 - 1,000

10,001 - 100,000

100,001 - 1,000,000

Number of areas that are specified by generic key and contain the given ... 4,506 7,172 9,195 16,607 3,234 5,715 7,195 16,587 1,011 1,072 1,487 1,536 210 333 457 428 0 5 8 8 2 1 0 0

VERSN

SAP AG 2005, SAP Customer Program Optimization / 76

38

Interference Analysis (1)


Clarify table usage
Use where-used list from ABAP Dictionary

Perform object-related DB SQL cache analysis before changing index design


DB SQL cache => restrict to tables and views Retain all SQL statements with access strategy, where the tables or views are affected

Change index design Reset DB SQL cache Perform object-related DB SQL cache analysis after changing index design
DB SQL cache => restrict to tables and views Retain all SQL statements with access strategy, where the tables or views are affected
SAP AG 2005, SAP Customer Program Optimization / 77

Interference Analysis (2)


Where-Used List Database table DB table Dictionary: Initial Screen Dictionary object Edit Goto Utilities VVBAK

Used in Environment System Help Programs Screens Logical databases Function module interfaces Domains Structures DB tables Table types Views Entity types Lock objects Search helps Classes (definition) Classes (implementation) Create Interfaces Test cases

Database table View Data type Domain Search help Lock object Display

VVBAK

Change

In background

Search area

SAP AG 2005, SAP Customer Program Optimization / 78

39

Interference Analysis (3)


Database Performance: Shared SQL
? Reset Since DB start Select table Explain

1999 10:12:21 Shared Cursor Cache


Total Executions 101,002 34,004 5,340 702 546 Disk Reads 4,506 7,145 92,195 107 16,347 Reads/ Execution s 0.0 0.2 17.3 0.2 29.9 Buffer Gets 45,678 109,072 7,041,487 1,536 231,536 Gets/ Execution 0.5 3.2 1,318.6 2.2 424.0 Records/ Execution 0.8 6.0 0.4 1.0 47.5 SQL Text SELECT * FROM SELECT VBELN SELECT MANDT UPDATE VVBAK SELECT * FROM

SELECT STATEMENT TABLE ACESS BY INDEX ROWID INDEX RANGE SCAN VVBAK

VVBAK ~ 0

SAP AG 2005, SAP Customer Program Optimization / 79

Database Access Optimization


Suitable Access Path General Methods Accessing Individual Tables Accessing Several Tables Accessing Clustered and Pooled Tables R/3 Table Buffering

40

Reducing the Records to be Transferred (1)


SELECT ... FROM resb INTO TABLE g_itab_resb.

Missing WHERE clause


SELECT ... FROM resb INTO TABLE g_itab_resb WHERE matnr IN g_matnr.

Unstable WHERE clause


SELECT ... FROM resb INTO TABLE g_itab_resb WHERE matnr IN g_matnr AND kzear IS INITIAL.

Stable WHERE clause


SAP AG 2005, SAP Customer Program Optimization / 81

Reducing the Records to be Transferred (2)

SELECT vbeln erdat kunnr FROM vvbak INTO g_wa_vvbak. CHECK g_wa_vvbak-vbeln GE g_low CHECK AND g_wa_vvbak-vbeln LE g_high. APPEND g_wa_vvbak TO g_itab_vvbak. ENDSELECT.

CHECK condition
SELECT vbeln erdat kunnr FROM vvbak INTO g_wa_vvbak WHERE vbeln BETWEEN g_low AND g_high. WHERE APPEND g_wa_vvbak TO g_itab_vvbak. ENDSELECT.

WHERE condition
SAP AG 2005, SAP Customer Program Optimization / 82

41

Reducing the Columns to be Transferred


SELECT * FROM vvbak INTO TABLE g_itab_vvbak_all WHERE vbeln IN g_vbeln.

SELECT * INTO TABLE


SELECT vbeln erdat kunnr FROM vvbak SELECT INTO CORRESPONDING FIELDS OF TABLE g_itab_vvbak_fieldlist OF TABLE WHERE vbeln IN g_vbeln.

SELECT field list INTO ... SET


SELECT vbeln erdat kunnr FROM vvbak SELECT INTO TABLE g_itab_vvbak_fieldlist WHERE vbeln IN g_vbeln.

SELECT field list INTO TABLE


SAP AG 2005, SAP Customer Program Optimization / 83

Database Access Optimization


Suitable Access Path General Methods Accessing Individual Tables Accessing Several Tables Accessing Clustered and Pooled Tables R/3 Table Buffering

42

SELECT FOR ALL ENTRIES (1)


LOOP AT g_itab_vvbak INTO g_wa_vvbak. LOOP AT SELECT kunnr adrnr FROM kkna1 SELECT APPENDING TABLE g_itab_kkna1 WHERE kunnr = g_wa_vvbak-kunnr. ENDLOOP. ENDLOOP.

SELECT inside LOOP

DESCRIBE TABLE g_itab_vvbak LINES lines. IF lines EQ 0. Check whether driver table is empty MESSAGE I051(bc490). EXIT. ENDIF. SORT g_itab_vvbak BY kunnr. DELETE ADJACENT DUPLICATES FROM g_itab_vvbak. SELECT kunnr adrnr FROM kkna1 INTO TABLE g_itab_kkna1 FOR ALL ENTRIES IN g_itab_vvbak WHERE kunnr = g_itab_vvbak-kunnr.
Eliminate duplicates

SELECT FOR ALL ENTRIES


SAP AG 2005, SAP Customer Program Optimization / 85

SELECT FOR ALL ENTRIES (2)


Object VVBAK VVBAK KKNA1 KKNA1 KKNA1 KKNA1 KKNA1 KKNA1 Oper REOPEN FETCH REOPEN FETCH REOPEN FETCH REOPEN FETCH Rec 0 100000 0 149 0 149 0 149 SELECT "KUNNR" , "ADRNR" FROM "KKNA1" WHERE "MANDT" = :A0 AND "KUNNR" = :A1 OR "MANDT" = :A2 AND "KUNNR" = :A3 OR "MANDT" = :A4 AND "KUNNR" = :A5 OR "MANDT" = :A6 AND "KUNNR" = :A7 ....

... rsdb/max_blocking_factor specifies number of concatenations

...

...

SELECT STATEMENT

(Estimated Costs = ....

CONCATENATION TABLE ACESS BY INDEX ROWID KKNA1

INDEX UNIQUE SCAN KKNA1~0 TABLE ACESS BY INDEX ROWID INDEX UNIQUE SCAN
SAP AG 2005, SAP Customer Program Optimization / 86

KKNA1

KKNA1~0

43

RANGES Tables

SELECT mandt vbeln erdat erzet FROM vvbak INTO TABLE g_itab_vvbak WHERE vbeln IN g_itab_vbeln. IN

SELECT with RANGES table


SELECT STATEMENT (Estimated Costs = .... CONCATENATION TABLE ACESS BY INDEX ROWID SELECT "MANDT" , "VBELN" , "ERDAT" , "ERZET" ) FROM "VVBAK" WHERE "MANDT" = :A0 AND VVBAKVBELN IN ( :A1 , :A2 , :A3 , )

INDEX UNIQUE SCAN VVBAK~0 TABLE ACESS BY INDEX ROWID INDEX UNIQUE SCAN
SAP AG 2005, SAP Customer Program Optimization / 87

VVBAK

VVBAK~0

Aggregate Functions

SELECT vbeln zmeng matnr FROM vvbap INTO TABLE g_itab_vvbap WHERE vbeln IN g_vbeln. LOOP AT g_itab_vvbap. * Compute maximum of zmeng and minimum of matnr ... ENDLOOP.

Aggregate functions in ABAP


SELECT vbeln max( zmeng ) min( matnr ) FROM vvbap INTO TABLE g_itab_vvbap WHERE vbeln IN g_vbeln GROUP BY vbeln.

Aggregate functions at DB level


SAP AG 2005, SAP Customer Program Optimization / 88

44

HAVING Clause

SAP AG 2005, SAP Customer Program Optimization / 89

Array Fetch

SAP AG 2005, SAP Customer Program Optimization / 90

45

Up to N rows

SAP AG 2005, SAP Customer Program Optimization / 91

UPDATE .. SET Field = Value

SAP AG 2005, SAP Customer Program Optimization / 92

46

Create, Change and Delete Mass Data

SAP AG 2005, SAP Customer Program Optimization / 93

Database Access Optimization


Suitable Access Path General Methods Accessing Individual Tables Accessing Several Tables Accessing Clustered and Pooled Tables R/3 Table Buffering

47

INNER and OUTER JOIN Logic

MANDT

VBELN 0000000001 0000000001 0000000003 0000000003 0000000004 ...

POSNR 0001 0002 0001 0002 0001

VVBAP

VVBAK

MANDT 400 400 400

VBELN
0000000001 0000000002 0000000003

...

400 400 400

...

...

400 400 ...

INNER JOIN
MANDT VBELN 400 400 400 400 ... POSNR 0000000001 0001 0000000001 0002 0000000003 0001 0000000003 0002 ... 400 400 400 400 ... 400 ...

LEFT OUTER JOIN


MANDT VBELN POSNR 0000000001 0001 0000000001 0002 0000000002 0000000003 0001 ... 0000000003 0002 ...

SAP AG 2005, SAP Customer Program Optimization / 95

Implementation in ABAP

SAP AG 2005, SAP Customer Program Optimization / 96

48

Nested SELECTs
SELECT SELECT vbeln kunnr FROM vvbak INTO (g_wa_vvbak_kkna1-vbeln, g_wa_vvbak_kkna1-kunnr) WHERE vbeln IN g_vbeln. SELECT SINGLE adrnr FROM kkna1 SELECT INTO g_wa_vvbak_kkna1-adrnr WHERE kunnr EQ g_wa_vvbak_kkna1-kunnr. IF sy-subrc EQ 0. APPEND g_wa_vvbak_kkna1 TO g_itab_vvbak_kkna1. ENDIF. ENDSELECT ENDSELECT.
Object SELECT STATEMENT (Estimated Costs = .... VVBAK~0 ) VVBAK VVBAK KKNA1 KKNA1 KKNA1 SELECT STATEMENT (Estimated Costs = .... KKNA1~0 ) KKNA1 KKNA1 KKNA1 Oper REOPEN FETCH REOPEN FETCH REOPEN FETCH REOPEN FETCH Rec 0 1000 0 1 0 1 0 1

INDEX RANGE SCAN

INDEX UNIQUE SCAN

...
SAP AG 2005, SAP Customer Program Optimization / 97

...

...

Problems with Nested SELECTs


Inefficient transfer
Data records from inner table are transferred record by record Lots of small fetches, instead of fewer and more compact fetches

Identical SELECTs
Data records from inner table are re-read unnecessarily

Incorrect order of access


Whether a table is an inner or outer table is defined in coding Dynamic WHERE conditions are ignored
SAP AG 2005, SAP Customer Program Optimization / 98

49

Creating JOINs Over the Database

Possible access strategies:


Nested loop Sort merge join

Database optimizer determines order of access Access to outer table should be as selective as possible (selective WHERE conditions) For each data record in the outer table, as few records as possible should exist in the inner table (selective JOIN conditions) An index for the JOIN conditions should exist on the inner table (at least)

SAP AG 2005, SAP Customer Program Optimization / 99

Database View

SELECT vbeln kunnr adrnr FROM vbak_kna1 INTO TABLE g_itab_vvbak_kkna1 WHERE vbeln IN g_vbeln.

SELECT STATEMENT

(Estimated Costs = ....

Create DB view in Dictionary

NESTED LOOPS TABLE ACESS BY INDEX ROWID INDEX RANGE SCAN VVBAK

VVBAK~0 KKNA1

CREATE VIEW vbak_kna1 ...

TABLE ACESS BY INDEX ROWID INDEX UNIQUE SCAN

KKNA1~0

SAP AG 2005, SAP Customer Program Optimization / 100

50

ABAP INNER JOIN


SELECT t1~vbeln t2~kunnr t2~adrnr INTO TABLE g_itab_vvbak_kkna1 FROM vvbak AS t1 INNER JOIN kkna1 AS t2 ON t1~kunnr = t2~kunnr WHERE t1~vbeln IN g_vbeln.
SELECT STATEMENT
MANDT VBELN 001 001 001 001 001 001 001 001 ... 0000120 0000121 0000122 0000123 0000124 0000125 0000126 0000127 KUNNR ... 0000100 0000100 0000101 0000101 0000102 0000103 0000103 0000104

(Estimated Costs = ....

NESTED LOOPS TABLE ACESS BY INDEX ROWID INDEX RANGE SCAN VVBAK

VVBAK~0 KKNA1

TABLE ACESS BY INDEX ROWID


MANDT 001 001 001 ... KUNNR 0000100 0000103 0000104 ... ... ... ...

INDEX UNIQUE SCAN

KKNA1~0

VVBAK

KKNA1
SAP AG 2005, SAP Customer Program Optimization / 101

FOR ALL ENTRIES Over Two Database Tables


SELECT vbeln kunnr FROM vvbak INTO TABLE g_itab_vvbak WHERE vbeln IN g_vbeln.
SELECT STATEMENT (Estimated Costs = .... )

DESCRIBE TABLE g_itab_vvbak LINES g_lines. ... SORT g_itab_kkna1 BY kunnr. DELETE ADJACENT DUPLICATES FROM g_itab_kkna1.

CONCATENATION TABLE ACCESS BY INDEX ROWID INDEX UNIQUE SCAN KKNA1~0 KKNA1 KKNA1

TABLE ACCESS BY INDEX ROWID KKNA1~0

SELECT kunnr adrnr INDEX UNIQUE SCAN FROM kkna1 INTO (g_wa_kkna1-kunnr, g_wa_kkna1-adrnr) FOR ALL ENTRIES IN g_itab_kkna1 WHERE kunnr = g_itab_kkna1-kunnr. MODIFY g_itab_kkna1 FROM g_wa_kkna1 INDEX sy-dbcnt. ENDSELECT. LOOP AT g_itab_vvbak INTO g_wa_vvbak. ... ENDLOOP.
SAP AG 2005, SAP Customer Program Optimization / 102

51

Subquery
... SELECT kunnr adrnr INTO TABLE g_itab_vvbak_kkna1 FROM kkna1 WHERE kunnr IN ( SELECT DISTINCT kunnr FROM vvbak ( WHERE WHERE vbeln IN g_vbeln ) ). ...
SELECT STATEMENT (Estimated Costs = .... ) NESTED LOOPS ... TABLE ACCESS BY INDEX ROWID INDEX RANGE SCAN TABLE ACCESS BY INDEX ROWID INDEX UNIQUE SCAN KKNA1~0 VVBAK

VVBAK~0

KKNA1

SAP AG 2005, SAP Customer Program Optimization / 103

ABAP OUTER JOIN


SELECT t1~vbeln t2~kunnr t2~adrnr INTO TABLE g_itab_vvbak_kkna1 FROM vvbak AS t1 LEFT OUTER JOIN kkna1 AS t2 ON t1~kunnr = t2~kunnr WHERE t1~vbeln IN g_vbeln.

SELECT STATEMENT
MANDT VBELN 001 001 001 001 001 001 001 001 ... 0000120 0000121 0000122 0000123 0000124 0000125 0000126 0000127 KUNNR ... 0000100 0000100 0000101 0000101 0000102 0000103 0000103 0000104

(Estimated Costs = ....

NESTED LOOPS OUTER TABLE ACESS BY INDEX ROWID INDEX RANGE SCAN VVBAK~0 KKNA1 VVBAK

TABLE ACESS BY INDEX ROWID


MANDT 001 001 001 ... KUNNR 0000100 0000103 0000104 ... ... ... ...

INDEX UNIQUE SCAN

KKNA1~0

VVBAK

KKNA1
SAP AG 2005, SAP Customer Program Optimization / 104

52

Database Access Optimization


Suitable Access Path General Methods Accessing Individual Tables Accessing Several Tables Accessing Clustered and Pooled Tables R/3 Table Buffering

Pooled and Cluster Tables: Introduction

Logical view Transparent tables TAB_B Cluster tables CLUST_A CLUST_B CLUST_C Pooled tables POOL_A POOL_B

Physical view

Database tables

SAP AG 2005, SAP Customer Program Optimization / 106

53

Cluster Tables

Cluster table TABA A B C D TIMESTAMP Table cluster TABAB Key fields PAGELG

A A

B B

0 1

C G

D H

E I

F J

Cluster table TABB A A B B E H F I G J Cluster key

VARDATA field PAGNO Structure description of VARDATA field

Key fields
SAP AG 2005, SAP Customer Program Optimization / 107

Pooled Tables

Pooled table TABA DATALN A B C D Table pool TABAB Key

TABA TABB

A E

C F

D G H I

Pooled table TABB E F G H I

TABNAME VARKEY

VARDATA

Key
SAP AG 2005, SAP Customer Program Optimization / 108

54

Pooled and Cluster Tables: Pros and Cons


Advantages
Data compression
Less memory space Less network load

Disadvantages
Limitations on database functionalities
No views or ABAP JOINs No secondary indexes No GROUP BY, ORDER BY, ...

Fewer tables and table fields Fewer different SQL statements Less load on database dictionary and database buffer Simpler administration For cluster tables
Fewer database accesses

No native SQL No table appends For cluster tables


Limited selection on clusterkey fields

For pooled tables


Longer keys than necessary

SAP AG 2005, SAP Customer Program Optimization / 109

Selective Access to Cluster Tables

SELECT bukrs belnr gjahr kunnr FROM bseg INTO TABLE g_itab_bsid WHERE bukrs = 0001 AND belnr = 0000000022.

SQL statement in ABAP


SELECT MANDT, BUKRS, BELNR, GJAHR, PAGENO, TIMESTMP, PAGELG, VARDATA RFBLG FROM RFBLG WHERE MANDT = :A0 AND BUKRS = :A1 AND BELNR = :A2 ORDER BY MANDT, BUKRS, BELNR, GJAHR, PAGENO

SQL statement at DB level


SAP AG 2005, SAP Customer Program Optimization / 110

55

Unselective Access on Cluster Tables

SELECT bukrs belnr ... FROM bseg bseg INTO TABLE g_itab_bsid WHERE kunnr = '0000000100'.

SELECT MANDT, BUKRS, .. RFBLG FROM RFBLG WHERE MANDT = :A0 ORDER BY MANDT, BUKRS, ...

Unselective access
SELECT bukrs belnr FROM bsid INTO TABLE g_itab_bsid WHERE kunnr = '0000000100'. SELECT BUKRS, MANDT, .. FROM BSID WHERE MANDT = :A0 KUNNR AND KUNNR = :A1.

Selective access
SAP AG 2005, SAP Customer Program Optimization / 111

Selective Access on Pooled Tables


SELECT kappl kschl vkorg vtweg matnr FROM aa005 INTO TABLE g_itab_aa005 WHERE kappl = CS AND kschl = SAPZ AND vkorg = 0001 AND vtweg = 01 AND kunnr = 0000000009 AND matnr = 000000000000000027.

SQL statement in ABAP


SELECT TABNAME , VARKEY , DATALN , VARDATA FROM KAPOL KAPOL WHERE TABNAME = :A0 AND VARKEY LIKE :A1 TABNAME VARKEY ORDER BY TABNAME , VARKEY

SQL statement at DB level


SAP AG 2005, SAP Customer Program Optimization / 112

56

Unselective Access on Pooled Tables

SELECT kappl kschl vkorg .. FROM aa005 INTO TABLE g_itab_aa005 WHERE matnr = matnr 000000000000000027.

SELECT TABNAME, VARKEY .. FROM KAPOL KAPOL WHERE TABNAME = :A0 AND TABNAME VARKEY VARKEY LIKE :A1 ORDER BY TABNAME , ..

Unselective access
AA005 fully buffered on application server <=> Repeated database read is not necessary Remove AA05 from table pool KAPOL and create index for MATNR <=> Efficient data read is possible

Selective access
SAP AG 2005, SAP Customer Program Optimization / 113

Database Access Optimization


Suitable Access Path General Methods Accessing Individual Tables Accessing Several Tables Accessing Clustered and Pooled Tables R/3 Table Buffering

57

R/3 Table Buffering: Introduction (1)

...
SAP GUI

...
SAP GUI SAP GUI

...

...

SAP GUI

Application server A R/3 buffer

Application server B

Table buffer
Program buffer R/3 buffer Dictionary buffer Number range buffer Roll and paging buffers

...

Network

Database Management System

DBMS processes

Database buffer

Database

SAP AG 2005, SAP Customer Program Optimization / 115

R/3 Table Buffering: Introduction (2)

Application server A
ABAP program

SELECT * FROM VVBAK WHERE ...

0.2 - 6 ms

Database interface

Table buffer

8 - 600 ms
Network

DBMS

DBMS processes

Database buffer

Database

SAP AG 2005, SAP Customer Program Optimization / 116

58

Types of Buffering

Full buffering (100%)

Generic buffering with 1 key field

Generic buffering with 2 key fields


key1 key2
A A B B A A B B B C C D A A A B B C C C D D D

Single-record buffering
key1 key2
A A B B B A A A A B B B C C D A A A B B C C C C D D D D

key3
2 4 1 3 5 1 3 6 8 1 2 3 0 3 5 2 3 6 2 4 2 3 5 8 1 2 3 4

data

key3

data

key1
001 001 001 001 002 002 002 002 002 002 003 003 003 003 003 003 003 003

key2

key3

data

key1

key2

key3

data

001 001 001 001 002 002 002 002 002 002 002 002 003 003 003 003 003 003 003 003 003 003 003

001 001 001 001 001 002 002 002 002 002 002 002 002 002 002 003 003 003 003 003 003 003 003 003 003 003 003 003

SAP AG 2005, SAP Customer Program Optimization / 117

Buffer Synchronization
Buffer synchronization every 1 - 2 minutes
Application server A UPDATE TCURR... Table buffer DB interface Application server B

...
DB interface DB interface

Table buffer

1
INSERT DDLOG Network

3
SELECT DDLOG

Database management system

2
DDLOG

Database

SAP AG 2005, SAP Customer Program Optimization / 118

59

Degree of Invalidation

In work area mode (single record processing)


Full buffering: All records Generic buffering: Generic area Single record buffering: Changed single record

In set mode (mass processing)


All records regardless of type of buffering

SAP AG 2005, SAP Customer Program Optimization / 119

SQL Statements that Bypass the Buffer (1)


Native SQL Subqueries, ABAP JOINs) SELECT ... BYPASSING BUFFER SELECT FOR UPDATE Aggregate functions (COUNT, MIN, MAX, SUM, AVG) SELECT DISTINCT WHERE clause with "IS NULL" ORDER BY, GROUP BY (HAVING) For tables with single record buffering: All SQL statements except SELECT SINGLE ... For tables with generic buffering: All SQL statements except SELECT * .. if WHERE clause is field = value for all fields included in generic area
SAP AG 2005, SAP Customer Program Optimization / 120

60

SQL Statements that Bypass the Buffer (2)

=> Database
SELECT * FROM tcurr CLIENT SPECIFIED CLIENT SPECIFIED INTO TABLE g_itab_tcurr WHERE kurst = 'EURO'.

=>Table buffer
SELECT * FROM tcurr INTO TABLE g_itab_tcurr WHERE kurst = 'EURO'.

Generic buffering
=> Database
SELECT * FROM t100 INTO TABLE g_itab_t100 WHERE sprsl = 'D' AND arbgb = 'BC490' AND msgnr = '050'.

=>Table buffer
SELECT SINGLE * FROM t100 INTO g_wa_t100 WHERE sprsl = 'D' AND arbgb = 'BC490' AND msgnr = '050'.

Single record buffering


SAP AG 2005, SAP Customer Program Optimization / 121

Buffering Strategy: Technical Criteria

Technical criteria
Small, usually < 1 MB Often read, but seldom changed Temporary data inconsistency acceptable Access mainly from key fields

Note:
Buffer tables > 10 MB in exceptional cases only Table buffers cannot be accessed via secondary indexes Check available memory space before buffering more tables

SAP AG 2005, SAP Customer Program Optimization / 122

61

Buffering Strategy: Semantic Criteria

Do not buffer transaction data


Tables too big Tables changed frequently

Avoid buffering master data


Tables too big Different access paths to data

Buffer Customizing data


Tables small Few changes to tables

SAP AG 2005, SAP Customer Program Optimization / 123

Internal Table Optimization


Definition and Properties Efficient composition of internal tables Efficient access to internal tables

62

Internal Table Attributes

SAP AG 2005, SAP Customer Program Optimization / 125

Table Types
ANY TABLE INDEX TABLE STANDARD TABLE HASHED TABLE

SORTED TABLE

Key access

Table scan

Binary search

Hash function

Access costs for n entries Access using

increase linearly
O(n)

increase logarithmically O(log n)

are constant O(1)

n
mainly index mainly key UNIQUE | NON-UNIQUE only key UNIQUE

Uniqueness

NON-UNIQUE

SAP AG 2005, SAP Customer Program Optimization / 126

63

Defining Internal Tables

SAP AG 2005, SAP Customer Program Optimization / 127

Reading Single Records

SAP AG 2005, SAP Customer Program Optimization / 128

64

Access Costs: Single Record Access via Key


LOOP AT outer_tab INTO wa_out. LOOP WITH TABLE KEY READ TABLE inner_tab INTO wa_in WITH TABLE KEY ... ENDLOOP.
+0 6

Time in microseconds

1E

+0 5

1E

Loop costs depending on the internal table type

10

10

10

00

10

00

10

20

40

80

160

320

640

1280

Number of table entries in both tables: n


5 Standard Sorted Hashed 52 44 41 10 121 87 78 20 335 177 151 40 1081 391 305 80 3887 862 613 160 14999 1895 1249 320 4048 2497 640 8587 5080 1280 18136 10537 59700 238214 951704

SAP AG 2005, SAP Customer Program Optimization / 129

Hashed Tables

Single record access with full specification of the primary key


hash_vbak:

MANDT VBELN LOOP AT sort_vbap INTO wa_vbap.

...

AT NEW wa_vbap-vbeln. READ TABLE hash_vbak INTO wa_vbak WITH TABLE KEY READ TABLE mandt = sy-mandt vbeln = wa_vbap-vbeln. WRITE: / wa_vbak-vbeln, wa_vbak-erdat. ENDAT. WRITE: /10 wa_vbap-posnr, ... . ENDLOOP.

SAP AG 2005, SAP Customer Program Optimization / 130

65

Sorted Tables

Mass processing with partial specification of the key


- Try to include as many key fields in the WHERE condition as possible; starting from the left key field, and without skipping a key field.
sort_vbpa:

LOOP ... WHERE DELETE ... WHERE MODIFY ... WHERE

MANDT VBELN POSNR PARVW

LOOP AT sort_vbpa INTO wa_vbpa WHERE mandt = sy-mandt AND parvw = 'WE'. ENDLOOP.

LOOP AT sort_vbpa INTO wa_vbpa WHERE mandt = sy-mandt AND vbeln = '0000000900'. ENDLOOP. DELETE TABLE sort_vbpa WHERE mandt = sy-mandt AND vbeln = '0000000900'.

SAP AG 2005, SAP Customer Program Optimization / 131

Standard Tables
Flexible access
for example, using various keys

PARAMETERS:

p_ernam TYPE vbak-ernam, p_vdatu TYPE vbak-vdatu.

SORT SORT stan_vbak BY ernam. READ TABLE stan_vbak WITH KEY ernam = p_ernam INTO wa READ BINARY SEARCH. IF sy-subrc NE 0. ... ENDIF. SORT stan_vbak BY erdat vdatu. SORT LOOP LOOP AT stan_vbak INTO wa WHERE erdat = sy-datu AND vdatu = p_vdatu. IF wa-erdat NE sy-datu OR wa-vdatu NE p_vdatu. EXIT. ENDIF. ENDLOOP.
SAP AG 2005, SAP Customer Program Optimization / 132

66

Internal Table Optimization


Definition and Properties Efficient composition of internal tables Efficient access to internal tables

Array Fetch

SAP AG 2005, SAP Customer Program Optimization / 134

67

Inserting Internal Tables

SAP AG 2005, SAP Customer Program Optimization / 135

Filling an Internal Table with Cumulative Values

SAP AG 2005, SAP Customer Program Optimization / 136

68

Loading Unique Internal Tables

SAP AG 2005, SAP Customer Program Optimization / 137

Internal Table Optimization


Definition and Properties Efficient composition of internal tables Efficient access to internal tables

69

Limited Mass Data Access

SAP AG 2005, SAP Customer Program Optimization / 139

Selective Field Transport

SAP AG 2005, SAP Customer Program Optimization / 140

70

Using Field Symbols

SAP AG 2005, SAP Customer Program Optimization / 141

Access on Tables without Field Transfer

SAP AG 2005, SAP Customer Program Optimization / 142

71

Reading Single Records

READ TABLE ... INDEX

...

If possible, use index operations (not possible


with hashed tables)

READ TABLE ... FROM <WA>. READ TABLE ... WITH TABLE KEY ... WITH TABLE KEY ...

If you can fully specify the primary key, use FROM or WITH TABLE KEY

READ TABLE ... BINARY SEARCH.

Use BINARY SEARCH for standard tables (after sorting)

SAP AG 2005, SAP Customer Program Optimization / 143

Reading Single Records: Example


WA wa
400 0000000110

i_vbak
mandt 400 400 400 400 400 vbeln 0000000100 0000000110 0000000120 0000000130 0000001000

wa-mandt = sy-mandt. wa-vbeln = 0000000110'. READ TABLE i_vbak FROM wa INTO wa. * or READ TABLE i_vbak WITH TABLE KEY mandt = sy-mandt vbeln = 0000000110 INTO wa.

READ TABLE i_vbak WITH KEY mandt = sy-mandt vbeln = 0000000110 INTO wa.

* for standard tables: SORT i_vbak BY mandt vbeln. READ TABLE i_vbak INTO wa WITH KEY mandt = sy-mandt vbeln = 0000000110 BINARY SEARCH BINARY SEARCH .

SAP AG 2005, SAP Customer Program Optimization / 144

72

Mass Processing using a WHERE Clause

Only access the table lines you really need

LOOP AT <itab> ... LOOP AT MODIFY MODIFY <itab> ... DELETE DELETE <itab> ...

WHERE ... WHERE ... WHERE ...


SELECT-OPTIONS: s_vbeln FOR vbak-vbeln. LOOP AT stan_vbap INTO wa WHERE mandt = sy-mandt AND vbeln IN s_vbeln. ENDLOOP.

SELECT-OPTIONS: s_vbeln FOR vbak-vbeln. LOOP AT stan_vbap INTO wa. IF wa-vbeln IN s_vbeln. * ... ENDIF. ENDLOOP.

SAP AG 2005, SAP Customer Program Optimization / 145

Mass Processing using an Index Interval


Only access the table lines you really need FROM LOOP ... FROM ... TO ... . APPEND ... FROM ... TO ... . INSERT ... FROM ... TO ... . DELETE ... FROM ... TO ... . TO
LOOP AT sort_vbak ASSIGNING <fs_vbak>. LOOP AT sort_vbap ... WHERE VBELN = <fs_vbak>-vbeln. ENDLOOP. ENDLOOP.

Sort_vbak 1 2

Sort_vbak
1 2 3 4 5

index 1 2

1 2 3 4 .. 10

index = 1. LOOP AT sort_vbak ASSIGNING <fs_vbak>. LOOP AT sort_vbap ASSIGNING <fs_vbap> FROM index. FROM index. IF <fs_vbap>-vbeln NE <fs_vbak>-vbeln. index = sy-tabix. index EXIT. ENDIF. ENDLOOP. ENDLOOP.

...

SAP AG 2005, SAP Customer Program Optimization / 146

73

Vous aimerez peut-être aussi