Upload
warsho-stenlay
View
166
Download
4
Embed Size (px)
Citation preview
1
PerformanceOptimizationWorkshop
Kim, Ji Hoon
SAP - Active Global Support
February, 2008
SAP AG 2005, SAP Customer Program Optimization / 2
Agenda
Analyzing Tools
Database Access Optimization
Internal Table Optimization
Case Study
2
Analyzing Tools
SQL performance trace
ABAP runtime analysis
Analyzing transaction steps
Performance Optimization Examples in SAP system
SAP AG 2005, SAP Customer Program Optimization / 4
Analyzing Transaction Steps: Overview
Database serverNetwork
NetworkApplication server
Presentation server
DispatcherQueue
Databasebuffer
Roll fileRoll file
R/3 databaseinterface
User context
. . .
R/3 work process
Dispatcher
R/3 table buffer
Databaseprocess
Roll buffer4
12
23 13
1
5
6 1011
7
9
8
3
SAP AG 2005, SAP Customer Program Optimization / 5
Analyzing Transaction Steps: R/3 Workload Monitor
SAP AG 2005, SAP Customer Program Optimization / 6
Analyzing Transaction Steps: Response Time
4
Analyzing Tools
SQL performance trace
ABAP runtime analysis
Analyzing transaction steps
Performance Optimization Examples in SAP system
SAP AG 2005, SAP Customer Program Optimization / 8
SQL Performance Trace: Initial Screen
5
SAP AG 2005, SAP Customer Program Optimization / 9
SQL Performance Trace: SQL Trace Overview
Application server
R/3 tablebuffering
SELECT <field list> FROM vvbakWHERE ...
R/3 databaseinterface
ABAP Program
Database server
Databaseprocess
Databasebuffer
data records
Data-basefiles
SQLTraceFile
SAP AG 2005, SAP Customer Program Optimization / 10
SQL Performance Trace: Trace List
6
SAP AG 2005, SAP Customer Program Optimization / 11
SQL Performance Trace: Database Operations
Applicationserver
SELECT Fieldlist FROM vvbakWHERE vbeln = ‘0000000100’.
ABAP Program
Databaseserver
SAP cursor cache DB cursor available,no or yes?
Data bufferDB SQL cacheSELECT mandt ...FROM VVBAKWHERE vbeln = :A1
Shared memory of the database server
DB cursor cache
A1
PREPAREin trace
OPENin trace
FETCHin trace
REOPENin trace
FETCHin trace
Database InterfaceDatarecords
SAP AG 2005, SAP Customer Program Optimization / 12
SQL Performance Trace: Explain SQL
SQL Statement
SELECT"VBELN" , "AUART"
FROM"VVBAK"
WHERE"MANDT" = :A0 AND "VBELN" BETWEEN :A1 AND :A2
Execution Plan
SELECT STATEMENT ( Estimated Costs = 3 , Estimated #Rows = 3 )
TABLE ACCESS BY INDEX ROWID VVBAK
INDEX RANGE SCAN VVBAK~0
Explain with hint... Optimizer Trace Analyze...
System HelpShow Execution Plan for SQL Statement
Table VVBAK
Last statistics date 01.09.1999Analyze Method ComputeNumber of rows 1.000Number of blocks allocated 626Number of empty blocks 15Average space 7.328Chain count 0Average row length 454
UNIQUE Index VVBAK~0
Column Name #Distinct
MANDT 1VBELN 1.000
Index Statistics Analyze...
Table and Index Info for VVBAK
1
2
34
5
7
SAP AG 2005, SAP Customer Program Optimization / 13
SQL Performance Trace: ABAP Display
Transaction = PID = 9551 Ptype = DIA Client = 100 User
Duration Object name Oper Rec RC Statement
41 VVBAP REOPEN 0 0 SELECT WHERE "MANDT" = '100' AND "VBEL805 VVBAP FETCH 5 140341 VVBAP REOPEN 0 0 SELECT WHERE "MANDT" = '100' AND "VBEL785 VVBAP FETCH 5 140341 VVBAP REOPEN 0 0 SELECT WHERE "MANDT" = '100' AND "VBEL788 VVBAP FETCH 5 140340 VVBAK REOPEN 0 0 SELECT WHERE "MANDT" = '100' AND "VBEL
1.061 VVBAK FETCH 10 14031.327 VVBAP PREPARE 0 0 SELECT WHERE "MANDT" = :A0 AND "VBELN"
85 VVBAP OPEN 0 0 SELECT WHERE "MANDT" = '100' AND "VBEL6.919 VVBAP FETCH 49 1403826 VBAK_VBAP PREPARE 0 0 SELECT WHERE "MANDT" = :A0 AND "VBELN"41 VBAK_VBAP OPEN 0 0 SELECT WHERE "MANDT" = '100' AND "VBEL
5.924 VBAK_VBAP FETCH 49 1403800 VVBAK PREPARE 0 0 SELECT WHERE ( T_01 . "MANDT" = :A0 A46 VVBAK OPEN 0 0 SELECT WHERE ( T_01 . "MANDT" = '100'
5.704 VVBAK FETCH 49 1403217 VVBAK REOPEN 0 0 SELECT WHERE "MANDT" = '100' AND "VBEL
DDIC info Explain SQL ABAP display Extended list Replace var. Sort
Trace Edit Goto System HelpBasic SQL list - sorted by PID
119 SELECT vbeln auart FROM vvbak120 INTO CORRESPONDING FIELDS OF TABLE g_itab_vvbak121 WHERE vbeln IN g_vbeln.122123 IF sy-subrc NE 0. EXIT. ENDIF. " Necessary for the WHERE124125 SELECT vbeln posnr matnr FROM vvbap126 INTO CORRESPONDING FIELDS OF TABLE g_itab_vvbap127 FOR ALL ENTRIES IN g_itab_vvbak128 WHERE vbeln = g_itab_vvbak-vbeln.
Active line 119
Note marked line
ABAP-Editor: Display Program SAPBC490D_SQL_LOGICAL_VIEWS
SAP AG 2005, SAP Customer Program Optimization / 14
SQL Performance Trace: Compressed Summary
8
SAP AG 2005, SAP Customer Program Optimization / 15
SQL Performance Trace: Identical selects
Executions
422222222222
Object name
VVBAKVVBAPVVBAPVVBAPVVBAPVVBAPVVBAPVVBAPVVBAPVVBAPVVBAPVVBAP
WHERE clause
"MANDT" = '100' AND "VBELN" BETWEEN '000000000"MANDT" = '100' AND "VBELN" = '0000000001'"MANDT" = '100' AND "VBELN" = '0000000002'"MANDT" = '100' AND "VBELN" = '0000000003'"MANDT" = '100' AND "VBELN" = '0000000004'"MANDT" = '100' AND "VBELN" = '0000000005'"MANDT" = '100' AND "VBELN" = '0000000006'"MANDT" = '100' AND "VBELN" = '0000000007'"MANDT" = '100' AND "VBELN" = '0000000008'"MANDT" = '100' AND "VBELN" = '0000000009'"MANDT" = '100' AND "VBELN" = '0000000010'"MANDT" = '100' AND "VBELN" = '0000000001' OR
Continue Cancel
System HelpIdentical selects
Analyzing Tools
SQL performance trace
ABAP runtime analysis
Analyzing transaction steps
Performance Optimization Examples in SAP system
9
SAP AG 2005, SAP Customer Program Optimization / 17
ABAP Runtime Analysis: Overview
Application server
SET RUNTIME ANALYZER ONLOOP AT itab…ENDLOOPSET RUNTIME ANALYZER OFF
ABAP runtime environment
ABAPTraceFile
LocalTraceFile
The three phases of the ABAPruntime analysis:
Limiting the analysis
Obtaining the run data
Evaluating the results
SAP AG 2005, SAP Customer Program Optimization / 18
ABAP Runtime Analysis: Initial Screen
MeasurementShort descriptionIn the current user session
TransactionProgramFunction module
Execute
In parallel mode
On / Off
Limit analysisVariant DEFAULT
Standard variant
Performance fileOther file
Tips & Tricks
Runtime analysis Edit Goto Utilities Settings System HelpRuntime analysis: Initial Screen
Runs the object in thecurrent user session
"On" causes thedata to be recordedin a separate user
session
10
SAP AG 2005, SAP Customer Program Optimization / 19
ABAP Runtime Analysis: Aggregation
Program part statements Duration & type
Maximum size of fileMaximum execution time
FullPer calling positionNone
Aggregation
10001800
KBSeconds
No aggregationProgram part XStatement YDO 1000 TIMESProgram part X => 1001 EntriesStatement Y => 1001 Entries
ENDDO
Full aggregationProgram part XStatement YDO 1000 TIMESProgram part X => 1 EntryStatement Y => 1 Entry
ENDDO
Full aggregationProgram part XStatement YDO 1000 TIMESProgram part X => 1 EntryStatement Y => 1 Entry
ENDDO
Aggregation per calling itemProgram part XStatement YDO 1000 TIMESProgram part X => 2 EntriesStatement Y => 2 Entries
ENDDO
Aggregation per calling itemProgram part XStatement YDO 1000 TIMESProgram part X => 2 EntriesStatement Y => 2 Entries
ENDDO
SAP AG 2005, SAP Customer Program Optimization / 20
ABAP Runtime Analysis: Evaluating the Results
Detailsview
Results displayscreen
Initialscreen
ABAPDBR/3
Statistics records:ConversionsLoaded programs
Calls Number Gross ...Runtime analysis 1 80,610LOOP 3 20,113...
• Select data file• Save data file locally• Evaluate the data
11
SAP AG 2005, SAP Customer Program Optimization / 21
ABAP Runtime Analysis: Evaluating the Results (2)
SAP AG 2005, SAP Customer Program Optimization / 22
ABAP Runtime Analysis: Evaluating the Results (3)
SELECT
PERFORM
READ TABLE
Gross Net
.
.
.
.
.
.
.
.
.
.
.
.
.
.
12
Analyzing Tools
SQL performance trace
ABAP runtime analysis
Analyzing transaction steps
Performance Optimization Examples in SAP system
SAP AG 2005, SAP Customer Program Optimization / 24
Performance Optimization Example in SAP system (1)
In any ABAP editor transactions (SE37, SE38, SE24, SE80, …)Menu PATH :
13
SAP AG 2005, SAP Customer Program Optimization / 25
Performance Optimization Example in SAP system (2)
SAP AG 2005, SAP Customer Program Optimization / 26
Single transaction analysis (ABAP + SQL Trace)
Transaction Code : ST12
14
SAP AG 2005, SAP Customer Program Optimization / 27
Memory Analysis in ABAP debugger
Database Access OptimizationUnsuitable Access Path
Introduction to DB Indexes
Introduction to DB Joins
Changing the ABAP coding
Changing the Index Design
15
SAP AG 2005, SAP Customer Program Optimization / 29
Overview: Cost-Based Database Optimizer
Cost-baseddatabaseoptimizer
ROWID MAND T VBELNPOSNR MATN R...
...4533 2 001
0000 132 4700030008 15...
...
ROWID MAND T VBELNPOSNR MATN R...
...4433 9 001
0000 001 2200030047 11...
...
ROWID MAND T VBELNPOSNR MATN R...
...4689 4 002
0000 965 800120001 23...
...
...
ROWID MAND T VBELNPOSNR MATN R...
...4587 6 001
0000 001 2200130012 34...
...
Full table scan
Estimated costs
ROWID MANDT VBELNERDATERZETERNAM...
...65872 0010000123
...
Possible access pathsTable and indexstatistics
...
Index access
SELECT STATEMENT
VVBAKTABLE ACCESS FULL
SELECT STATEMENT
INDEX UNIQUE SCAN VVBAK~0
SELECT STATEMENT
INDEX RANGE SCAN VVBAK~0
SAP AG 2005, SAP Customer Program Optimization / 30
Database
DBMS
Data bufferData buffer
DB SQL Cache
Database files
...
Database servermemory
R/3 workprocess
SELECT *FROM MARA
WHERE ...Logical read accesses
Physical read accesses
Data buffer
SQL statementswith access strategy
Data blocks (e.g. 8 KB blocks)
DB workprocess
Overview: DB SQL Cache
16
SAP AG 2005, SAP Customer Program Optimization / 31
DB SQL Cache Analysis: Overview (2)
Appl.Server
Database system
ABAP program
R/3 DB interface
DBMSprocess
Database
Database buffer
Appl.Server
Database system
ABAP program
R/3 DB interface
DBMSprocess
Database
Database bufferLogical readaccesses
Physical readaccesses
Transferreddata records
Few recordsMany records
Many blocks Many blocks
Suitable access path No suitable access path
SAP AG 2005, SAP Customer Program Optimization / 32
Search range(area of tablethat needs tobe searchedfor required
records)
Data recordscorresponding toWHERE clause
MANDT VBELN OBJNR ERNAM
001 000013245 654875454 ERNIE001 000013246 697935435 FRED001 000013247 673225747 JAMES001 000013248 216356543 GUS001 000013249 687535435 ERNIE001 000013250 258674802 KURT001 000113245 983231365 MAX001 000113251 522112486 BERT001 000113252 325348654 BERT002 000013249 241567685 HUGO002 000013250 968351332 FRED002 000013251 874352321 MAX002 000013252 544546546 GUS
Overview: Unsuitable Access Path
Narrowingthe search
17
SAP AG 2005, SAP Customer Program Optimization / 33
MANDT VBELN OBJNR ERNAM
001 000013245 654875454 ERNIE001 000013246 697935435 FRED001 000013247 673225747 JAMES001 000013248 216356543 GUSS001 000013249 687535435 ERNIE001 000013250 258674802 KURT001 000113245 983231365 MAX001 000113251 522112486 BERT001 000113252 325348654 BERT002 000013249 241567685 HUGO002 000013250 968351332 FRED002 000013251 874352321 MAX002 000013252 544546546 GUSS
Reducingrows to betransferred
Reducingcolumnsto betransferred
Data recordscorresponding toWHERE clause
Search range(area of table that
needs to besearched for
required records)
Columnscorresponding toSELECT clause
Overview: Suitable Access Path (1)
SAP AG 2005, SAP Customer Program Optimization / 34
FETCH
FETCH
FETCH
FETCH
MANDT VBELN OBJNR ERNAM
001 000013245 654875454 ERNIE001 000013246 697935435 FRED001 000013247 673225747 JAMES001 000013248 216356543 GUSS001 000013249 687535435 ERNIE001 000013250 258674802 KURT001 000113245 983231365 MAX001 000113251 522112486 BERT001 000113252 325348654 BERT002 000013249 241567685 HUGO002 000013250 968351332 FRED002 000013251 874352321 MAX002 000013252 544546546 GUSS
:
Reducing datatransfer
Data transfer toapplication server
Overview: Suitable Access Path (2)
18
SAP AG 2005, SAP Customer Program Optimization / 35
Introduction to DB Index : Overview (1)
SAP AG 2005, SAP Customer Program Optimization / 36
Introduction to DB Index : Overview (2)
19
SAP AG 2005, SAP Customer Program Optimization / 37
Introduction to DB Index : Index Unique Scan
’
SAP AG 2005, SAP Customer Program Optimization / 38
Introduction to DB Index : Index Range Scan (1)
20
SAP AG 2005, SAP Customer Program Optimization / 39
Introduction to DB Index : Index Range Scan (2)
POSNR
SAP AG 2005, SAP Customer Program Optimization / 40
Introduction to DB Index : Overview (3)
21
SAP AG 2005, SAP Customer Program Optimization / 41
Introduction to DB Index : Concatenation
SAP AG 2005, SAP Customer Program Optimization / 42
Full Table Scan
22
SAP AG 2005, SAP Customer Program Optimization / 43
Table Access Strategies
SAP AG 2005, SAP Customer Program Optimization / 44
DB2 example : Index Unique Scan
23
SAP AG 2005, SAP Customer Program Optimization / 45
Oracle example : Index Unique Scan
SAP AG 2005, SAP Customer Program Optimization / 46
DB2 example : Index Range Scan
24
SAP AG 2005, SAP Customer Program Optimization / 47
Oracle example : Index Range Scan
SAP AG 2005, SAP Customer Program Optimization / 48
DB2 example : Full Table Scan
25
SAP AG 2005, SAP Customer Program Optimization / 49
Oracle example : Full Table Scan
SAP AG 2005, SAP Customer Program Optimization / 50
DB2 example : Concatenation
26
SAP AG 2005, SAP Customer Program Optimization / 51
Oracle example : Concatenation
Database Access OptimizationUnsuitable Access Path
Introduction to DB Indexes
Introduction to DB Joins
Changing the ABAP coding
Changing the Index Design
27
SAP AG 2005, SAP Customer Program Optimization / 53
Introduction to DB Joins : Overview
T1K1 F1 F2
T2K1 F3 F4
DBMS
T1.K1 T1.F1 T1.F2 T2.F3 T2.F4
Application
T1.K1 T1.F1 T1.F2 T2.F3 T2.F4T1.K1 T1.F1 T1.F2 T2.F3 T2.F4T1.K1 T1.F1 T1.F2 T2.F3 T2.F4T1.K1 T1.F1 T1.F2 T2.F3 T2.F4 Resulting set
Database
SELECT T1~K1 T1~K2 T1~F2 T2~F3 T2~F4FROM tab1 AS T1 INNER JOIN tab2 AS T2ON T1~K1 = T2~K1WHERE T1~F2 = value.ON
INNER JOINSELECT
SAP AG 2005, SAP Customer Program Optimization / 54
Introduction to DB Joins : Nested Loop
MANDT VBELN OBJNR ...001 0000120 0000110001 0000121001 0000122 0000110001 0000123 0000777001 0000124 0000777001 0000125 0000777...
MANDT VBELN OBJNR ...001 0000120 0000110001 0000121001 0000122 0000110001 0000123 0000777001 0000124 0000777001 0000125 0000777...
VVBAK VVBUK
MANDT VBELN ...001 0000121 ...001 0000122 ...
001 0000123 ...
...
MANDT VBELN ...001 0000121 ...001 0000122 ...
001 0000123 ...
...
Nested loop
SELECT t1~vbeln t2~vbobj INTO ...FROM vvbak AS t1 INNER JOIN vvbuk AS t2ON t1~vbeln = t2~vbelnWHERE t1~objnr = '0000777'.
28
SAP AG 2005, SAP Customer Program Optimization / 55
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 / 56
DB2 Nested Loop
29
SAP AG 2005, SAP Customer Program Optimization / 57
Oracle Nested Loop
SAP AG 2005, SAP Customer Program Optimization / 58
Introduction to DB Joins : Sort Merge Join
SELECT t1~vbeln t2~vbobj INTO ...FROM vvbap AS t1 INNER JOIN vvbak AS t2ON t1~vbeln = t2~vbelnWHERE t1~cuobj = ’12345’AND t2~objnr = ‘0000777’.
MANDT VBELN OBJNR ...001 0000120 0000110001 0000121001 0000122 0000110001 0000123 0000777001 0000124 0000777001 0000125 0000777...
MANDT VBELN OBJNR ...001 0000120 0000110001 0000121001 0000122 0000110001 0000123 0000777001 0000124 0000777001 0000125 0000777...
MANDT VBELN CUOBJ ...001 0000121 1 ...001 0000122 2 ...001 0000123 3 ...001 0000123 4 ......
001 0000123 12345 ...001 0000456 12345 ...
MANDT VBELN CUOBJ ...001 0000121 1 ...001 0000122 2 ...001 0000123 3 ...001 0000123 4 ......
001 0000123 12345 ...001 0000456 12345 ...
VVBAP VVBAK
Selection andmerging of
table records
Sort merge join
30
SAP AG 2005, SAP Customer Program Optimization / 59
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 fromexisting B-tree indexes
SAP AG 2005, SAP Customer Program Optimization / 60
DB2 Sort Merge Join
31
SAP AG 2005, SAP Customer Program Optimization / 61
Oracle Sort Merge Join
SAP AG 2005, SAP Customer Program Optimization / 62
Nested loopDecide which tables to use for accessAccess to next innermost table using JOIN condition
Sort Merge JoinEvaluate WHERE clause on all relevant tablesSort according to JOIN condition
Merge resulting set
Important Access Strategies for DB Joins
32
Database Access OptimizationUnsuitable Access Path
Introduction to DB Indexes
Introduction to DB Joins
Changing the ABAP coding
Changing the Index Design
SAP AG 2005, SAP Customer Program Optimization / 64
SELECT bukrs belnr gjahrFROM bkkpfINTO TABLE g_itab_bkkpfWHERE belnr = ‘0000000100’.
ineffective use of indexineffective use of index
effective use of indexeffective use of index
SELECT bukrs belnr gjahrFROM bkkpfINTO TABLE g_itab_bkkpfWHERE bukrs = ‘0001’AND belnr = ‘0000000100’.
specific index fields: MANDT, BELNR=> gap in index search string
Index search string:‘001____0000000100’
specific index fields: MANDT, BUKRS, BELNR=> index fields specified fully
Index search string: ‘00100010000000100’WHERE bukrs
Missing WHERE Conditions (1)
33
SAP AG 2005, SAP Customer Program Optimization / 65
SELECT vbeln erdat FROM vvbakCLIENT SPECIFIEDINTO TABLE g_itab_vvbakWHERE vbeln = ‘0000000100’.
ineffective use of indexineffective use of index
effective use of indexeffective use of index
SELECT vbeln erdat FROM vvbakINTO TABLE g_itab_vvbakWHERE vbeln = ‘0000000100’.
* orSELECT vbeln erdat FROM vvbakCLIENT SPECIFIEDINTO TABLE g_itab_vvbakWHERE mandt = sy-mandtAND vbeln = ‘0000000100’.
specific index fields: VBELN=> index fields not specified
Index search string: ‘___0000000100’
specific index fields: MANDT,VBELN
=> index fields specified fullyIndex search string: ‘0010000000100’
CLIENT SPECIFIED
WHERE vbeln
CLIENT SPECIFIED
WHERE mandt
Missing WHERE Conditions (2)
SAP AG 2005, SAP Customer Program Optimization / 66
Critical Operators (NOT and <>)
SELECT vbeln bstnk kunnr FROM vvbakINTO TABLE g_itab_vvbakWHERE kunnr = '0000000050'AND NOT faksk = '00'.
SELECT vbeln bstnk kunnr FROM vvbakINTO TABLE g_itab_vvbakWHERE kunnr = '0000000050'AND faksk IN ('01','02','03','04','05','08','09').
Negative formulationNegative formulation
Positive formulationPositive formulation
specific index fields: MANDT,KUNNR and FAKSK => ineffective
useof indexes
IN
NOT
specific index fields: MANDT,KUNNR and FAKSK => effective use
of indexes
34
SAP AG 2005, SAP Customer Program Optimization / 67
SELECT vbeln erdat erzet FROM vvbak INTO TABLE g_itab_vvbakWHERE vbeln BETWEEN '0000000001' AND '0000000005'.
SELECT vbeln erdat erzet FROM vvbak INTO TABLE g_itab_vvbakWHERE vbeln IN ('0000000001', '0000000002', '0000000003',
'0000000004', '0000000005').
BETWEEN operatorBETWEEN operator
IN operatorIN operatorReplace WHERE field LIKE value
withWHERE field = value
OmitWHERE field LIKE '%'
BETWEEN
IN
Critical Operators (BETWEEN)
SAP AG 2005, SAP Customer Program Optimization / 68
Sort at Database Level or in ABAP?
35
Database Access OptimizationUnsuitable Access Path
Introduction to DB Indexes
Introduction to DB Joins
Changing the ABAP coding
Changing the Index Design
SAP AG 2005, SAP Customer Program Optimization / 70
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 tablesTables with D010, D020, DD* or NAST
Prerequisites for Changing the Index Design
36
SAP AG 2005, SAP Customer Program Optimization / 71
General rulesPrefer disjunctive indexes
No unintentionally used indexes
As few indexes as possible per table (as few as possible, but as many asnecessary)
Selection of index fieldsAs 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 indexesunless recommended by SAP (see SAP Note System)
Rules for Changing the Index Design
SAP AG 2005, SAP Customer Program Optimization / 72
Selectivity Analysis: Semantic Typing
37
SAP AG 2005, SAP Customer Program Optimization / 73
Selectivity Analysis: Distinct Values
30.09.1999 14:46:44 KP6 pwdf0024VVBAK
Data from DBA_TAB_COLUMNS
DatabaseLast Sample Distinct
Column Data type analyzed* size* values*
MANDT VARCHAR2 30.09.1999 220,780 1VBELN VARCHAR2 30.09.1999 220,780 220,780ERDAT VARCHAR2 30.09.1999 220,780 856
Back Table <-> Indices Extents Table columns Detailed analysis History Sort
Database analysis Goto Monitor System HelpTables and Indexes Columns of Table VVBAK
ERZET VARCHAR2 30.09.1999 220,780 187,023ERNAM VARCHAR2 30.09.1999 220,780 73ANGDT VARCHAR2 30.09.1999 220,780 435BNDDT VARCHAR2 30.09.1999 220,780 267
Updated by “ORACLE STATISTICS UPDATE”
SAP AG 2005, SAP Customer Program Optimization / 74
Selectivity Analysis: Histograms (1)
38
SAP AG 2005, SAP Customer Program Optimization / 75
DB05: Selectivity Analysis: Histograms (2)
Date/time of analysis: 02.08.1999 17:45:28Analyzed table: COSSTotal number of rows: 635,336 rowsRequested bytes per row: 31 bytes per rowRequested bytes to be buffered 100%: 19,695,416 bytesCurrent buffering mode: no buffering
USPOB
Key fields
Rows pergeneric key
GJAHRWRTTP
VERSN
Number of areas that are specified by generic key and contain the given ...
Distinctvalues
11- 100
1- 10
101- 1.000
10.001- 100.000
100.001- 1.000.000
4,5067,1729,195
16,607
3,2345,7157,195
16,587
1,0111,0721,4871,536
210333457428
058
21008
Compare number ofdistinct values with totalnumber of records
16,587 combinations ofindex fields return between1 and 10 records
SAP AG 2005, SAP Customer Program Optimization / 76
Date/time of analysis: 02.08.1999 17:45:28Analyzed table: COSSTotal number of rows: 635,336 rowsRequested bytes per row: 31 bytes per rowRequested bytes to be buffered 100%: 19,695,416 bytesCurrent buffering mode: no buffering
USPOBKey fields
Rows pergeneric key
GJAHRWRTTP
VERSN
Number of areas that are specified by generic key and contain the given ...
Distinctvalues
11- 100
1- 10
101- 1,000
10,001- 100,000
100,001- 1,000,000
4,5067,1729,195
16,607
3,2345,7157,195
16,587
1,0111,0721,4871,536
210333457428
058
21008
8 combinations of index fields returnbetween 10,001 and 100,000 records
DB05: Selectivity Analysis: Histograms (3)
39
SAP AG 2005, SAP Customer Program Optimization / 77
Clarify table usageUse where-used list from ABAP Dictionary
Perform object-related DB SQL cache analysis beforechanging index design
DB SQL cache => restrict to tables and views
Retain all SQL statements with access strategy, where the tablesor views are affected
Change index designReset DB SQL cache
Perform object-related DB SQL cache analysis after changingindex design
DB SQL cache => restrict to tables and views
Retain all SQL statements with access strategy, where the tablesor views are affected
Interference Analysis (1)
SAP AG 2005, SAP Customer Program Optimization / 78
Create
Database table VVBAKView
Data type
DomainSearch helpLock object
Display Change
Dictionary object Edit Goto Utilities Environment System HelpDictionary: Initial Screen
DB table VVBAK
Used in
ProgramsScreensLogical databasesFunction module interfaces
DomainsStructuresDB tablesTable typesViewsEntity typesLock objectsSearch helps
Classes (definition)Classes (implementation)Interfaces
Test cases
In background Search area
Where-Used List Database table
Interference Analysis (2)
40
SAP AG 2005, SAP Customer Program Optimization / 79
Database Performance: Shared SQL
??
1999 10:12:21 Shared Cursor Cache
Reset Since DB start Select table Explain
101,002
TotalExecutions
34,0045,340
702
DiskReads
BufferGets
Reads/Executions
Gets/Execution
Records/Execution
4,5067,145
92,195107
0.00.2
17.30.2
45,678109,072
7,041,4871,536
0.53.2
1,318.62.2
0.86.00.41.0
546 16,347 29.9 231,536 424.0
SQLTextSELECT * FROMSELECT “VBELN”SELECT “MANDT”UPDATE“VVBAK”SELECT * FROM47.5
SELECT STATEMENT
TABLE ACESS BY INDEX ROWID VVBAK
INDEX RANGE SCAN VVBAK ~ 0
Interference Analysis (3)
Database Access OptimizationSuitable Access Path
General Methods
Accessing Individual Tables
Accessing Several Tables
Accessing Clustered and Pooled Tables
R/3 Table Buffering
41
SAP AG 2005, SAP Customer Program Optimization / 81
SELECT ... FROM resbINTO TABLE g_itab_resb.
SELECT ... FROM resbINTO TABLE g_itab_resbWHERE matnr IN g_matnr.
Unstable WHERE clauseUnstable WHERE clause
Stable WHERE clauseStable WHERE clause
SELECT ... FROM resbINTO TABLE g_itab_resbWHERE matnr IN g_matnrAND kzear IS INITIAL.
Missing WHERE clauseMissing WHERE clause
Reducing the Records to be Transferred (1)
SAP AG 2005, SAP Customer Program Optimization / 82
SELECT vbeln erdat kunnr FROM vvbakINTO g_wa_vvbak.CHECK g_wa_vvbak-vbeln GE g_lowAND g_wa_vvbak-vbeln LE g_high.APPEND g_wa_vvbak TO g_itab_vvbak.
ENDSELECT.
WHERE conditionWHERE condition
SELECT vbeln erdat kunnr FROM vvbakINTO g_wa_vvbakWHERE vbeln BETWEEN g_low AND g_high.APPEND g_wa_vvbak TO g_itab_vvbak.
ENDSELECT.
CHECK
WHERE
CHECK conditionCHECK condition
Reducing the Records to be Transferred (2)
42
SAP AG 2005, SAP Customer Program Optimization / 83
Reducing the Columns to be Transferred
SELECT vbeln erdat kunnr FROM vvbakINTO TABLE g_itab_vvbak_fieldlistWHERE vbeln IN g_vbeln.
SELECT * FROM vvbakINTO TABLE g_itab_vvbak_allWHERE vbeln IN g_vbeln.
SET
SELECT
SELECT
SELECT field list INTO TABLESELECT field list INTO TABLE
SELECT vbeln erdat kunnr FROM vvbakINTO CORRESPONDING FIELDSOF TABLE g_itab_vvbak_fieldlistWHERE vbeln IN g_vbeln.
SELECT
SELECT field list INTO ...SELECT field list INTO ...
SELECT * INTO TABLESELECT * INTO TABLE
INTO TABLE
INTO CORRESPONDING FIELDSOF TABLE
INTO TABLE
Database Access OptimizationSuitable Access Path
General Methods
Accessing Individual Tables
Accessing Several Tables
Accessing Clustered and Pooled Tables
R/3 Table Buffering
43
SAP AG 2005, SAP Customer Program Optimization / 85
DESCRIBE TABLE g_itab_vvbak LINES lines.IF lines EQ 0.MESSAGE I051(bc490). EXIT.ENDIF.
SORT g_itab_vvbak BY kunnr.DELETE ADJACENT DUPLICATESFROM g_itab_vvbak.SELECT kunnr adrnr FROM kkna1INTO TABLE g_itab_kkna1FOR ALL ENTRIES IN g_itab_vvbakWHERE kunnr = g_itab_vvbak-kunnr.
LOOP AT g_itab_vvbak INTO g_wa_vvbak.SELECT kunnr adrnr FROM kkna1APPENDING TABLE g_itab_kkna1WHERE kunnr = g_wa_vvbak-kunnr.
ENDLOOP.
LOOP AT
APPENDING TABLE
ENDLOOP.
SELECTINTO TABLEFOR ALL ENTRIES IN
SELECT
SELECT inside LOOPSELECT inside LOOP
SELECT FOR ALL ENTRIESSELECT FOR ALL ENTRIES
Check whether driver table is empty
Eliminate duplicates
SELECT FOR ALL ENTRIES (1)
SAP AG 2005, SAP Customer Program Optimization / 86
KKNA1
KKNA1~0
SELECT STATEMENT
CONCATENATION
TABLE ACESS BY INDEX ROWID
INDEX UNIQUE SCAN
TABLE ACESS BY INDEX ROWID KKNA1
KKNA1~0
(Estimated Costs = .... )
Object Oper Rec
FETCH
FETCH
VVBAKVVBAKKKNA1
KKNA1
REOPEN
REOPEN
0100000
FETCHKKNA10
. . .
REOPENKKNA114901490149FETCHKKNA1
REOPENKKNA1
. . . . . .rsdb/max_blocking_factor
specifies numberof concatenations
SELECT"KUNNR" , "ADRNR"
FROM"KKNA1"
WHERE"MANDT" = :A0 AND "KUNNR" = :A1
OR "MANDT" = :A2 AND "KUNNR" = :A3OR "MANDT" = :A4 AND "KUNNR" = :A5OR "MANDT" = :A6 AND "KUNNR" = :A7....
INDEX UNIQUE SCAN
SELECT FOR ALL ENTRIES (2)
44
SAP AG 2005, SAP Customer Program Optimization / 87
SELECT mandt vbeln erdat erzetFROM vvbakINTO TABLE g_itab_vvbakWHERE vbeln IN g_itab_vbeln.
SELECT
IN
VVBAK
VVBAK~0
SELECT STATEMENT
CONCATENATION
TABLE ACESS BY INDEX ROWID
INDEX UNIQUE SCAN
TABLE ACESS BY INDEX ROWID VVBAK
VVBAK~0
(Estimated Costs = .... )
INDEX UNIQUE SCAN
SELECT with RANGES tableSELECT with RANGES tableSELECT
"MANDT" , "VBELN" , "ERDAT" , "ERZET"FROM"VVBAK"
WHERE"MANDT" = :A0AND “VBELN IN ( :A1 , :A2 , :A3 , …)
RANGES Tables
SAP AG 2005, SAP Customer Program Optimization / 88
Aggregate functions in ABAPAggregate functions in ABAP
Aggregate functions at DB levelAggregate functions at DB level
SELECT vbeln zmeng matnrFROM vvbapINTO TABLE g_itab_vvbapWHERE vbeln IN g_vbeln.
LOOP AT g_itab_vvbap.* Compute maximum of zmeng and minimum of matnr ...ENDLOOP.
SELECT vbeln max( zmeng ) min( matnr )FROM vvbapINTO TABLE g_itab_vvbapWHERE vbeln IN g_vbelnGROUP BY vbeln.
Aggregate Functions
45
SAP AG 2005, SAP Customer Program Optimization / 89
HAVING Clause
SAP AG 2005, SAP Customer Program Optimization / 90
Array Fetch
46
SAP AG 2005, SAP Customer Program Optimization / 91
Up to N rows
SAP AG 2005, SAP Customer Program Optimization / 92
UPDATE .. SET Field = Value
47
SAP AG 2005, SAP Customer Program Optimization / 93
Create, Change and Delete Mass Data
Database Access OptimizationSuitable Access Path
General Methods
Accessing Individual Tables
Accessing Several Tables
Accessing Clustered and Pooled Tables
R/3 Table Buffering
48
SAP AG 2005, SAP Customer Program Optimization / 95
INNER and OUTER JOIN Logic
00000000030000000003
INNERJOIN
LEFT OUTERJOIN
MANDT . . .400400
. . . . . .400
MANDT VBELN
0000000001400400
0000000001
. . .. . .400400
MANDT POSNR
0002400
0001
. . .. . .400 0002
0001400
400
VBELN
VBELN
VVBAKVVBAP
00000000010000000001
00000000030000000003
MANDT POSNR
0002400
0001
. . .. . .400 0001400
400
VBELN0000000001
0000000001
00000000020000000003
400 00020000000003. . .. . .
000000000100000000020000000003
POSNR
0000000004400
0001
0002000100020001
SAP AG 2005, SAP Customer Program Optimization / 96
Implementation in ABAP
49
SAP AG 2005, SAP Customer Program Optimization / 97
Object Oper Rec
FETCH
FETCH
VVBAKVVBAKKKNA1
KKNA1
REOPEN
REOPEN
01000
FETCHKKNA10
. . .
REOPENKKNA110101FETCHKKNA1
REOPENKKNA1
. . . . . .
SELECT STATEMENT
INDEX RANGE SCAN
(Estimated Costs = .... )
VVBAK~0
SELECT STATEMENT (Estimated Costs = .... )
KKNA1~0INDEX UNIQUE SCAN
SELECT vbeln kunnr FROM vvbakINTO (g_wa_vvbak_kkna1-vbeln, g_wa_vvbak_kkna1-kunnr)WHERE vbeln IN g_vbeln.SELECT SINGLE adrnr FROM kkna1INTO g_wa_vvbak_kkna1-adrnrWHERE kunnr EQ g_wa_vvbak_kkna1-kunnr.IF sy-subrc EQ 0.APPEND g_wa_vvbak_kkna1 TO g_itab_vvbak_kkna1.
ENDIF.ENDSELECT.
SELECT
SELECT
ENDSELECT
Nested SELECTs
SAP AG 2005, SAP Customer Program Optimization / 98
Problems with Nested SELECTs
Inefficient transferData records from inner table are transferred record byrecord
Lots of small fetches, instead of fewer andmore compact fetches
Identical SELECTsData records from inner table arere-read unnecessarily
Incorrect order of accessWhether a table is an inner or outer tableis defined in codingDynamic WHERE conditions are ignored
50
SAP AG 2005, SAP Customer Program Optimization / 99
Creating JOINs Over the Database
Possible access strategies:Nested loop
Sort merge join
Database optimizer determines order of accessAccess to outer table should be as selective as possible (selectiveWHERE conditions)
For each data record in the outer table, as few records as possibleshould exist in the inner table (selective JOIN conditions)
An index for the JOIN conditions should existon the inner table (at least)
SAP AG 2005, SAP Customer Program Optimization / 100
SELECT vbeln kunnr adrnr FROM vbak_kna1INTO TABLE g_itab_vvbak_kkna1WHERE vbeln IN g_vbeln.
Create DB viewin Dictionary
CREATE VIEWvbak_kna1 ...
SELECT STATEMENT
NESTED LOOPS
(Estimated Costs = .... )
TABLE ACESS BY INDEX ROWID
KKNA1~0
KKNA1
INDEX UNIQUE SCAN
TABLE ACESS BY INDEX ROWID
VVBAK~0
VVBAK
INDEX RANGE SCAN
SELECT FROM vbak_kna1
Database View
51
SAP AG 2005, SAP Customer Program Optimization / 101
SELECT t1~vbeln t2~kunnr t2~adrnrINTO TABLE g_itab_vvbak_kkna1FROM vvbak AS t1INNER JOIN kkna1 AS t2ON t1~kunnr = t2~kunnrWHERE t1~vbeln IN g_vbeln.
SELECT STATEMENT
NESTED LOOPS
(Estimated Costs = .... )
TABLE ACESS BY INDEX ROWID
KKNA1~0
KKNA1
INDEX UNIQUE SCAN
TABLE ACESS BY INDEX ROWID
VVBAK~0
VVBAK
INDEX RANGE SCAN
MANDT VBELN KUNNR...
001 0000120 0000100001 0000121 0000100001 0000122 0000101001 0000123 0000101001 0000124 0000102001 0000125 0000103001 0000126 0000103001 0000127 0000104
...
VVBAK
KKNA1
MANDT KUNNR ...001 0000100 ...001 0000103 ...001 0000104 ......
SELECT
INNER JOIN ASAS
ON
ABAP INNER JOIN
SAP AG 2005, SAP Customer Program Optimization / 102
SELECT STATEMENT
CONCATENATION
(Estimated Costs = .... )
TABLE ACCESS BY INDEX ROWID
KKNA1~0
KKNA1
INDEX UNIQUE SCAN
TABLE ACCESS BY INDEX ROWID
KKNA1~0
KKNA1
INDEX UNIQUE SCAN
SELECT vbeln kunnr FROM vvbak INTO TABLE g_itab_vvbakWHERE vbeln IN g_vbeln.
DESCRIBE TABLE g_itab_vvbakLINES g_lines....
SORT g_itab_kkna1 BY kunnr.DELETE ADJACENT DUPLICATESFROM g_itab_kkna1.
SELECT kunnr adrnrFROM kkna1INTO (g_wa_kkna1-kunnr, g_wa_kkna1-adrnr)FOR ALL ENTRIES IN g_itab_kkna1WHERE 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.
FOR ALL ENTRIES IN
SELECT
SELECT
DESCRIBE TABLE
DELETE ADJACENT DUPLICATES
FOR ALL ENTRIES Over Two Database Tables
52
SAP AG 2005, SAP Customer Program Optimization / 103
...SELECT kunnr adrnrINTO TABLE g_itab_vvbak_kkna1FROM kkna1WHERE kunnr IN ( SELECT DISTINCT kunnr FROM vvbak
WHERE vbeln IN g_vbeln )....
...
VVBAK~0
KKNA1~0INDEX UNIQUE SCAN
(Estimated Costs = .... )
NESTED LOOPS
SELECT STATEMENT
…
TABLE ACCESS BY INDEX ROWID
TABLE ACCESS BY INDEX ROWID
INDEX RANGE SCAN
VVBAK
KKNA1
)(WHERE
SELECT
SELECT DISTINCT
Subquery
SAP AG 2005, SAP Customer Program Optimization / 104
SELECT STATEMENT
TABLE ACESS BY INDEX ROWID
VVBAK~0INDEX RANGE SCAN
(Estimated Costs = .... )
TABLE ACESS BY INDEX ROWID
INDEX UNIQUE SCAN KKNA1~0
VVBAK
NESTED LOOPS OUTER
KKNA1
SELECT t1~vbeln t2~kunnr t2~adrnrINTO TABLE g_itab_vvbak_kkna1FROM vvbak AS t1LEFT OUTER JOIN kkna1 AS t2ON t1~kunnr = t2~kunnrWHERE t1~vbeln IN g_vbeln.
SELECT
LEFT OUTER JOIN ASAS
ON
MANDT VBELN KUNNR...
001 0000120 0000100001 0000121 0000100001 0000122 0000101001 0000123 0000101001 0000124 0000102001 0000125 0000103001 0000126 0000103001 0000127 0000104
...
VVBAK
KKNA1
MANDT KUNNR ...001 0000100 ...001 0000103 ...001 0000104 ......
ABAP OUTER JOIN
53
Database Access OptimizationSuitable Access Path
General Methods
Accessing Individual Tables
Accessing Several Tables
Accessing Clustered and Pooled Tables
R/3 Table Buffering
SAP AG 2005, SAP Customer Program Optimization / 106
Logical view
Physical view
TAB_B
Transparent tables Cluster tables Pooled tables
POOL_ACLUST_APOOL_B
Database tables
CLUST_BCLUST_C
Pooled and Cluster Tables: Introduction
54
SAP AG 2005, SAP Customer Program Optimization / 107
A C DB
A E FB
A H IB
A B
A B
0
1
PAGNO
TIMESTAMP PAGELG
C D E
G H I
Cluster table TABA
Cluster table TABB
Table clusterTABAB
Key fields
Key fields
G
J
F
J
VARDATA fieldClusterkey
Structure descriptionof VARDATA field
Cluster Tables
SAP AG 2005, SAP Customer Program Optimization / 108
A CB
E G
E
BTABA
TABB
DATALN
C D
F G
Pooled table TABA
Pooled table TABB
Table poolTABAB
Key
Key
I
H
VARDATAVARKEY
D
H
A
F
I
TABNAME
Pooled Tables
55
SAP AG 2005, SAP Customer Program Optimization / 109
Advantages Disadvantages
Data compressionLess memory space
Less network load
Fewer tables and table fields
Fewer different SQLstatements
Less load on databasedictionary and databasebuffer
Simpler administration
For cluster tablesFewer database accesses
Limitations on databasefunctionalities
No views or ABAP JOINs
No secondary indexes
No GROUP BY, ORDER BY, ...
No native SQL
No table appends
For cluster tablesLimited selection on clusterkeyfields
For pooled tablesLonger keys than necessary
Pooled and Cluster Tables: Pros and Cons
SAP AG 2005, SAP Customer Program Optimization / 110
SELECT bukrs belnr gjahr kunnrFROM bsegINTO TABLE g_itab_bsidWHERE bukrs = ‘0001’AND belnr = ‘0000000022’.
SELECT “MANDT”, “BUKRS”, “BELNR”, “GJAHR”, “PAGENO”,“TIMESTMP”, “PAGELG”, “VARDATA”FROM “RFBLG”WHERE “MANDT” = :A0AND “BUKRS” = :A1AND “BELNR” = :A2
ORDER BY “MANDT”, “BUKRS”, “BELNR”, “GJAHR”, “PAGENO”
SQL statement at DB levelSQL statement at DB level
RFBLG
bseg
SQL statement in ABAPSQL statement in ABAP
Selective Access to Cluster Tables
56
SAP AG 2005, SAP Customer Program Optimization / 111
SELECT bukrs belnr ...FROM bsegINTO TABLE g_itab_bsidWHERE kunnr = '0000000100'.
SELECT bukrs belnr …FROM bsidINTO TABLE g_itab_bsidWHERE kunnr = '0000000100'.
SELECT “BUKRS”, “MANDT”, ..FROM BSIDWHERE “MANDT” = :A0AND “KUNNR” = :A1.
Selective accessSelective access
SELECT “MANDT”, “BUKRS”, ..FROM “RFBLG”WHERE “MANDT” = :A0ORDER BY “MANDT”, “BUKRS”,...
KUNNR
BSID
RFBLG
bsid
kunnr
bseg
kunnr
Unselective accessUnselective access
Unselective Access on Cluster Tables
SAP AG 2005, SAP Customer Program Optimization / 112
SELECT kappl kschl vkorg vtweg matnr FROM aa005INTO TABLE g_itab_aa005WHERE kappl = ‘CS’ AND kschl = ‘SAPZ’AND vkorg = ‘0001’ AND vtweg = ‘01’AND kunnr = ‘0000000009’AND matnr = ‘000000000000000027’.
SELECT“TABNAME” , “VARKEY” , “DATALN” , “VARDATA”FROM“KAPOL”WHERE“TABNAME” = :A0 AND “VARKEY” LIKE :A1ORDER BY “TABNAME” , “VARKEY”
SQL statement at DB levelSQL statement at DB level
TABNAME VARKEY
KAPOL
aa005
SQL statement in ABAPSQL statement in ABAP
Selective Access on Pooled Tables
57
SAP AG 2005, SAP Customer Program Optimization / 113
SELECT kappl kschl vkorg ..FROM aa005INTO TABLE g_itab_aa005WHERE matnr =
‘000000000000000027’.
AA005 fully bufferedon application server
<=>Repeated database read
is not necessary
Selective accessSelective access
SELECT “TABNAME”, “VARKEY” ..FROM “KAPOL”WHERE “TABNAME” = :A0 AND“VARKEY” LIKE :A1ORDER BY “TABNAME” , ..
Remove AA05 from table pool KAPOLand create index for MATNR
<=>Efficient data read
is possible
aa005
matnr
KAPOLTABNAME
VARKEY
Unselective accessUnselective access
Unselective Access on Pooled Tables
Database Access OptimizationSuitable Access Path
General Methods
Accessing Individual Tables
Accessing Several Tables
Accessing Clustered and Pooled Tables
R/3 Table Buffering
58
SAP AG 2005, SAP Customer Program Optimization / 115
Application server A
SAP GUI SAP GUI
...
...
R/3 buffer
Network
DatabaseManagementSystem
DBMSprocesses
Databasebuffer
Database
Application server B
SAP GUI SAP GUI
...
...
Program bufferDictionary bufferNumber range bufferRoll and paging buffers
Table buffer
R/3 buffer...
R/3 Table Buffering: Introduction (1)
SAP AG 2005, SAP Customer Program Optimization / 116
Application server A
Table buffer
Network
DBMS DBMSprocesses
Databasebuffer
Database
SELECT * FROM VVBAKWHERE ...
Database interface
ABAP program
8 - 600 ms
0.2 - 6 ms
R/3 Table Buffering: Introduction (2)
59
SAP AG 2005, SAP Customer Program Optimization / 117
key1 key2 key3 data
key1 key2 key3 data
key1 key2 key3 data
key1 key2 key3 data
Full buffering(100%)
Generic bufferingwith 1 key field
Generic bufferingwith 2 key fields
Single-record buffering
001001001001002002002002002002003003003003003003003003
001001001001002002002002002002002002003003003003003003003003003003003
001001001001001002002002002002002002002002002003003003003003003003003003003003003003
AA
BB
AABBBC
DC
AAABBCCCDDD
AA
BB
BAAAABBBC
DC
AAABBCCCCDDDD
42
31
513681230
53
2362423581234
Types of Buffering
SAP AG 2005, SAP Customer Program Optimization / 118
DB interface
Application server A
Table buffer
Network
Database management system
Database
Application server B
Table buffer...
DDLOG
Buffer synchronizationevery 1 - 2 minutes
UPDATE TCURR...
DB interface
INSERT DDLOG SELECT DDLOG
DB interface
1
2
3
Buffer Synchronization
60
SAP AG 2005, SAP Customer Program Optimization / 119
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
Degree of Invalidation
SAP AG 2005, SAP Customer Program Optimization / 120
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 isfield = value for all fields included in generic area
SQL Statements that Bypass the Buffer (1)
61
SAP AG 2005, SAP Customer Program Optimization / 121
SQL Statements that Bypass the Buffer (2)
SELECT * FROM tcurrCLIENT SPECIFIEDINTO TABLE g_itab_tcurrWHERE kurst = 'EURO'.
SELECT * FROM t100INTO TABLE g_itab_t100WHERE sprsl = 'D'AND arbgb = 'BC490'AND msgnr = '050'.
Generic bufferingGeneric buffering
Single record bufferingSingle record buffering
SELECT * FROM tcurrINTO TABLE g_itab_tcurrWHERE kurst = 'EURO'.
SELECT SINGLE * FROM t100INTO g_wa_t100WHERE sprsl = 'D'AND arbgb = 'BC490'AND msgnr = '050'.
CLIENT SPECIFIED
=> Database =>Table buffer
=> Database =>Table bufferSINGLE
SAP AG 2005, SAP Customer Program Optimization / 122
Technical criteriaSmall, usually < 1 MBOften read, but seldom changedTemporary data inconsistency acceptableAccess mainly from key fields
Note:Buffer tables > 10 MB in exceptional cases onlyTable buffers cannot be accessed via secondary indexesCheck available memory space before buffering more tables
Buffering Strategy: Technical Criteria
62
SAP AG 2005, SAP Customer Program Optimization / 123
Buffering Strategy: Semantic Criteria
Do not buffer transaction dataTables too bigTables changed frequently
Avoid buffering master dataTables too bigDifferent access paths to data
Buffer Customizing dataTables smallFew changes to tables
Internal Table OptimizationDefinition and Properties
Efficient composition of internal tables
Efficient access to internal tables
63
SAP AG 2005, SAP Customer Program Optimization / 125
Internal Table Attributes
SAP AG 2005, SAP Customer Program Optimization / 126
Key access
Access costs increase linearly increase logarithmically are constantfor n entries O(n) O(log n) O(1)
Access using
mainly index mainly key only key
Uniqueness NON-UNIQUE UNIQUE | NON-UNIQUE UNIQUE
STANDARD TABLE SORTED TABLE
INDEX TABLE HASHED TABLE
ANY TABLE
nn
Table scan Binary search Hashfunction
Table Types
64
SAP AG 2005, SAP Customer Program Optimization / 127
Defining Internal Tables
SAP AG 2005, SAP Customer Program Optimization / 128
Reading Single Records
65
SAP AG 2005, SAP Customer Program Optimization / 129
Loop costsdepending onthe internaltable type
10
100
1000
1000
01E
+051E
+06
5 10 20 40 80 160 320 640 1280
Number of table entries in both tables: n
Tim
e in
mic
rose
cond
s
Standard 52 121 335 1081 3887 14999 59700 238214 951704Sorted 44 87 177 391 862 1895 4048 8587 18136Hashed 41 78 151 305 613 1249 2497 5080 10537
5 10 20 40 80 160 320 640 1280
LOOP AT outer_tab INTO wa_out.READ TABLE inner_tab INTO wa_in WITH TABLE KEY ...
ENDLOOP.
LOOPREAD TABLE WITH TABLE KEY
Access Costs: Single Record Access via Key
SAP AG 2005, SAP Customer Program Optimization / 130
Hashed Tables
LOOP AT sort_vbap INTO wa_vbap.
AT NEW wa_vbap-vbeln.READ TABLE hash_vbak INTO wa_vbak WITH TABLE KEY
mandt = sy-mandt vbeln = wa_vbap-vbeln.WRITE: / wa_vbak-vbeln, wa_vbak-erdat.
ENDAT.
WRITE: /10 wa_vbap-posnr, ... .ENDLOOP.
Single record access with full specification of the primary key
MANDT VBELN ...MANDT VBELN ...
hash_vbak:
READ TABLE
LOOP
66
SAP AG 2005, SAP Customer Program Optimization / 131
LOOP AT sort_vbpa INTO wa_vbpaWHERE mandt = sy-mandt
AND parvw = 'WE'.ENDLOOP.
Mass processing with partial specification of the key- Try to include as many key fields in the WHERE condition aspossible; starting from the left key field, and without skipping a keyfield.
MANDT VBELN POSNR PARVWMANDT VBELN POSNR PARVW
sort_vbpa:LOOP ... WHEREDELETE ... WHEREMODIFY ... WHERE
Sorted Tables
LOOP AT sort_vbpa INTO wa_vbpaWHERE mandt = sy-mandt
AND vbeln = '0000000900'.ENDLOOP.
DELETE TABLE sort_vbpaWHERE mandt = sy-mandt
AND vbeln = '0000000900'.
LOOPWHERE
DELETE TABLEWHERE
SAP AG 2005, SAP Customer Program Optimization / 132
Standard Tables
PARAMETERS: p_ernam TYPE vbak-ernam,p_vdatu TYPE vbak-vdatu.
SORT stan_vbak BY ernam.READ TABLE stan_vbak WITH KEY ernam = p_ernam INTO wa
BINARY SEARCH.IF sy-subrc NE 0. ... ENDIF.
SORT stan_vbak BY erdat vdatu.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.
Flexible accessfor example, using various keys
SORT BYREAD
SORTLOOP
BY
67
Internal Table OptimizationDefinition and Properties
Efficient composition of internal tables
Efficient access to internal tables
SAP AG 2005, SAP Customer Program Optimization / 134
Array Fetch
68
SAP AG 2005, SAP Customer Program Optimization / 135
Inserting Internal Tables
SAP AG 2005, SAP Customer Program Optimization / 136
Filling an Internal Table with Cumulative Values
69
SAP AG 2005, SAP Customer Program Optimization / 137
Loading Unique Internal Tables
Internal Table OptimizationDefinition and Properties
Efficient composition of internal tables
Efficient access to internal tables
70
SAP AG 2005, SAP Customer Program Optimization / 139
Limited Mass Data Access
SAP AG 2005, SAP Customer Program Optimization / 140
Selective Field Transport
71
SAP AG 2005, SAP Customer Program Optimization / 141
Using Field Symbols
SAP AG 2005, SAP Customer Program Optimization / 142
Access on Tables without Field Transfer
72
SAP AG 2005, SAP Customer Program Optimization / 143
Reading Single Records
Use BINARY SEARCHfor standard tables(after sorting)
READ TABLE ... BINARY SEARCH.
READ TABLE ... ...INDEX
READ TABLE ... FROM <WA>.READ TABLE ... WITH TABLE KEY ...
FROMWITH TABLE KEY ...
If you can fully specifythe primary key, useFROM or WITH TABLE KEY
If possible, use indexoperations (not possiblewith hashed tables)
SAP AG 2005, SAP Customer Program Optimization / 144
i_vbak
wa
mandt vbeln
400 0000000110
400400400400400
Reading Single Records: Example
READ TABLE i_vbak WITH KEYmandt = sy-mandtvbeln = ‘0000000110‘ INTO wa.
wa-mandt = sy-mandt.wa-vbeln = ‘0000000110'.READ TABLE i_vbak FROM wa INTO wa.
* or
READ TABLE i_vbak WITH TABLE KEYmandt = sy-mandtvbeln = ‘0000000110‘ INTO wa.
* for standard tables:
SORT i_vbak BY mandt vbeln.READ TABLE i_vbak INTO wa
mandt = sy-mandtvbeln = ‘0000000110‘
BINARY SEARCH .
00000001000000000110000000012000000001300000001000
WA
i_vbak
WITH KEY
READ FROM
READ WITH TABLE KEY
READWITH KEY
BINARY SEARCH
73
SAP AG 2005, SAP Customer Program Optimization / 145
Mass Processing using a WHERE Clause
SELECT-OPTIONS:s_vbeln FOR vbak-vbeln.
LOOP AT stan_vbap INTO wa.IF wa-vbeln IN s_vbeln.
* ...ENDIF.
ENDLOOP.
LOOP AT <itab> ... WHERE ...MODIFY <itab> ... WHERE ...DELETE <itab> ... WHERE ...
Only access the table lines you really need
SELECT-OPTIONS:s_vbeln FOR vbak-vbeln.
LOOP AT stan_vbap INTO waWHERE mandt = sy-mandt
AND vbeln IN s_vbeln.ENDLOOP.
LOOP ATMODIFYDELETE WHERE
LOOP LOOPWHERE
WHEREWHERE
SAP AG 2005, SAP Customer Program Optimization / 146
Mass Processing using an Index Interval
LOOP AT sort_vbakASSIGNING <fs_vbak>.
LOOP AT sort_vbap ...WHERE VBELN = <fs_vbak>-vbeln.
ENDLOOP.
ENDLOOP.
LOOP ... FROM ... TO ... .APPEND ... FROM ... TO ... .INSERT ... FROM ... TO ... .DELETE ... FROM ... TO ... .
index = 1.LOOP AT sort_vbak
ASSIGNING <fs_vbak>.LOOP AT sort_vbap
ASSIGNING <fs_vbap>FROM index.
IF <fs_vbap>-vbeln NE<fs_vbak>-vbeln.
index = sy-tabix.EXIT.
ENDIF.ENDLOOP.
ENDLOOP.
2
12345
1234..10
1
Sort_vbak Sort_vbakindexOnly access the table lines you
really need1
2
3...
FROM index.
index
FROMFROMFROMFROM
TOTOTOTO