51
Nov. 12-15, 2002 © SAP Labs, Inc. SAP TechEd 2002 November 12-15, 2002 (session id) 1 Efficient Database Programming with ABAP Ulrich Koch, SAP AG Tobias Wenner, SAP AG SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 2 Learning Objectives As a result of this workshop, you will be able to: Explain the communication between database and application server Analyze bottlenecks in database programming Understand how table buffers and indexes work Use Open SQL for efficient database access

ABAP152 - Efficient Database Programming With ABAP

  • Upload
    yennt83

  • View
    22

  • Download
    0

Embed Size (px)

Citation preview

Page 1: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 1

Nov. 12-15, 2002

© SAP Labs, Inc. SAP TechEd 2002 November 12-15, 2002 (session id) 1

Efficient Database Programming with ABAP

Ulrich Koch, SAP AGTobias Wenner, SAP AG

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 2

Learning Objectives

As a result of this workshop, you will be able to:

Explain the communication between database and application serverAnalyze bottlenecks in database programmingUnderstand how table buffers and indexes workUse Open SQL for efficient database access

Page 2: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 2

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 3

Performance of Business Transactions

A "normal" ABAP statement executes within microseconds. A database access normally needs a few milliseconds or even seconds to return a result. General rule:The performance of a business transaction is primarily determined by its database accesses.

DB

Application

GUI

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 4

Agenda

Web AS and Database ArchitectureClient / Server TechnologyTable BufferingIndexes

How to Identify Expensive SQL

Rules for Better SQL ProgrammingTheory and Hands-On

Rule 1Rule 2Rule 3Rule 4Rule 5

Summary

Page 3: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 3

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 5

WAS and Database Architecture (I)

Application Server(s)

User communication

Data transfer between database serverand application server(s)

Frontend(s) / Presentation Server(s)

DB Server

DBcache

DB WP

localdataWP

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 6

WAS and Database Architecture (II)

R/3 work processR/3 work process

DB work process DB work process

Database cacheDatabaseService

processes

Operating system

Database files

R/3 work process

DB work process

LANcommunication

DB CPUconsumption

DB memoryconsumption

Physical I/O

Table BufferApp ServerCPU consumption

App Servermemoryconsumption

Page 4: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 4

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 7

Agenda

Web AS and Database ArchitectureClient / Server TechnologyTable BufferingIndexes

How to Identify Expensive SQL

Rules for Better SQL ProgrammingTheory and Hands-On

Rule 1Rule 2Rule 3Rule 4Rule 5

Summary

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 8

Table Buffering

GoalCache portions of a database table in the application server to reduce database load and network communication

FunctionalityBuffering can be enabled for individual tablesBuffering is transparent to the application programThe buffer granularity are generic key ranges, i.e. the set of rows whose first N key columns have equal values. N is defined at design time (Dictionary). Special cases:

N = 0: complete table bufferedN = #key columns: single rows buffered (including misses)

A query whose WHERE clause restricts the result set to the defined key range is executed on the buffer (some exceptions: aggregate functions, GROUP BY, HAVING, ORDER BY for non-primary key order, Joins, etc.)The first buffer access implicitly loads the according key range into the bufferA modification operation concerning one key range is performed in the local buffer and invalidates the corresponding key ranges in remote buffersInvalidation requests are send through buffer synchronization

Page 5: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 5

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 9

Table Buffering: Types

Full buffering(no key fields)

8D0036D0032D0035C0034B0032B0036A0033A0032A0035D0021C0020C0023B0022B0021B0028A0026A0023A0021A0025B0013B0011B0014A0012A001

datakey3key2key1

Generic buffering(two key fields)

8D0036D0032D0035C0034B0032B0036A0033A0032A0035D0021C0020C0023B0022B0021B0028A0026A0023A0021A0025B0013B0011B0014A0012A001

datakey3key2key1

Single row buffering(all key fields)

8D0036D0032D0035C0034B0032B0036A0033A0032A0035D0021C0020C0023B0022B0021B0028A0026A0023A0021A0025B0013B0011B0014A0012A001

datakey3key2key1

Generic buffering(one key field)

8D0036D0032D0035C0034B0032B0036A0033A0032A0035D0021C0020C0023B0022B0021B0028A0026A0023A0021A0025B0013B0011B0014A0012A001

datakey3key2key1

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 10

Buffer Synchronization – Example (I)

Server 1 Server 2

Table Buffer

Database

Table Buffer

T

TT

DDLOG

Page 6: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 6

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 11

Buffer Synchronization – Example (II)

Server 1 Server 2

Table Buffer

Database

Table Buffer

T

TT

DDLOG

Update on T

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 12

Buffer Synchronization – Example (III)

Server 1 Server 2

Table Buffer

Database

Table Buffer

T

TT

DDLOG

Update on T

INSERT

Page 7: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 7

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 13

Buffer Synchronization – Example (IV)

Server 1 Server 2

Table Buffer

Database

Table Buffer

T

TT

DDLOG

Update on T

INSERT READ

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 14

Agenda

Web AS and Database ArchitectureClient / Server TechnologyTable BufferingIndexes

How to Identify Expensive SQL

Rules for Better SQL ProgrammingTheory and Hands-On

Rule 1Rule 2Rule 3Rule 4Rule 5

Summary

Page 8: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 8

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 15

Access by Full Table Scan

16:3113:30SFOJFK0017AA

08:1507:10FRATXL2407LH

17:0015:00UNLGKS0815AA

15:0513:30JFKFRA0402LH

18:2510:00JFKSFO1984DL

17:5514:45SFOJFK0007UA

10:3015:00FRASFO0455LH

12:3010:10SFOFRA0454LH

arrtimedeptimeairptoairpfromconnidcarrid

SPFLI

SELECT * FROM spfli WHERE deptime = '150000'.

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 16

Tables and Indexes

Adams, Tim

Burton, Robert

Daum, Paul

Maier, Bernd

Maier, Kurt

Stewart, Ron

Zarcone, Pia

Adams, Tim

Burton, Robert

Daum, Paul

Maier, Bernd

Maier, Kurt

Stewart, Ron

Zarcone, Pia

Maier, BerndBerlin

Burton, RobertNew Orleans

Zarcone, PiaRome

Daum, PaulBremen

Adams, TimMiami

Maier, KurtHamburg

Stewart, RonEdinburgh

Maier, BerndBerlin

Burton, RobertNew Orleans

Zarcone, PiaRome

Daum, PaulBremen

Adams, TimMiami

Maier, KurtHamburg

Stewart, RonEdinburgh

Berlin

Bremen

Edinburgh

Hamburg

Miami

New Orleans

Rome

Berlin

Bremen

Edinburgh

Hamburg

Miami

New Orleans

Rome

TableIndex (Name) Index (City)

Page 9: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 9

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 17

...

Tables and Indexes

Primary Key

Primary index• implicitly defined• unique

Secondary indexes• user defined• unique or non-unique

spfli

... ...

mandt carrid connid

mandt carrid

airpfrom airpto

airptoairpfrom

cityfrom cityto

citytocityfromconnid

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 18

Access by Primary Index

16:3113:30SFOJFK0017AA

08:1507:10FRATXL2407LH

14:0012:00UNLGKS0815AA

15:0513:30JFKFRA0402LH

18:2510:00JFKSFO1984DL

17:5514:45SFOJFK0007UA

10:3015:00FRASFO0455LH

12:3010:10SFOFRA0454LH

arrtimedeptimeairptoairpfromconnidcarrid

0007UA

2407LH

0455LH

0454LH

0402LH

1984DL

0815AA

0017AA

connidcarrid

SPFLI

SELECT * FROM spfli WHERE carrid = 'AA' AND connid = '0815'.

Page 10: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 10

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 19

Access by Secondary Index

16:3113:30SFOJFK0017AA

08:1507:10FRATXL2407LH

14:0012:00UNLGKS0815AA

15:0513:30JFKFRA0402LH

18:2510:00JFKSFO1984DL

17:5514:45SFOJFK0007UA

10:3015:00FRASFO0455LH

12:3010:10SFOFRA0454LH

arrtimedeptimeairptoairpfromconnidcarrid

0007UA

2407LH

0455LH

0454LH

0402LH

1984DL

0815AA

0017AA

connidcarrid

FRATXL

JFKSFO

FRASFO

SFOJFK

SFOJFK

UNLGKS

SFOFRA

JFKFRA

airptoairpfrom

SPFLI

SELECT * FROM spfli WHERE airpfrom = 'JFK' AND airpto = 'SFO'.

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 20

Indexes

Indexes can vastly improve performance when searching for data.Indexes will slightly slow down updates.A bad index is worse than none at all because data blocks might be read again and again.Sometimes a Full Table Scan is faster. The optimizer should do it right.

Page 11: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 11

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 21

Statement Optimizer

Execution Plan

SELECT * FROM sflightINTO xflightWHERE cityfrom = 'OSLO'AND fldate = '20030914'

ORDER BY carrid.

rule-based

cost-based

... decides how to execute the SQL statement

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 22

Agenda

Web AS and Database ArchitectureClient / Server TechnologyTable BufferingIndexes

How to Identify Expensive SQL

Rules for Better SQL ProgrammingTheory and Hands-On

Rule 1Rule 2Rule 3Rule 4Rule 5

Summary

Page 12: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 12

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 23

SQL Trace

1. Start ST05

3. Run the test program (in a different window)

4. Switch off theSQL trace: "Trace off"

5. List the SQL statements recorded: "List trace"

2. Switch on theSQL trace: "Trace on"

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 24

SQL Trace

Page 13: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 13

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 25

Execution Plan

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 26

Other Tools

There are other tools you should also regard when performance problems show up:

Transaction SE30: The ABAP Profiler

Transaction DB01: Lockwait Situations

Transaction ST02: Buffer Statistics

Transaction ST04: Database Performance Analysis

Page 14: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 14

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 27

Demo

Demo

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 28

Agenda

Web AS and Database ArchitectureClient / Server TechnologyTable BufferingIndexes

How to Identify Expensive SQL

Rules for Better SQL ProgrammingTheory and Hands-On

Rule 1Rule 2Rule 3Rule 4Rule 5

Summary

Page 15: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 15

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 29

databaseFROM clause

one or more tables

Index accesssearch area

WHERE clause

hit list

solution set

SELECT clause

SELECT carrid connid INTO (carrid, connid)FROM spfli WHERE carrid = 'LH' AND cityfrom = 'ROME'.

SELECT Statement Overview

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 30

Agenda

Web AS and Database ArchitectureClient / Server TechnologyTable BufferingIndexes

How to Identify Expensive SQL

Rules for Better SQL ProgrammingTheory and Hands-On

Rule 1Rule 2Rule 3Rule 4Rule 5

Summary

Page 16: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 16

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 31

Rule 1: Keep the Hit List Small

Keep the hit list small!

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 32

Keep the Hit List Small

Use a WHERE clause whenever possible.

Select all LH0300 flights.

SELECT * FROM sflightINTO xflightWHERE carrid = 'LH'AND connid = '0300'.WRITE: / xflight-fldate.

ENDSELECT.

SELECT * FROM sflightINTO xflight.CHECK xflight-carrid = 'LH'.CHECK xflight-connid = '0300'.WRITE: / xflight-fldate.

ENDSELECT.

Page 17: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 17

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 33

Keep the Hit List Small

Always try to describe the full search condition.

Select all LH0300 flights in 2003.

SELECT * FROM sflight INTO xflight WHERE carrid = 'LH'AND connid = '0300' AND fldate LIKE '2003%'.

WRITE: / xflight-fldate.ENDSELECT.

SELECT * FROM sflight INTO xflightWHERE carrid = 'LH' AND connid = '0300'.

CHECK xflight-fldate(4) = '2003'.WRITE: / xflight-fldate.

ENDSELECT.

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 34

App Server 1

Keep the Hit List Small: Effects

DB Server

Database files

DB work processDB CPUconsumption

DB memoryconsumption

App ServerCPU consumption

App Servermemoryconsumption

App Server 2

R/3 work process R/3 work process R/3 work process

Table Buffer Table Buffer

DB work processDB work process

LANcommunication

physicalI/O

Database Service

ProcessesDatabase Cache

Database Service

Processes

Page 18: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 18

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 35

Keep the Hit List Small: Exercise

Exercise 1: Keep the Hit List Small

1. Open program ZABAP152_1_XX for editing.(XX = the number of your group)

2. Optimize the SELECT statements in form "VERSION1":Substitute the CHECK conditions by specifying the desired rows in a WHERE clause.

3. Run the program to see the effect of your optimization.

Exercise 1: Keep the Hit List Small

1. Open program ZABAP152_1_XX for editing.(XX = the number of your group)

2. Optimize the SELECT statements in form "VERSION1":Substitute the CHECK conditions by specifying the desired rows in a WHERE clause.

3. Run the program to see the effect of your optimization.

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 36

Agenda

Web AS and Database ArchitectureClient / Server TechnologyTable BufferingIndexes

How to Identify Expensive SQL

Rules for Better SQL ProgrammingTheory and Hands-On

Rule 1Rule 2Rule 3Rule 4Rule 5

Summary

Page 19: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 19

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 37

Rule 2: Minimize the Amount of Transferred Data

Minimize the amount of data

transferred between the database and the application

server!

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 38

Minimize the Amount of Transferred Data

Use a field list instead of SELECT *.

Select all LH0300 flights in 2003.

SELECT fldate FROM sflight INTO (xflight-fldate)WHERE carrid = 'LH'AND connid = '0300'AND fldate LIKE '2003%'.

WRITE: / xflight-fldate.ENDSELECT.

SELECT * FROM sflightINTO xflightWHERE carrid = 'LH'AND connid = '0300' AND fldate LIKE '2003%'.

WRITE: / xflight-fldate.ENDSELECT.

Page 20: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 20

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 39

Minimize the Amount of Transferred Data: Exercise

Exercise 2: Specify a SELECT list

1. Open program ZABAP152 _2_XX for editing.(XX = the number of your group)

2. Optimize the SELECT statements in form "VERSION2":Select only the columns needed in the subroutine.

3. Run the program to see the effect of your optimization.

Exercise 2: Specify a SELECT list

1. Open program ZABAP152 _2_XX for editing.(XX = the number of your group)

2. Optimize the SELECT statements in form "VERSION2":Select only the columns needed in the subroutine.

3. Run the program to see the effect of your optimization.

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 40

Minimize the Amount of Transferred Data: Exercise

Optional Exercise 2.1: Use FOR ALL ENTRIES

1. Open program ZABAP152 _6_XX for editing.(XX = the number of your group)

2. Optimize the SELECT statements in form "VERSION2_1":Replace the nested SELECT loops by SELECT ... INTO TABLE and SELECT... FOR ALL ENTRIES .

3. Run the program to see the effect of your optimization.

Optional Exercise 2.1: Use FOR ALL ENTRIES

1. Open program ZABAP152 _6_XX for editing.(XX = the number of your group)

2. Optimize the SELECT statements in form "VERSION2_1":Replace the nested SELECT loops by SELECT ... INTO TABLE and SELECT... FOR ALL ENTRIES .

3. Run the program to see the effect of your optimization.

Page 21: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 21

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 41

Minimize the Amount of Transferred Data

Apply UP TO n ROWS for a top n solution set.

Find those 10 business customers with the biggest discount.

SELECT id name discount FROM scustom UP TO 10 ROWSINTO (xid, xname, xdiscount) WHERE custtype = 'B'ORDER BY discount DESCENDING.

WRITE: / xid, xname, xdiscount.ENDSELECT.

SELECT id name discount FROM scustom INTO (xid, xname, xdiscount) WHERE custtype = 'B'ORDER BY discount DESCENDING.

IF sy-dbcnt > 10. EXIT. ENDIF.WRITE: / xid, xname, xdiscount.

ENDSELECT.

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 42

Minimize the Amount of Transferred Data

Use the UPDATE ... SET statement.

Flight LH0400 will be renamed to LH0500 starting on January 1, 2004.

UPDATE sbook SET connid = '0500' WHERE carrid = 'LH'AND connid = '0400'AND fldate >= '20040101'.

SELECT * FROM sbook INTO xbookWHERE carrid = 'LH'AND connid = '0400'AND fldate >= '20040101'.

xbook-connid = '0500'. UPDATE sbook FROM xbook.

ENDSELECT.

Page 22: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 22

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 43

Minimize the Amount of Transferred Data

Use aggregate functions.

Calculate total number of LH passengers in 2003.

SELECT SUM( seatsocc )FROM sflight INTO sumWHERE carrid = 'LH'AND fldate LIKE '2003%'.

WRITE: / sum.

sum = 0.SELECT seatsocc

FROM sflight INTO xseatsoccWHERE carrid = 'LH'AND fldate LIKE '2003%'.

sum = sum + xseatsocc.ENDSELECT. WRITE: / sum.

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 44

Minimize the Amount of Transferred Data

Apply the HAVING clause.

Find all flights that have bookings with a luggage weight > 20 kg.

SELECT carrid connid fldate MAX( luggweight ) INTO (xcarrid, xconnid, xfldate, max) FROM sbookGROUP BY carrid connid fldateHAVING MAX( luggweight ) > 20.

WRITE: / xcarrid, xconnid, xfldate, max. ENDSELECT.

SELECT carrid connid fldate MAX( luggweight ) INTO (xcarrid, xconnid, xfldate, max) FROM sbookGROUP BY carrid connid fldate.

CHECK max > 20. WRITE: / xcarrid, xconnid, xfldate, max.

ENDSELECT.

Page 23: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 23

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 45

App Server 1

Minimize the Amount of Transferred Data: Effects

DB Server

Database files

DB work processDB CPUconsumption

DB memoryconsumption

App ServerCPU consumption

App Servermemoryconsumption

App Server 2

R/3 work process R/3 work process R/3 work process

Table Buffer Table Buffer

DB work processDB work process

LANcommunication

physicalI/O

Database Service

ProcessesDatabase Cache

Database Service

Processes

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 46

Minimize the Amount of Transferred Data: Exercise

Exercise 3: Use Aggregate Functions

1. Open program ZABAP152 _3_XX for editing.(XX = the number of your group)

2. Optimize the SELECT statements in form "VERSION3":Have the database calculate the sum in the inner loop.

3. Run the program to see the effect of your optimization.

Exercise 3: Use Aggregate Functions

1. Open program ZABAP152 _3_XX for editing.(XX = the number of your group)

2. Optimize the SELECT statements in form "VERSION3":Have the database calculate the sum in the inner loop.

3. Run the program to see the effect of your optimization.

Page 24: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 24

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 47

Agenda

Web AS and Database ArchitectureClient / Server TechnologyTable BufferingIndexes

How to Identify Expensive SQL

Rules for Better SQL ProgrammingTheory and Hands-On

Rule 1Rule 2Rule 3Rule 4Rule 5

Summary

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 48

Rule 3: Keep the Number of Round Trips Small

Keep the number of round trips

between the database and the application server

small!

Page 25: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 25

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 49

Data Transfer: Single Row INSERT

R/3 work process DB work processINSERT INTO sflight VALUES flight_wa.

INSERT INTO sflight VALUES flight_wa.

Data to insert

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 50

Data Transfer: Single Row INSERT

R/3 work process DB work processINSERT INTO sflight VALUES flight_wa.

INSERT INTO sflight VALUES flight_wa.

Data to insert

Page 26: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 26

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 51

Data Transfer: Array INSERT

R/3 work process DB work processINSERT INTO sflight FROM TABLE flight_tab.

INSERT INTO sflight FROM TABLE flight_tab.

Data to insert

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 52

Data Transfer: Array INSERT

R/3 work process DB work processINSERT INTO sflight FROM TABLE flight_tab.

INSERT INTO sflight FROM TABLE flight_tab.

Page 27: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 27

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 53

Keep the Number of Round Trips Small

Use array operations with UPDATE, INSERT, and DELETE.

Insert lines from internal table into table SBOOK.

INSERT sbook FROM TABLE itab.

LOOP AT itab INTO wa. INSERT INTO sbook VALUES wa.

ENDLOOP.

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 54

Challenge: Conversion of a huge table.

Solution:

OPEN CURSOR WITH HOLD dbcur FORSELECT … FROM old_table.

DO.FETCH NEXT CURSOR dbcur

INTO TABLE itab PACKAGE SIZE 10000.IF SY-SUBRC <> 0.EXIT.

ENDIF.UPDATE new_table FROM TABLE itab.CALL FUNCTION 'DB_COMMIT'.

ENDDO.CLOSE CURSOR dbcur.

OPEN CURSOR WITH HOLD (I)

Page 28: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 28

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 55

OPEN CURSOR WITH HOLD (II)

The package size depends on the table width, the size of the rollback segments, and the size of the lock tables.

Recommendation: Do not update more than 10 MB of data within one database transaction.

COMMIT WORK implicitly closes database cursors in contrast to the function module DB_COMMIT.

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 56

Sometimes all the comparison values for a SELECT statement are given as an internal table.

Example: We got an internal table "conn_tab" containing the CARRIDs and CONNIDs for certain flights departing from Las Vegas. Determine the flight details for all these connections for September 2003.

SELECT carrid connid fldate seatsocc FROM sflightINTO TABLE seatsocc_tabFOR ALL ENTRIES IN conn_tabWHERE carrid = conn_tab-carridAND connid = conn_tab-connidAND fldate LIKE '200309%'.

SELECT … FOR ALL ENTRIES (I)

Page 29: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 29

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 57

The FOR ALL ENTRIES condition has the same effect as:

SELECT DISTINCT carrid connid fldate seatsoccFROM sflightINTO TABLE seatsocc_tabWHERE ( carrid = <row 1 of conn_tab>-carrid ANDconnid = <row 1 of conn_tab>-connid ANDfldate LIKE '200309%' )

OR( carrid = <row 2 of conn_tab>-carrid ANDconnid = <row 2 of conn_tab>-connid ANDfldate LIKE '200309%' )

OR ... OR( carrid = <row n of conn_tab>-carrid ANDconnid = <row n of conn_tab>-connid AND fldate LIKE '200309%' ).

SELECT … FOR ALL ENTRIES (II)

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 58

FOR ALL ENTRIES (III)

Duplicate rows are discarded from the result set.

There is no limit for the size of the FAE internal table because of the database dependent decomposition of the statement.

COUNT(*) is the only aggregate function supported. But it is only emulated: the calculation is done on the application server.

You can use UP TO n ROWS and PACKAGE SIZE together with FOR ALL ENTRIES. But it is only emulated: the whole result set is transferred to the application server.

Caution: If the FAE internal table is empty, then no WHERE condition is generated. Thus all rows will be selected.

If the source of the content of the FAE internal table is anotherSELECT statement then a JOIN is often the better solution.

Page 30: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 30

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 59

INNER JOIN

2002/11/090402LHNEW YORK0402LH

2002/11/080402LHNEW YORK0402LH

2002/11/120017AAFRANKFURT0017AA

2002/11/070017AAFRANKFURT0017AA

2002/11/090402LH

2002/11/080402LH

2002/11/120017AA

2002/11/070017AA

fldateconnidcarrid

NEW YORK0598QF

FRANKFURT0440LH

NEW YORK0402LH

FRANKFURT0017AA

cityfromconnidcarrid

fldateconnidcarridcityfromconnidcarrid

Inner Join

SPFLI SFLIGHT

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 60

INNER JOIN

2002/11/090402LHNEW YORK0402LH

2002/11/080402LHNEW YORK0402LH

2002/11/120017AAFRANKFURT0017AA

2002/11/070017AAFRANKFURT0017AA

2002/11/090402LH

2002/11/080402LH

2002/11/120017AA

2002/11/070017AA

fldateconnidcarrid

NEW YORK0598QF

FRANKFURT0440LH

NEW YORK0402LH

FRANKFURT0017AA

cityfromconnidcarrid

fldateconnidcarridcityfromconnidcarrid

Inner Join

SPFLI SFLIGHT

Page 31: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 31

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 61

LEFT OUTER JOIN

NULLNULLNULLNEW YORK0598QF

NULLNULLNULLFRANKFURT0440LH

2002/11/090402LH

2002/11/080402LH

2002/11/120017AA

2002/11/070017AA

fldateconnidcarrid

NEW YORK0598QF

FRANKFURT0440LH

NEW YORK0402LH

FRANKFURT0017AA

cityfromconnidcarrid

2002/11/090402LHNEW YORK0402LH

2002/11/080402LHNEW YORK0402LH

2002/11/120017AAFRANKFURT0017AA

2002/11/070017AAFRANKFURT0017AA

fldateconnidcarridcityfromconnidcarrid

Left Outer Join

SPFLI SFLIGHT

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 62

LEFT OUTER JOIN

NULLNULLNULLNEW YORK0598QF

NULLNULLNULLFRANKFURT0440LH

2002/11/090402LHNEW YORK0402LH

2002/11/080402LHNEW YORK0402LH

2002/11/120017AAFRANKFURT0017AA

2002/11/070017AAFRANKFURT0017AA

2002/11/090402LH

2002/11/080402LH

2002/11/120017AA

2002/11/070017AA

fldateconnidcarrid

NEW YORK0598QF

FRANKFURT0440LH

NEW YORK0402LH

FRANKFURT0017AA

cityfromconnidcarrid

fldateconnidcarridcityfromconnidcarrid

Left Outer Join

SPFLI SFLIGHT

Page 32: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 32

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 63

SUBQUERIES

Sometimes you just want to know whether some records in asecondary table exist or not. You don‘t need their actual content. This is where Subqueries come in handy:

Example: Detect an inconsistency

Are there any rows in the SFLIGHT table without a corresponding entry in table SPFLI?

SELECT carrid connidINTO (xcarrid, xconnid) FROM sflight AS fWHERE NOT EXISTS ( SELECT * FROM spfli

WHERE carrid = f~carridAND connid = f~connid ).

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 64

Keep the Number of Round Trips Small

Apply the INNER JOIN.Avoid nested SELECT-ENDSELECT loops.Find all bookings for planes of type 727-200.

SELECT f~carrid f~connid b~bookid INTO (xcarrid, xconnid, ybookid) FROM sflight AS f INNER JOIN sbook AS b

ON f~carrid = b~carrid AND f~connid = b~connid AND f~fldate = b~fldate

WHERE planetype = '727-200'. WRITE: / xcarrid, xconnid, ybookid.

ENDSELECT.

SELECT carrid connid fldate FROM sflight INTO (xcarrid, xconnid, xfldate) WHERE planetype = '727-200'.

SELECT bookid FROM sbook INTO ybookid WHERE carrid = xcarrid AND connid = xconnid AND fldate = xfldate.

WRITE: / xcarrid, xconnid, ybookid.ENDSELECT.

ENDSELECT.

Page 33: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 33

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 65

Keep the Number of Round Trips Small

Apply the OUTER JOIN.

Find all bookings for planes of type 727-200. List flights without bookings as well.

SELECT f~carrid f~connid f~fldate b~bookid INTO (xcarrid, xconnid, xfldate, ybookid) FROM sflight AS f LEFT OUTER JOIN sbook AS b

ON f~carrid = b~carrid AND f~connid = b~connidAND f~fldate = b~fldate.

WHERE planetype = '727-200'. WRITE: / xcarrid, xconnid, xfldate, ybookid.

ENDSELECT.

SELECT carrid connid fldate FROM sflight INTO (xcarrid, xconnid, xfldate) WHERE planetype = '727-200'.

SELECT bookid FROM sbook INTO ybookid WHERE carrid = xcarridAND connid = xconnid AND fldate = xfldate.

WRITE: / xcarrid, xconnid, xfldate, ybookid.ENDSELECT.IF sy-dbcnt = 0. WRITE: / xcarrid, xconnid, xfldate.

ENDIF.ENDSELECT.

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 66

Keep the Number of Round Trips Small

Use subqueries (1).

Find the dates of the best booked flights for each connection.

SELECT carrid connid fldateFROM sflight AS fINTO (xcarrid, xconnid, xfldate) WHERE seatsocc IN ( SELECT MAX( seatsocc ) FROM sflight WHERE carrid = f~carrid AND connid = f~connid ).

WRITE: xcarrid, xconnid, xfldate.ENDSELECT.

SELECT carrid connid MAX( seatsocc )FROM sflight INTO (xcarrid, xconnid, max)GROUP BY carrid connid.SELECT fldate FROM sflight

INTO yfldateWHERE carrid = xcarrid AND

connid = xconnid ANDseatsocc = max.

WRITE: / xcarrid, xconnid, yfldate.ENDSELECT.

ENDSELECT.

Page 34: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 34

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 67

Keep the Number of Round Trips Small

For frequently used INNER JOINs you can create a database view in the ABAP Dictionary.

Select all bookings for all flights.

SELECT carrid connid bookid INTO (xcarrid, xconnid, xbookid) FROM sflightbook.

WRITE: / xcarrid, xconnid, xbookid.ENDSELECT.

SELECT f~carrid f~connid b~bookid INTO (xcarrid, xconnid, xbookid) FROM sflight AS f INNER JOIN sbook AS b

ON f~carrid = b~carrid AND f~connid = b~connid AND f~fldate = b~fldate.

WRITE: / xcarrid, xconnid, xbookid.ENDSELECT.

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 68

Keep the Number of Round Trips Small

At least one of the tables is buffered.A lot of columns of the left hand side table are selected and for each row there are many matching right side table rows. In this case a lot of redundant data has to be transferred to the application server. This might be a case for SELECT ... FOR ALL ENTRIES.The database optimizer fails to find an efficient query plan and hints are not possible. Again, this might be a case for FOR ALL ENTRIES.JOINs don't work for pooled tables and cluster tables.

Situation when JOINs might be counterproductive

Page 35: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 35

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 69

App Server 1

Keep the Number of Round Trips Small: Effects

DB Server

Database files

DB work processDB CPUconsumption

DB memoryconsumption

App ServerCPU consumption

App Servermemoryconsumption

App Server 2

R/3 work process R/3 work process R/3 work process

Table Buffer Table Buffer

DB work processDB work process

LANcommunication

physicalI/O

Database Service

ProcessesDatabase Cache

Database Service

Processes

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 70

Keep the Number of Round Trips Small: Exercise

Exercise 4: Use an Inner Join

1. Open program ZABAP152 _4_XX for editing.(XX = the number of your group)

2. Optimize the SELECT statements in form "VERSION4":Replace the nested SELECT statements by an inner join.

3. Run the program to see the effect of your optimization.

Exercise 4: Use an Inner Join

1. Open program ZABAP152 _4_XX for editing.(XX = the number of your group)

2. Optimize the SELECT statements in form "VERSION4":Replace the nested SELECT statements by an inner join.

3. Run the program to see the effect of your optimization.

Page 36: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 36

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 71

Keep the Number of Round Trips Small

Use subqueries (2).Calculate the revenue from the totally booked LH flights.

SELECT SUM( loccuram ) FROM sbook AS bINTO sumWHERE carrid = 'LH'AND loccurkey = 'EUR'AND EXISTS ( SELECT * FROM sflight AS f

WHERE f~carrid = b~carridAND f~connid = b~connidAND f~fldate = b~fldateAND f~seatsmax = f~seatsocc ).

sum = 0.SELECT carrid connid fldate FROM sflight

INTO (xcarrid, xconnid, xfldate)WHERE carrid = 'LH'AND seatsmax = sflight~seatsocc.

SELECT SUM( loccuram ) FROM sbookINTO sbook_wa-loccuramWHERE carrid = xcarridAND connid = xconnidAND fldate = xfldateAND loccurkey = 'EUR'.

sum = sum + sbook_wa-loccuram.ENDSELECT.

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 72

Keep the Number of Round Trips Small: Exercise

Optional Exercise 4.1: FOR ALL ENTRIES again

1. Open program ZABAP152 _8_XX for editing.(XX = the number of your group)

2. Find all cities reachable from a given starting point.This can be done with a single SELECT ... FOR ALL ENTRIES statement (in a loop).

3. Run the program to see whether you find all destinations.

Optional Exercise 4.1: FOR ALL ENTRIES again

1. Open program ZABAP152 _8_XX for editing.(XX = the number of your group)

2. Find all cities reachable from a given starting point.This can be done with a single SELECT ... FOR ALL ENTRIES statement (in a loop).

3. Run the program to see whether you find all destinations.

Page 37: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 37

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 73

Keep the Number of Round Trips Small: Exercise

Optional Exercise 5: Use a Subquery

1. Open program ZABAP152 _10_XX for editing.(XX = the number of your group)

2. Optimize the SELECT statements in form "VERSION3":Replace the two SELECT statements by a subquery.

3. Run the program to see the effect of your optimization.

Optional Exercise 5: Use a Subquery

1. Open program ZABAP152 _10_XX for editing.(XX = the number of your group)

2. Optimize the SELECT statements in form "VERSION3":Replace the two SELECT statements by a subquery.

3. Run the program to see the effect of your optimization.

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 74

Keep the Number of Round Trips Small: Exercise

Optional Exercise 6: Use a Subquery

1. Open program ZABAP152 _12_XX for editing.(XX = the number of your group)

2. Optimize the SELECT statements in form "VERSION2":Replace the two SELECT statements by one SELECT using subqueries and / or joins.

3. Run the program to see the effect of your optimization.

Optional Exercise 6: Use a Subquery

1. Open program ZABAP152 _12_XX for editing.(XX = the number of your group)

2. Optimize the SELECT statements in form "VERSION2":Replace the two SELECT statements by one SELECT using subqueries and / or joins.

3. Run the program to see the effect of your optimization.

Page 38: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 38

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 75

Agenda

Web AS and Database ArchitectureClient / Server TechnologyTable BufferingIndexes

How to Identify Expensive SQL

Rules for Better SQL ProgrammingTheory and Hands-On

Rule 1Rule 2Rule 3Rule 4Rule 5

Summary

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 76

Rule 4: Keep the Cost of the Search Down

Keep the cost of the search

down!

Page 39: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 39

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 77

Keep the Cost of the Search Down

SELECT field1 field2 field3FROM table1 WHERE field1 IN ('A','B') AND field3 like 'T%'

SELECTclause

WHERE clause

FROM clause

search area

One or more tables

Data which must besearched through -

limited only bythe use of indexes

The result set

Data to be transferred

Index: field1 field2

• An optimal index reduces the search area to the result set.• A bad index does not reduce the search area significantly, thus

requiring a lot of additional checks in the data blocks.

Bad index

Good index

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 78

Keep the Cost of the Search Down

If you can reduce data block accesses you can significantly improve the performance

For each data block which has to be accessed, there is the risk that it is not in the database buffer and thus has to be read from disk

A good index should reduce the area to be searched significantlyThus reducing the number of data blocks which have to be read

A bad index will not reduce the search area significantly. In this case it is necessary for the database to read lots of data blocks, thus throwing out other blocks from the bufferAdditional checks have to be done on the data blocks and afterwards the rows are discarded

Ideally an index will restrict the search area to the result setHowever, due to a restriction in the number of indexes per tablecompromises have to be made if a lot of different accesses are requiredSo in reality it is enough if the index reduces the search area close to the result set

Page 40: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 40

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 79

Keep the Cost of the Search Down

SELECT * FROM sflight INTO waWHERE price BETWEEN 300 AND 700.

key1 key2001001001001001002002002002002

AA

BB

BAAAAB

MANDT PRICE11111

10210

102102102

400

600500

7001

400345

300Index 1 Index 2

001001001001001

AA

BB

B

2020202020

400

600500

700

300

001001001001001

AA

BB

B

3030303030

400

600500

700

300

Using index 1 the search area can still be restricted using a binary search. With index 2, however, only PRICE can be restricted by binary search. The column MANDT has to be scanned sequentially for each row within the range.

002 A10 500002 A10 600002 A10 700

002 A10 300

key1 key2001001001001001002002002002002

PRICE MANDT3001300300400224005003001001001001001

5005006004600001001001001001

6007005700700

001001001001001002002002002002

110220301102102301102001001001001001

2030110220001001001001001

3011022030

002002400400

0020021020

0022 002102002500 00210

0022 002102002600 00210

0022 002102002700 00210

0022 002102002300 00210

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 80

Reasonable Index Design (I)

Accessing an index for data reduction can be divided into two parts.The first part is a binary part building a start and stop key.The second part is a sequential scan within the key range.

A good index allows a database to reduce the search area mainly by the usage of a binary strategy.

In a bad index, a big part of the index has to be scanned sequentially, thus making it necessary to access lots of index blocks and to perform many data checks.

The more selective a field is, the better it is to have this field at the beginning of the index.

Fields which can be specified with an EQ (=) or IN list are also best at the beginning of an index, because they do not prevent the database from the usage of a further binary strategy on the next index column.

Page 41: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 41

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 81

Reasonable Index Design (II)

The client is good in the first position because itis nearly always specified (except usage of CLIENT SPECIFIED)

is nearly always specified with the EQ operator and thus does not harm further binary data reduction.

Create small indexes.

Keep in mind, which indexes are defined, avoid overlaps.

Up to 5 indexes in each table generally are not critical.

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 82

Keep the Cost of the Search Down

Make sure that the first n fields of the designated index are stated with EQ (or IN) within the WHERE clause.

Find total revenue for Euro zone carriers in September 2003.

SELECT SUM( loccuram ) FROM sbook INTO amountWHERE carrid IN ( SELECT carrid FROM scarr

WHERE currcode = 'EUR' )AND connid = '0400'AND fldate like '200309%'AND loccurkey = 'EUR'.

SELECT SUM( loccuram ) FROM sbookINTO amountWHERE connid = '0400'AND fldate LIKE '200309%'AND loccurkey = 'EUR'.

Page 42: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 42

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 83

Keep the Cost of the Search Down

Replace an inner OR with an IN operator.

Find the dates for LH0300 and LH0302 in 2003.

SELECT * FROM sflightINTO xflight WHERE carrid = 'LH' AND connid IN ('0300', '0302')AND fldate LIKE '2003%'.

WRITE: / xflight-fldate.ENDSELECT.

SELECT * FROM sflightINTO xflight WHERE carrid = 'LH' AND ( connid = '0300' OR connid = '0302' ) AND fldate LIKE '2003%'.

WRITE: / xflight-fldate.ENDSELECT.

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 84

Keep the Cost of the Search Down

Indexes don't support NOT operators.

Find total revenue for all carriers except LH in September 2003.

SELECT SUM( loccuram ) FROM sbook INTO AMOUNTWHERE carrid IN ( SELECT carrid FROM scarr

WHERE carrid <> 'LH' )AND connid = '0400'AND fldate like '200309%'.

SELECT loccuram FROM sbookINTO amountWHERE carrid <> 'LH'AND connid = '0400'AND fldate LIKE '200309%'.

Page 43: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 43

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 85

Keep the Cost of the Search Down

Think about optimizer hints if the optimizer fails to find a sound execution plan.

Find all LH flights starting in Frankfurt.

SELECT connid citytoFROM spfli INTO (xconnid, xcityto) WHERE carrid = 'LH' AND cityfrom = 'FRANKFURT'

%_HINTS ORACLE 'INDEX("SPFLI" "SPFLI~001")'.WRITE: / xconnid, xcityto.

ENDSELECT.

SELECT connid citytoFROM spfli INTO (xconnid, xcityto) WHERE carrid = 'LH' AND cityfrom = 'FRANKFURT'.

WRITE: / xconnid, xcityto.ENDSELECT.

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 86

App Server 1

Keep the Cost of the Search Down: Effects

DB Server

Database files

DB work processDB CPUconsumption

DB memoryconsumption

App ServerCPU consumption

App Servermemoryconsumption

App Server 2

R/3 work process R/3 work process R/3 work process

Table Buffer Table Buffer

DB work processDB work process

LANcommunication

physicalI/O

Database Service

ProcessesDatabase Cache

Database Service

Processes

Page 44: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 44

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 87

Agenda

Web AS and Database ArchitectureClient / Server TechnologyTable BufferingIndexes

How to Identify Expensive SQL

Rules for Better SQL ProgrammingTheory and Hands-On

Rule 1Rule 2Rule 3Rule 4Rule 5

Summary

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 88

Rule 5: Remove the Load From the Database

Remove the load

from the database!

Page 45: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 45

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 89

Remove the Load From the Database

Avoid reading the same data again and again.

SELECT SINGLE * FROM scarrINTO xcarrWHERE carrid = 'LH'.

...zcarr = xcarr....

SELECT SINGLE * FROM scarrINTO xcarrWHERE carrid = 'LH'.

...SELECT SINGLE * FROM scarrINTO zcarrWHERE carrid = 'LH'.

...

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 90

Remove the Load From the Database

When to apply table buffering:the table is frequently read,relatively small, and deferred visibility of changes is acceptable.

When to avoid table buffering:the table is heavily changed, andSELECTs must always deliver up-to-date data.

Check whether a table meets the criteria for table buffering.

Page 46: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 46

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 91

Remove the Load From the Database

SELECT ... DISTINCTSELECT ... COUNT( ), SUM( ), AVG( ), MIN( ), MAX( )SELECT ... ORDER BY f1 ... fnSELECT ... GROUP BY / HAVINGSELECT ... FOR UPDATESELECT ... JOINWHERE clause contains IS NULL statementWHERE clause contains subquery

SELECT ... BYPASSING BUFFER

Statements that Bypass the Table Buffer

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 92

Remove the Load From the Database

Avoid the ORDER BY clause if the desired sorting doesn’t correspond to the index used.

Get all LH flights with their connection numbers and cities of departure and arrival.

SELECT connid cityfrom cityto FROM spfliINTO TABLE flightsWHERE carrid = 'LH'.

SORT flights BY cityto cityfrom.

SELECT connid cityfrom cityto FROM spfliINTO TABLE flightsWHERE carrid = 'LH'ORDER BY cityto cityfrom.

Page 47: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 47

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 93

App Server 1

Remove the Load From the Database: Effects

DB Server

Database files

DB work processDB CPUconsumption

DB memoryconsumption

App ServerCPU consumption

App Servermemoryconsumption

App Server 2

R/3 work process R/3 work process R/3 work process

Table Buffer Table Buffer

DB work processDB work process

LANcommunication

physicalI/O

Database Service

ProcessesDatabase Cache

Database Service

Processes

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 94

Golden Rule

Think and experiment!

Page 48: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 48

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 95

Think and Experiment

Take recommendations as rules of thumb rather than laws.

Some of the rules unveil their benefits only if you use tables of a certain minimum capacity.

Some of the goals of the rules are even inconsistent.

Recommendations hold true for all SAP-supported DB systems.

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 96

Agenda

Web AS and Database ArchitectureClient / Server TechnologyTable BufferingIndexes

How to Identify Expensive SQL

Rules for Better SQL ProgrammingTheory and Hands-On

Rule 1Rule 2Rule 3Rule 4Rule 5

Summary

Page 49: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 49

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 97

Summary

There is just one database server

Use buffers and indexes

Check their usage via SQL trace

Try to stick to the presented rules:Small hit listMinimize transfersMinimize number of round tripsNarrow your searchMinimize database load

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 98

Further Information

Public Web:SAP Customer Services Network: www.sap.com/services/SAP Help Portal: help.sap.com

Related Workshops/Lectures at SAP TechEd 2003ABAP251, "ABAP: Traps and Pitfalls" 2-hr Hands-onABAP255, "ABAP for Power Users" 4-hr Hands-onBW354, "Performance Tuning for SAP BW 4-hr Hands-on

Related SAP Education Training Opportunitieshttp://www.sap.com/usa/education/BC414 Programming Database UpdatesBC490 ABAP Performance TuningWNAT10 Developing User Dialog and Database Updates

Consulting ContactRoy Wood, VP SAP NetWeaver Consulting Practice ([email protected])

Page 50: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 50

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 99

Q&A

Questions?

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 100

Feedback

Please complete your session evaluation and drop it in the box on

your way out.

Be courteous — deposit your trash, and do not take the handouts for the

following session.

The SAP TechEd ’03 Las Vegas Team

Page 51: ABAP152 - Efficient Database Programming With ABAP

SAP TechEd ‘03 Las Vegas

© 2003 SAP Labs, LLC ABAP152, Ulrich Koch 51

SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 101

No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP AG. The information contained herein may be changed without prior notice.

Some software products marketed by SAP AG and its distributors contain proprietary software components of other software vendors.

Microsoft®, WINDOWS®, NT®, EXCEL®, Word®, PowerPoint® and SQL Server® are registered trademarks of Microsoft Corporation.

IBM®, DB2®, DB2 Universal Database, OS/2®, Parallel Sysplex®, MVS/ESA, AIX®, S/390®, AS/400®, OS/390®, OS/400®, iSeries, pSeries, xSeries, zSeries, z/OS, AFP, Intelligent Miner, WebSphere®, Netfinity®, Tivoli®, Informix and Informix® Dynamic ServerTM are trademarks of IBM Corporation in USA and/or other countries.

ORACLE® is a registered trademark of ORACLE Corporation.

UNIX®, X/Open®, OSF/1®, and Motif® are registered trademarks of the Open Group.

Citrix®, the Citrix logo, ICA®, Program Neighborhood®, MetaFrame®, WinFrame®, VideoFrame®, MultiWin® and other Citrix product names referenced herein are trademarks of Citrix Systems, Inc.

HTML, DHTML, XML, XHTML are trademarks or registered trademarks of W3C®, World Wide Web Consortium, Massachusetts Institute of Technology.

JAVA® is a registered trademark of Sun Microsystems, Inc.

JAVASCRIPT® is a registered trademark of Sun Microsystems, Inc., used under license for technology invented and implemented by Netscape.

MarketSet and Enterprise Buyer are jointly owned trademarks of SAP AG and Commerce One.

SAP, R/3, mySAP, mySAP.com, xApps, xApp and other SAP products and services mentioned herein as well astheir respective logos are trademarks or registered trademarks of SAP AG in Germany and in several other countries all over the world. All other product and service names mentioned are the trademarks of their respective companies.

Copyright 2003 SAP AG. All Rights Reserved