0% found this document useful (0 votes)
41 views

Query SQL Inner Join - US

SAP TechEd '03 Las Vegas (c) 2003 SAP Labs, LLC ABAP152, Ulrich Koch 1 Nov. 12-15, 2002 (session id) this workshop explains the communication between database and Application Server. A "normal" ABAP statement executes within microseconds. A database access normally needs a few milliseconds or even seconds to return a result.

Uploaded by

hiso1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Query SQL Inner Join - US

SAP TechEd '03 Las Vegas (c) 2003 SAP Labs, LLC ABAP152, Ulrich Koch 1 Nov. 12-15, 2002 (session id) this workshop explains the communication between database and Application Server. A "normal" ABAP statement executes within microseconds. A database access normally needs a few milliseconds or even seconds to return a result.

Uploaded by

hiso1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

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 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
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 Architecture
Client / Server Technology
Table Buffering
Indexes
How to Identify Expensive SQL
Rules for Better SQL Programming
Theory and Hands-On
Rule 1
Rule 2
Rule 3
Rule 4
Rule 5
Summary
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 server
and application server(s)
Frontend(s) / Presentation Server(s)
DB Server
DB
cache
DB
WP
local
data WP
SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 6
WAS and Database Architecture (II)
R/3 work process R/3 work process
DB work process DB work process
Database cache
Database
Service
processes
Operating system
Database files
R/3 work process
DB work process
LAN
communication
DB CPU
consumption
DB memory
consumption
Physical I/O
Table Buffer
App Server
CPU consumption
App Server
memory
consumption
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 Architecture
Client / Server Technology
Table Buffering
Indexes
How to Identify Expensive SQL
Rules for Better SQL Programming
Theory and Hands-On
Rule 1
Rule 2
Rule 3
Rule 4
Rule 5
Summary
SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 8
Table Buffering
Goal
Cache portions of a database table in the application server to reduce
database load and network communication
Functionality
Buffering can be enabled for individual tables
Buffering is transparent to the application program
The 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 buffered
N = #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 buffer
A modification operation concerning one key range is performed in the local
buffer and invalidates the corresponding key ranges in remote buffers
Invalidation requests are send through buffer synchronization
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)
8 D 003
6 D 003
2 D 003
5 C 003
4 B 003
2 B 003
6 A 003
3 A 003
2 A 003
5 D 002
1 C 002
0 C 002
3 B 002
2 B 002
1 B 002
8 A 002
6 A 002
3 A 002
1 A 002
5 B 001
3 B 001
1 B 001
4 A 001
2 A 001
data key3 key2 key1
Generic buffering
(two key fields)
8 D 003
6 D 003
2 D 003
5 C 003
4 B 003
2 B 003
6 A 003
3 A 003
2 A 003
5 D 002
1 C 002
0 C 002
3 B 002
2 B 002
1 B 002
8 A 002
6 A 002
3 A 002
1 A 002
5 B 001
3 B 001
1 B 001
4 A 001
2 A 001
data key3 key2 key1
Single row buffering
(all key fields)
8 D 003
6 D 003
2 D 003
5 C 003
4 B 003
2 B 003
6 A 003
3 A 003
2 A 003
5 D 002
1 C 002
0 C 002
3 B 002
2 B 002
1 B 002
8 A 002
6 A 002
3 A 002
1 A 002
5 B 001
3 B 001
1 B 001
4 A 001
2 A 001
data key3 key2 key1
Generic buffering
(one key field)
8 D 003
6 D 003
2 D 003
5 C 003
4 B 003
2 B 003
6 A 003
3 A 003
2 A 003
5 D 002
1 C 002
0 C 002
3 B 002
2 B 002
1 B 002
8 A 002
6 A 002
3 A 002
1 A 002
5 B 001
3 B 001
1 B 001
4 A 001
2 A 001
data key3 key2 key1
SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 10
Buffer Synchronization Example (I)
Server 1
Server 2
Table Buffer
Database
Table Buffer
T
T
T
DDLOG
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
T
T
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
T
T
DDLOG
Update on T
INSERT
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
T
T
DDLOG
Update on T
INSERT READ
SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 14
Agenda
Web AS and Database Architecture
Client / Server Technology
Table Buffering
Indexes
How to Identify Expensive SQL
Rules for Better SQL Programming
Theory and Hands-On
Rule 1
Rule 2
Rule 3
Rule 4
Rule 5
Summary
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:31 13:30 SFO JFK 0017 AA
08:15 07:10 FRA TXL 2407 LH
17:00 15:00 UNL GKS 0815 AA
15:05 13:30 JFK FRA 0402 LH
18:25 10:00 JFK SFO 1984 DL
17:55 14:45 SFO JFK 0007 UA
10:30 15:00 FRA SFO 0455 LH
12:30 10:10 SFO FRA 0454 LH
arrtime deptime airpto airpfrom connid carrid
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, Bernd
Berlin
Burton, Robert
New Orleans
Zarcone, Pia
Rome
Daum, Paul
Bremen
Adams, Tim
Miami
Maier, Kurt
Hamburg
Stewart, Ron
Edinburgh
Maier, Bernd
Berlin
Burton, Robert
New Orleans
Zarcone, Pia
Rome
Daum, Paul
Bremen
Adams, Tim
Miami
Maier, Kurt
Hamburg
Stewart, Ron
Edinburgh
Berlin
Bremen
Edinburgh
Hamburg
Miami
New Orleans
Rome
Berlin
Bremen
Edinburgh
Hamburg
Miami
New Orleans
Rome
Table Index (Name)
Index (City)
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
airpto airpfrom
cityfrom cityto
cityto cityfrom
connid
SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 18
Access by Primary Index
16:31 13:30 SFO JFK 0017 AA
08:15 07:10 FRA TXL 2407 LH
14:00 12:00 UNL GKS 0815 AA
15:05 13:30 JFK FRA 0402 LH
18:25 10:00 JFK SFO 1984 DL
17:55 14:45 SFO JFK 0007 UA
10:30 15:00 FRA SFO 0455 LH
12:30 10:10 SFO FRA 0454 LH
arrtime deptime airpto airpfrom connid carrid
0007 UA
2407 LH
0455 LH
0454 LH
0402 LH
1984 DL
0815 AA
0017 AA
connid carrid
SPFLI
SELECT * FROM spfli
WHERE carrid = 'AA' AND connid = '0815'.
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:31 13:30 SFO JFK 0017 AA
08:15 07:10 FRA TXL 2407 LH
14:00 12:00 UNL GKS 0815 AA
15:05 13:30 JFK FRA 0402 LH
18:25 10:00 JFK SFO 1984 DL
17:55 14:45 SFO JFK 0007 UA
10:30 15:00 FRA SFO 0455 LH
12:30 10:10 SFO FRA 0454 LH
arrtime deptime airpto airpfrom connid carrid
0007 UA
2407 LH
0455 LH
0454 LH
0402 LH
1984 DL
0815 AA
0017 AA
connid carrid
FRA TXL
JFK SFO
FRA SFO
SFO JFK
SFO JFK
UNL GKS
SFO FRA
JFK FRA
airpto airpfrom
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.
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 sflight
INTO xflight
WHERE 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 Architecture
Client / Server Technology
Table Buffering
Indexes
How to Identify Expensive SQL
Rules for Better SQL Programming
Theory and Hands-On
Rule 1
Rule 2
Rule 3
Rule 4
Rule 5
Summary
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 the
SQL trace:
"Trace off"
5. List the SQL
statements
recorded:
"List trace"
2. Switch on the
SQL trace:
"Trace on"
SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 24
SQL Trace
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
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 Architecture
Client / Server Technology
Table Buffering
Indexes
How to Identify Expensive SQL
Rules for Better SQL Programming
Theory and Hands-On
Rule 1
Rule 2
Rule 3
Rule 4
Rule 5
Summary
SAP TechEd 03 Las Vegas
2003 SAP Labs, LLC ABAP152, Ulrich Koch 15
SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 29
database
FROM clause
one or more tables
Index access
search 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 Architecture
Client / Server Technology
Table Buffering
Indexes
How to Identify Expensive SQL
Rules for Better SQL Programming
Theory and Hands-On
Rule 1
Rule 2
Rule 3
Rule 4
Rule 5
Summary
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 sflight
INTO xflight
WHERE carrid = 'LH'
AND connid = '0300'.
WRITE: / xflight-fldate.
ENDSELECT.

SELECT * FROM sflight


INTO xflight.
CHECK xflight-carrid = 'LH'.
CHECK xflight-connid = '0300'.
WRITE: / xflight-fldate.
ENDSELECT.

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 xflight
WHERE 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 process
DB CPU
consumption
DB memory
consumption
App Server
CPU consumption
App Server
memory
consumption
App Server 2
R/3 work process R/3 work process
R/3 work process
Table Buffer Table Buffer
DB work process DB work process
LAN
communication
physical
I/O
Database
Service
Processes
Database Cache
Database
Service
Processes
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 Architecture
Client / Server Technology
Table Buffering
Indexes
How to Identify Expensive SQL
Rules for Better SQL Programming
Theory and Hands-On
Rule 1
Rule 2
Rule 3
Rule 4
Rule 5
Summary
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 sflight


INTO xflight
WHERE carrid = 'LH'
AND connid = '0300'
AND fldate LIKE '2003%'.
WRITE: / xflight-fldate.
ENDSELECT.

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.
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 ROWS
INTO (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 xbook
WHERE carrid = 'LH'
AND connid = '0400'
AND fldate >= '20040101'.
xbook-connid = '0500'.
UPDATE sbook FROM xbook.
ENDSELECT.

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 sum
WHERE carrid = 'LH'
AND fldate LIKE '2003%'.
WRITE: / sum.

sum = 0.
SELECT seatsocc
FROM sflight INTO xseatsocc
WHERE 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 sbook
GROUP BY carrid connid fldate
HAVING MAX( luggweight ) > 20.
WRITE: / xcarrid, xconnid, xfldate, max.
ENDSELECT.

SELECT carrid connid fldate MAX( luggweight )


INTO (xcarrid, xconnid, xfldate, max)
FROM sbook
GROUP BY carrid connid fldate.
CHECK max > 20.
WRITE: / xcarrid, xconnid, xfldate, max.
ENDSELECT.

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 process
DB CPU
consumption
DB memory
consumption
App Server
CPU consumption
App Server
memory
consumption
App Server 2
R/3 work process R/3 work process
R/3 work process
Table Buffer Table Buffer
DB work process DB work process
LAN
communication
physical
I/O
Database
Service
Processes
Database 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 programto 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 programto see the effect of your optimization.
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 Architecture
Client / Server Technology
Table Buffering
Indexes
How to Identify Expensive SQL
Rules for Better SQL Programming
Theory and Hands-On
Rule 1
Rule 2
Rule 3
Rule 4
Rule 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!
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 process
INSERT 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 process
INSERT INTO sflight
VALUES flight_wa.
INSERT INTO sflight
VALUES flight_wa.
Data to insert
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 process
INSERT 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 process
INSERT INTO sflight
FROM TABLE flight_tab.
INSERT INTO sflight
FROM TABLE flight_tab.
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 FOR
SELECT 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)
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 sflight
INTO TABLE seatsocc_tab
FOR ALL ENTRIES IN conn_tab
WHERE carrid = conn_tab-carrid
AND connid = conn_tab-connid
AND fldate LIKE '200309%'.
SELECT FOR ALL ENTRIES (I)
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 seatsocc
FROM sflight
INTO TABLE seatsocc_tab
WHERE
( carrid = <row 1 of conn_tab>-carrid AND
connid = <row 1 of conn_tab>-connid AND
fldate LIKE '200309%' )
OR
( carrid = <row 2 of conn_tab>-carrid AND
connid = <row 2 of conn_tab>-connid AND
fldate LIKE '200309%' )
OR ...
OR
( carrid = <row n of conn_tab>-carrid AND
connid = <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 another
SELECT statement then a JOIN is often the better solution.
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/09 0402 LH NEW YORK 0402 LH
2002/11/08 0402 LH NEW YORK 0402 LH
2002/11/12 0017 AA FRANKFURT 0017 AA
2002/11/07 0017 AA FRANKFURT 0017 AA
2002/11/09 0402 LH
2002/11/08 0402 LH
2002/11/12 0017 AA
2002/11/07 0017 AA
fldate connid carrid
NEW YORK 0598 QF
FRANKFURT 0440 LH
NEW YORK 0402 LH
FRANKFURT 0017 AA
cityfrom connid carrid
fldate connid carrid cityfrom connid carrid
Inner Join
SPFLI SFLIGHT
SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 60
INNER JOIN
2002/11/09 0402 LH NEW YORK 0402 LH
2002/11/08 0402 LH NEW YORK 0402 LH
2002/11/12 0017 AA FRANKFURT 0017 AA
2002/11/07 0017 AA FRANKFURT 0017 AA
2002/11/09 0402 LH
2002/11/08 0402 LH
2002/11/12 0017 AA
2002/11/07 0017 AA
fldate connid carrid
NEW YORK 0598 QF
FRANKFURT 0440 LH
NEW YORK 0402 LH
FRANKFURT 0017 AA
cityfrom connid carrid
fldate connid carrid cityfrom connid carrid
Inner Join
SPFLI SFLIGHT
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
NULL NULL NULL NEW YORK 0598 QF
NULL NULL NULL FRANKFURT 0440 LH
2002/11/09 0402 LH
2002/11/08 0402 LH
2002/11/12 0017 AA
2002/11/07 0017 AA
fldate connid carrid
NEW YORK 0598 QF
FRANKFURT 0440 LH
NEW YORK 0402 LH
FRANKFURT 0017 AA
cityfrom connid carrid
2002/11/09 0402 LH NEW YORK 0402 LH
2002/11/08 0402 LH NEW YORK 0402 LH
2002/11/12 0017 AA FRANKFURT 0017 AA
2002/11/07 0017 AA FRANKFURT 0017 AA
fldate connid carrid cityfrom connid carrid
Left Outer Join
SPFLI SFLIGHT
SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 62
LEFT OUTER JOIN
NULL NULL NULL NEW YORK 0598 QF
NULL NULL NULL FRANKFURT 0440 LH
2002/11/09 0402 LH NEW YORK 0402 LH
2002/11/08 0402 LH NEW YORK 0402 LH
2002/11/12 0017 AA FRANKFURT 0017 AA
2002/11/07 0017 AA FRANKFURT 0017 AA
2002/11/09 0402 LH
2002/11/08 0402 LH
2002/11/12 0017 AA
2002/11/07 0017 AA
fldate connid carrid
NEW YORK 0598 QF
FRANKFURT 0440 LH
NEW YORK 0402 LH
FRANKFURT 0017 AA
cityfrom connid carrid
fldate connid carrid cityfrom connid carrid
Left Outer Join
SPFLI SFLIGHT
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 a
secondary table exist or not. You dont 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 connid
INTO (xcarrid, xconnid)
FROM sflight AS f
WHERE NOT EXISTS ( SELECT * FROM spfli
WHERE carrid = f~carrid
AND 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.

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~connid
AND 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 = xcarrid
AND 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 fldate
FROM sflight AS f
INTO (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 yfldate
WHERE carrid = xcarrid AND
connid = xconnid AND
seatsocc = max.
WRITE: / xcarrid, xconnid, yfldate.
ENDSELECT.
ENDSELECT.

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
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 process
DB CPU
consumption
DB memory
consumption
App Server
CPU consumption
App Server
memory
consumption
App Server 2
R/3 work process R/3 work process
R/3 work process
Table Buffer Table Buffer
DB work process DB work process
LAN
communication
physical
I/O
Database
Service
Processes
Database 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.
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 b
INTO sum
WHERE carrid = 'LH'
AND loccurkey = 'EUR'
AND EXISTS ( SELECT * FROM sflight AS f
WHERE f~carrid = b~carrid
AND f~connid = b~connid
AND f~fldate = b~fldate
AND 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 sbook
INTO sbook_wa-loccuram
WHERE carrid = xcarrid
AND connid = xconnid
AND fldate = xfldate
AND 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.
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.
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 Architecture
Client / Server Technology
Table Buffering
Indexes
How to Identify Expensive SQL
Rules for Better SQL Programming
Theory and Hands-On
Rule 1
Rule 2
Rule 3
Rule 4
Rule 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!
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 field3
FROM table1
WHERE field1 IN ('A','B') AND field3 like 'T%'
SELECT
clause
WHERE clause
FROM clause
search area
One or more tables
Data which must be
searched through -
limited only by
the 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 significantly
Thus 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 buffer
Additional 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 set
However, due to a restriction in the number of indexes per table
compromises have to be made if a lot of different accesses are required
So in reality it is enough if the index reduces the search area close to the
result set
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 wa
WHERE price BETWEEN 300 AND 700.
key1 key2
001
001
001
001
001
002
002
002
002
002
A
A
B
B
B
A
A
A
A
B
MANDT PRICE
1
1
1
1
1
102
10
102
102
102
400
600
500
700
1
400
3
4
5
300
Index 1
Index 2
001
001
001
001
001
A
A
B
B
B
20
20
20
20
20
400
600
500
700
300
001
001
001
001
001
A
A
B
B
B
30
30
30
30
30
400
600
500
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 A 10 500
002 A 10 600
002 A 10 700
002 A 10 300
key1 key2
001
001
001
001
001
002
002
002
002
002
PRICE MANDT
300
1
300
300
400
2
2
400
500
3
001
001
001
001
001
500
500
600
4
600
001
001
001
001
001
600
700
5
700
700
001
001
001
001
001
002
002
002
002
002
1
102
20
30
1
102
102
30
1
102
001
001
001
001
001
20
30
1
102
20
001
001
001
001
001
30
1
102
20
30
002
002
400
400
002
002
10
20
002 2 002 102 002 500 002 10
002 2 002 102 002 600 002 10
002 2 002 102 002 700 002 10
002 2 002 102 002 300 002 10
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.
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 it
is 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 amount
WHERE carrid IN ( SELECT carrid FROM scarr
WHERE currcode = 'EUR' )
AND connid = '0400'
AND fldate like '200309%'
AND loccurkey = 'EUR'.

SELECT SUM( loccuram ) FROM sbook


INTO amount
WHERE connid = '0400'
AND fldate LIKE '200309%'
AND loccurkey = 'EUR'.

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 sflight
INTO xflight
WHERE carrid = 'LH'
AND connid IN ('0300', '0302')
AND fldate LIKE '2003%'.
WRITE: / xflight-fldate.
ENDSELECT.

SELECT * FROM sflight


INTO 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 AMOUNT
WHERE carrid IN ( SELECT carrid FROM scarr
WHERE carrid <> 'LH' )
AND connid = '0400'
AND fldate like '200309%'.

SELECT loccuram FROM sbook


INTO amount
WHERE carrid <> 'LH'
AND connid = '0400'
AND fldate LIKE '200309%'.

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 cityto
FROM spfli INTO (xconnid, xcityto)
WHERE carrid = 'LH'
AND cityfrom = 'FRANKFURT'
%_HINTS ORACLE 'INDEX("SPFLI" "SPFLI~001")'.
WRITE: / xconnid, xcityto.
ENDSELECT.

SELECT connid cityto


FROM 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 process
DB CPU
consumption
DB memory
consumption
App Server
CPU consumption
App Server
memory
consumption
App Server 2
R/3 work process R/3 work process
R/3 work process
Table Buffer Table Buffer
DB work process DB work process
LAN
communication
physical
I/O
Database
Service
Processes
Database Cache
Database
Service
Processes
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 Architecture
Client / Server Technology
Table Buffering
Indexes
How to Identify Expensive SQL
Rules for Better SQL Programming
Theory and Hands-On
Rule 1
Rule 2
Rule 3
Rule 4
Rule 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!
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 scarr
INTO xcarr
WHERE carrid = 'LH'.
...
zcarr = xcarr.
...

SELECT SINGLE * FROM scarr


INTO xcarr
WHERE carrid = 'LH'.
...
SELECT SINGLE * FROM scarr
INTO zcarr
WHERE 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, and
SELECTs must always deliver up-to-date data.
Check whether a table meets the criteria for table buffering.
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 ... DISTINCT
SELECT ... COUNT( ), SUM( ), AVG( ), MIN( ), MAX( )
SELECT ... ORDER BY f1 ... fn
SELECT ... GROUP BY / HAVING
SELECT ... FOR UPDATE
SELECT ... JOIN
WHERE clause contains IS NULL statement
WHERE clause contains subquery
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 doesnt
correspond to the index used.
Get all LH flights with their connection numbers and cities of departure and arrival.
SELECT connid cityfrom cityto FROM spfli
INTO TABLE flights
WHERE carrid = 'LH'.
SORT flights BY cityto cityfrom.

SELECT connid cityfrom cityto FROM spfli


INTO TABLE flights
WHERE carrid = 'LH'
ORDER BY cityto cityfrom.

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 process
DB CPU
consumption
DB memory
consumption
App Server
CPU consumption
App Server
memory
consumption
App Server 2
R/3 work process R/3 work process
R/3 work process
Table Buffer Table Buffer
DB work process DB work process
LAN
communication
physical
I/O
Database
Service
Processes
Database Cache
Database
Service
Processes
SAP AG 2003, ABAP152, Ulrich Koch, Tobias Wenner / 94
Golden Rule
Think and experiment!
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 Architecture
Client / Server Technology
Table Buffering
Indexes
How to Identify Expensive SQL
Rules for Better SQL Programming
Theory and Hands-On
Rule 1
Rule 2
Rule 3
Rule 4
Rule 5
Summary
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 list
Minimize transfers
Minimize number of round trips
Narrow your search
Minimize 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 2003


ABAP251, "ABAP: Traps and Pitfalls" 2-hr Hands-on
ABAP255, "ABAP for Power Users" 4-hr Hands-on
BW354, "Performance Tuning for SAP BW 4-hr Hands-on

Related SAP Education Training Opportunities


http://www.sap.com/usa/education/
BC414 Programming Database Updates
BC490 ABAP Performance Tuning
WNAT10 Developing User Dialog and Database Updates

Consulting Contact
Roy Wood, VP SAP NetWeaver Consulting Practice ([email protected])
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
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 Server
TM
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 as
their 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

You might also like