efficient database programming with abap - community … · ©2004 sap ag, abap210 3 agenda was and...
TRANSCRIPT
![Page 1: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/1.jpg)
Efficient Database Programming with ABAP
![Page 2: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/2.jpg)
© 2004 SAP AG, ABAP210 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 indices workUse Open SQL for efficient database access
![Page 3: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/3.jpg)
© 2004 SAP AG, ABAP210 3
Agenda
WAS and Database Architecture
ABAP Open SQL Overview
How to Identify Expensive SQL
Rules for Better SQL ProgrammingTheory andHands-On
Summary
![Page 4: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/4.jpg)
© 2004 SAP AG, ABAP210 4
Performance of Business Transactions
General rule:The performance of a business transaction is primarily determined by its DB accesses.
DB
Application
GUI
![Page 5: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/5.jpg)
© 2004 SAP AG, ABAP210 5
WAS and Database Architecture
Application Server(s)
User communication
Data transfer between DB serverand application server(s)
Frontend(s) / Presentation Server(s)
DB Server
DBcache
DB WP
localdataWP
![Page 6: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/6.jpg)
© 2004 SAP AG, ABAP210 6
WAS and Database Architecture
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 7: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/7.jpg)
© 2004 SAP AG, ABAP210 7
Table Buffering: Types
Full buffering(no key fields)
8D0036D0032D0035C0034B0032B0036A0033A0032A0035D0021C0020C0023B0022B0021B0028A0026A0023A0021A0025B0013B0011B0014A0012A001
datakey3key2key1
Generic buffering(two key fields)
8D0036D0032D0035C0034B0032B0036A0033A0032A0035D0021C0020C0023B0022B0021B0028A0026A0023A0021A0025B0013B0011B0014A0012A001
datakey3key2key1
Single-record buffering(all key fields)
8D0036D0032D0035C0034B0032B0036A0033A0032A0035D0021C0020C0023B0022B0021B0028A0026A0023A0021A0025B0013B0011B0014A0012A001
datakey3key2key1
Generic buffering(one key field)
8D0036D0032D0035C0034B0032B0036A0033A0032A0035D0021C0020C0023B0022B0021B0028A0026A0023A0021A0025B0013B0011B0014A0012A001
datakey3key2key1
![Page 8: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/8.jpg)
© 2004 SAP AG, ABAP210 8
Table Buffering: Invalidation Policy
Local Buffer
Full Buffering:In case of a Workarea-Update, the corresponding row is updated in the buffer.Otherwise, the entire buffer of table t is invalidated.
Generic Buffering:In case of a Workarea-Update, the corresponding row is updated in the buffer. If there is a change in only the generic area, only this area is invalidated. Otherwise, the entire buffer of table t is invalidated.
Single-Record Buffering:If there is a change in only a single row, only this row is invalidated.Otherwise, the entire buffer of table t is invalidated.
Local Buffer
Full Buffering:In case of a Workarea-Update, the corresponding row is updated in the buffer.Otherwise, the entire buffer of table t is invalidated.
Generic Buffering:In case of a Workarea-Update, the corresponding row is updated in the buffer. If there is a change in only the generic area, only this area is invalidated. Otherwise, the entire buffer of table t is invalidated.
Single-Record Buffering:If there is a change in only a single row, only this row is invalidated.Otherwise, the entire buffer of table t is invalidated.
![Page 9: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/9.jpg)
© 2004 SAP AG, ABAP210 9
Remote Buffers
Full Buffering:Any change invalidates the entire buffer of table t.
Generic Buffering:If there is a change in only the generic area, only this area is invalidated.Otherwise, the entire buffer of table t is invalidated.
Single-Record Buffering:If there is a change in only a single row, only this row is invalidated.Otherwise, the entire buffer of table t is invalidated.
Remote Buffers
Full Buffering:Any change invalidates the entire buffer of table t.
Generic Buffering:If there is a change in only the generic area, only this area is invalidated.Otherwise, the entire buffer of table t is invalidated.
Single-Record Buffering:If there is a change in only a single row, only this row is invalidated.Otherwise, the entire buffer of table t is invalidated.
Table Buffering: Invalidation Policy
![Page 10: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/10.jpg)
© 2004 SAP AG, ABAP210 10
Buffer Synchronisation – Example (1)
Server 1 Server 2
Table Buffer
Database
Table Buffer
T
TT
DDLOG
![Page 11: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/11.jpg)
© 2004 SAP AG, ABAP210 11
Buffer Synchronisation – Example (2)
Server 1 Server 2
Table Buffer
Database
Table Buffer
T
TT
DDLOG
Update on T
![Page 12: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/12.jpg)
© 2004 SAP AG, ABAP210 12
Buffer Synchronisation – Example (3)
Server 1 Server 2
Table Buffer
Database
Table Buffer
T
TT
DDLOG
Update on T
INSERT
![Page 13: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/13.jpg)
© 2004 SAP AG, ABAP210 13
Buffer Synchronisation – Example (4)
Server 1 Server 2
Table Buffer
Database
Table Buffer
T
TT
DDLOG
Update on T
INSERT READ
![Page 14: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/14.jpg)
© 2004 SAP AG, ABAP210 14
Statement Optimizer
Execution Plan
SELECT * FROM sflightINTO xflightWHERE cityfrom = ‘OSLO’
AND fldate = ‘20020904’ORDER BY carrid.
rule-based
cost-based
... decides how to execute the SQL statement
![Page 15: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/15.jpg)
© 2004 SAP AG, ABAP210 15
Tables and Indices
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, RonEdinborough
Maier, BerndBerlin
Burton, RobertNew Orleans
Zarcone, PiaRome
Daum, PaulBremen
Adams, TimMiami
Maier, KurtHamburg
Stewart, RonEdinborough
Berlin
Bremen
Edinborough
Hamburg
Miami
New Orleans
Rome
Berlin
Bremen
Edinborough
Hamburg
Miami
New Orleans
Rome
TableIndex (Name) Index (City)
![Page 16: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/16.jpg)
© 2004 SAP AG, ABAP210 16
Access by Full Table Scan
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
SPFLI
SELECT * FROM spfliWHERE deptime = '150000'.
![Page 17: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/17.jpg)
© 2004 SAP AG, ABAP210 17
...
Tables and Indices
Primary Key
Primary index• implicitly defined• unique
Secondary indices• user defined• unique or non-unique
spfli
... ...
mandt carrid connid
mandt carrid
airpfrom airpto
airptoairpfrom
cityfrom cityto
citytocityfromconnid
![Page 18: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/18.jpg)
© 2004 SAP AG, ABAP210 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 spfliWHERE carrid = 'LH' AND connid = '0455'.
![Page 19: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/19.jpg)
© 2004 SAP AG, ABAP210 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 spfliWHERE airpfrom = 'SFO' AND airpto = 'FRA'.
![Page 20: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/20.jpg)
© 2004 SAP AG, ABAP210 20
Indices
Indices can vastly improve performance when searching fordata.Indices will slightly slow down updates.A bad index is worse than none at all because data blocksmight be read again and again.Sometimes a Full Table Scan is faster. The optimizer shoulddo it right.
![Page 21: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/21.jpg)
© 2004 SAP AG, ABAP210 21
Data Transfer: Single Row INSERT
R/3 work process DB work processINSERT INTO sflightVALUES flight_wa.
INSERT INTO sflightVALUES flight_wa.
Data to insert
![Page 22: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/22.jpg)
© 2004 SAP AG, ABAP210 22
Data Transfer: Single Row INSERT
R/3 work process DB work processINSERT INTO sflightVALUES flight_wa.
INSERT INTO sflightVALUES flight_wa.
Data to insert
![Page 23: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/23.jpg)
© 2004 SAP AG, ABAP210 23
Data Transfer: Array INSERT
R/3 work process DB work processINSERT INTO sflightFROM TABLE flight_tab.
INSERT INTO sflightFROM TABLE flight_tab.
Data to insert
![Page 24: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/24.jpg)
© 2004 SAP AG, ABAP210 24
Data Transfer: Array INSERT
R/3 work process DB work processINSERT INTO sflightFROM TABLE flight_tab.
INSERT INTO sflightFROM TABLE flight_tab.
![Page 25: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/25.jpg)
© 2004 SAP AG, ABAP210 25
Agenda
WAS and Database Architecture
ABAP Open SQL Overview
How to Identify Expensive SQL
Rules for Better SQL ProgrammingTheory andHands-On
Summary
![Page 26: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/26.jpg)
© 2004 SAP AG, ABAP210 26
Open SQL Statements
SELECT
OPEN CURSOR, FETCH NEXT, CLOSE CURSOR
INSERT
UPDATE
MODIFY
DELETE
COMMIT WORK
ROLLBACK WORK
![Page 27: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/27.jpg)
© 2004 SAP AG, ABAP210 27
Inner Join
2002/11/090402LHNEW YORK0402LH2002/11/080402LHNEW YORK0402LH2002/11/120017AAFRANKFURT0017AA2002/11/070017AAFRANKFURT0017AA
2002/11/090402LH2002/11/080402LH2002/11/120017AA2002/11/070017AAfldateconnidcarrid
NEW YORK0598QFFRANKFURT0440LHNEW YORK0402LHFRANKFURT0017AAcityfromconnidcarrid
fldateconnidcarridcityfromconnidcarrid
Inner Join
SPFLI SFLIGHT
![Page 28: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/28.jpg)
© 2004 SAP AG, ABAP210 28
Inner Join
2002/11/090402LHNEW YORK0402LH2002/11/080402LHNEW YORK0402LH2002/11/120017AAFRANKFURT0017AA2002/11/070017AAFRANKFURT0017AA
2002/11/090402LH2002/11/080402LH2002/11/120017AA2002/11/070017AAfldateconnidcarrid
NEW YORK0598QFFRANKFURT0440LHNEW YORK0402LHFRANKFURT0017AAcityfromconnidcarrid
fldateconnidcarridcityfromconnidcarrid
Inner Join
SPFLI SFLIGHT
![Page 29: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/29.jpg)
© 2004 SAP AG, ABAP210 29
Left Outer Join
NULLNULLNULLNEW YORK0598QFNULLNULLNULLFRANKFURT0440LH
2002/11/090402LH2002/11/080402LH2002/11/120017AA2002/11/070017AAfldateconnidcarrid
NEW YORK0598QFFRANKFURT0440LHNEW YORK0402LHFRANKFURT0017AAcityfromconnidcarrid
2002/11/090402LHNEW YORK0402LH2002/11/080402LHNEW YORK0402LH2002/11/120017AAFRANKFURT0017AA2002/11/070017AAFRANKFURT0017AAfldateconnidcarridcityfromconnidcarrid
Left Outer Join
SPFLI SFLIGHT
![Page 30: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/30.jpg)
© 2004 SAP AG, ABAP210 30
Left Outer Join
NULLNULLNULLNEW YORK0598QFNULLNULLNULLFRANKFURT0440LH2002/11/090402LHNEW YORK0402LH2002/11/080402LHNEW YORK0402LH2002/11/120017AAFRANKFURT0017AA2002/11/070017AAFRANKFURT0017AA
2002/11/090402LH2002/11/080402LH2002/11/120017AA2002/11/070017AAfldateconnidcarrid
NEW YORK0598QFFRANKFURT0440LHNEW YORK0402LHFRANKFURT0017AAcityfromconnidcarrid
fldateconnidcarridcityfromconnidcarrid
Left Outer Join
SPFLI SFLIGHT
![Page 31: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/31.jpg)
© 2004 SAP AG, ABAP210 31
Subqueries
Sometimes you just want to know whether some records in a secondary 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 correspondingentry in table SPFLI?
SELECT carrid connidINTO (xcarrid, xconnid) FROM sflight AS fWHERE NOT EXISTS ( SELECT * FROM spfli
WHERE carrid = f~carridAND connid = f~connid ).
![Page 32: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/32.jpg)
© 2004 SAP AG, ABAP210 32
Agenda
WAS and Database Architecture
ABAP Open SQL Overview
How to Identify Expensive SQL
Rules for Better SQL ProgrammingTheory andHands-On
Summary
![Page 33: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/33.jpg)
© 2004 SAP AG, ABAP210 33
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 statementsrecorded: "List trace"
2. Switch on theSQL trace: "Trace on"
![Page 34: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/34.jpg)
© 2004 SAP AG, ABAP210 34
SQL Trace
![Page 35: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/35.jpg)
© 2004 SAP AG, ABAP210 35
Execution Plan
![Page 36: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/36.jpg)
© 2004 SAP AG, ABAP210 36
Other Tools
There are other tools you should also regard when performanceproblems show up:
Transaction SE30: The ABAP Profiler
Transaction DB01: Lockwait Situations
Transaction ST02: Buffer Statistics
Transaction ST04: Database Performance Analysis
![Page 37: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/37.jpg)
© 2004 SAP AG, ABAP210 37
Agenda
WAS and Database Architecture
ABAP Open SQL Overview
How to Identify Expensive SQL
Rules for Better SQL ProgrammingTheory andHands-On
Summary
![Page 38: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/38.jpg)
© 2004 SAP AG, ABAP210 38
databaseFROM clause
one or more tables
Index accesssearch area
WHERE clause
hit list
solutionset
SELECT clause
SELECT p~carrid p~connid cityfrom fldate INTO (crid, cnid, from, date)FROM spfli AS p JOIN sflight AS f
ON p~carrid = f~carrid AND p~connid = f~connid)WHERE cityfrom <> 'ROME' AND fldate LIKE '200211%'.
SELECT Statement Overview
![Page 39: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/39.jpg)
© 2004 SAP AG, ABAP210 39
Rule 1: Keep the Hit List Small
Keep the hit list small!
![Page 40: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/40.jpg)
© 2004 SAP AG, ABAP210 40
Keep the Hit List Small
Use a WHERE clause whenever possible
SELECT * FROM sflightINTO xflight.CHECK xflight-carrid = 'LH '.CHECK xflight-connid = '0300'.CHECK xflight-fldate(4) = '2002'.WRITE: / xflight-fldate.
ENDSELECT.
SELECT * FROM sflightINTO xflightWHERE carrid = 'LH ' AND
connid = '0300' AND fldate LIKE '2002%'.
WRITE: / xflight-fldate.ENDSELECT.
![Page 41: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/41.jpg)
© 2004 SAP AG, ABAP210 41
Keep the Hit List Small
Try to describe the full search condition
SELECT * FROM sflightINTO xflightWHERE carrid = 'LH ' AND
connid = '0300' AND fldate LIKE '2002%'.
WRITE: / xflight-fldate.ENDSELECT.
SELECT * FROM sflightINTO xflightWHERE carrid = 'LH ' AND connid = '0300'.CHECK xflight-fldate(4) = '2002'.WRITE: / xflight-fldate.
ENDSELECT.
![Page 42: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/42.jpg)
© 2004 SAP AG, ABAP210 42
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 43: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/43.jpg)
© 2004 SAP AG, ABAP210 43
Keep the Hit List Small: Exercise
Exercise 1: Keep the Hit List Small
1. Open program zwr3d2w3_1_xx for editing.(xx = the number of your group)
2. Optimize the SELECT-statements in form "version2":Substitute the CHECK-conditions by specifying the desiredrows 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 zwr3d2w3_1_xx for editing.(xx = the number of your group)
2. Optimize the SELECT-statements in form "version2":Substitute the CHECK-conditions by specifying the desiredrows in a WHERE-clause.
3. Run the program to see the effect of your optimization.
![Page 44: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/44.jpg)
© 2004 SAP AG, ABAP210 44
Rule 2: Minimize the Amount of Transferred Data
Minimize the amount of data
transferred between the database and the application
server!
![Page 45: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/45.jpg)
© 2004 SAP AG, ABAP210 45
Minimize the Amount of Transferred Data
Use a field list instead of SELECT *
SELECT fldate FROM sflightINTO (xflight-fldate)WHERE carrid = 'LH ' AND
connid = '0300' AND fldate LIKE '2002%'.
WRITE: / xflight-fldate.ENDSELECT.
SELECT * FROM sflightINTO xflightWHERE carrid = 'LH ' AND
connid = '0300' AND fldate LIKE '2002%'.
WRITE: / xflight-fldate.ENDSELECT.
![Page 46: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/46.jpg)
© 2004 SAP AG, ABAP210 46
Minimize the Amount of Transferred Data: Exercises
Exercise 2: Specify a SELECT-List1. Open program zwr3d2w3 _2_xx for editing.
(xx = the number of your group)
2. Optimize the SELECT-statements in form "version3":Select only the columns needed in the subroutine.
3. Run the program to see the effect of your optimization.
Exercise 2: Specify a SELECT-List1. Open program zwr3d2w3 _2_xx for editing.
(xx = the number of your group)
2. Optimize the SELECT-statements in form "version3":Select only the columns needed in the subroutine.
3. Run the program to see the effect of your optimization.
![Page 47: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/47.jpg)
© 2004 SAP AG, ABAP210 47
Minimize the Amount of Transferred Data
Apply UP TO n ROWS for a top-n solution set
SELECT id name discount FROM scustom UP TO 10 ROWSINTO (xid, xname, xdiscount) WHERE custtype = 'B'ORDER BY discount.WRITE: / xid, xname, xdiscount.
ENDSELECT.
SELECT id name discount FROM scustomINTO (xid, xname, xdiscount) WHERE custtype = 'B'ORDER BY discount.IF sy-dbcnt > 10. EXIT. ENDIF.WRITE: / xid, xname, xdiscount.
ENDSELECT.
![Page 48: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/48.jpg)
© 2004 SAP AG, ABAP210 48
Minimize the Amount of Transferred Data
Use the UPDATE ... SET Statement
UPDATE sflightSET seatsocc = seatsocc + 1 WHERE carrid = 'LH '.
SELECT * FROM sflightINTO xflightWHERE carrid ='LH '.
xflight-seatsocc = xflight-seatsocc + 1. UPDATE sflight FROM xflight.
ENDSELECT.
![Page 49: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/49.jpg)
© 2004 SAP AG, ABAP210 49
Minimize the Amount of Transferred Data
Use aggregate functions
SELECT SINGLE SUM( seatsocc )FROM sflight INTO sumWHERE fldate LIKE '2002%'.
WRITE: / sum.
sum = 0.SELECT seatsocc
FROM sflight INTO xseatsoccWHERE fldate LIKE '2002%'.sum = sum + xseatsocc.
ENDSELECT. WRITE: / sum.
![Page 50: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/50.jpg)
© 2004 SAP AG, ABAP210 50
Minimize the Amount of Transferred Data
Apply the HAVING clause
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 51: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/51.jpg)
© 2004 SAP AG, ABAP210 51
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
![Page 52: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/52.jpg)
© 2004 SAP AG, ABAP210 52
Minimize the Amount of Transferred Data: Exercises
Exercise 3: Use Aggregate Functions
1. Open program zwr3d2w3 _3_xx for editing.(xx = the number of your group)
2. Optimize the SELECT-statements in form "version4":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 zwr3d2w3 _3_xx for editing.(xx = the number of your group)
2. Optimize the SELECT-statements in form "version4":Have the database calculate the sum in the inner loop.
3. Run the program to see the effect of your optimization.
![Page 53: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/53.jpg)
© 2004 SAP AG, ABAP210 53
Rule 3: Keep the Number of Round Trips Small
Keep the number of round trips
between the database and the application server
small!
![Page 54: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/54.jpg)
© 2004 SAP AG, ABAP210 54
Keep the Number of Round Trips Small
Use high-speed array operations with UPDATE, INSERT, DELETE, MODIFY
INSERT sbook FROM TABLE itab.
LOOP AT itab INTO wa. INSERT INTO sbook VALUES wa.
ENDLOOP.
![Page 55: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/55.jpg)
© 2004 SAP AG, ABAP210 55
Keep the Number of Round Trips Small
Apply the INNER JOINAvoid nested SELECT-ENDSELECT loops
SELECT f~carrid f~connid b~bookidINTO (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
WHERE planetype = '727-200'. WRITE: / xcarrid, xconnid, xbookid.
ENDSELECT.
SELECT * FROM sflight INTO xflight WHERE planetype = '727-200'.SELECT * FROM sbook INTO xbook
WHERE carrid = xflight-carrid ANDconnid = xflight-connid ANDfldate = xsflight-fldate.
WRITE: / xflight-carrid, xflight-connid, xbook-bookid.ENDSELECT.
ENDSELECT.
![Page 56: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/56.jpg)
© 2004 SAP AG, ABAP210 56
Keep the Number of Round Trips Small
Apply the OUTER JOIN
SELECT f~carrid f~connid f~fldate b~bookidINTO (xcarrid, xconnid, xfldate, xbookid) 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, xbookid.
ENDSELECT.
SELECT * FROM sflight INTO xflight WHERE planetype = '727-200'.SELECT * FROM sbook INTO xbook
WHERE carrid = xflight-carridAND connid = xflight-connidAND fldate = xflight-fldate.
WRITE: / xflight-carrid, xflight-connid, xflight-fldate, xbook-bookid.
ENDSELECT.IF sy-dbcnt = 0. CLEAR xbook-bookid. WRITE: / xflight-carrid, xflight-connid, xflight-fldate,
xbook-bookid. ENDIF.
ENDSELECT.
![Page 57: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/57.jpg)
© 2004 SAP AG, ABAP210 57
Keep the Number of Round Trips Small
Use subqueries
SELECT carrid connid fldateFROM sflight AS fINTO (xcarrid, xconnid, xfldate) WHERE seatsocc IN ( SELECT MAX( seatsocc ) FROM sflightWHERE carrid = f~carrid AND connid = f~connid )
ORDER BY carrid connid fldate. WRITE: xcarrid, xconnid, xfldate.
ENDSELECT.
SELECT carrid connid MAX( seatsocc )FROM sflightINTO (xcarrid, xconnid, max)GROUP BY carrid connidORDER BY carrid connid.SELECT fldate FROM sflight
INTO yfldateWHERE carrid = xcarrid AND
connid = xconnid ANDseatsocc = max
ORDER BY fldate.WRITE: / xcarrid, xconnid, yfldate.
ENDSELECT. ENDSELECT.
![Page 58: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/58.jpg)
© 2004 SAP AG, ABAP210 58
Keep the Number of Round Trips Small
For frequently used INNER JOINs, you can create a database view in the ABAP Dictionary
SELECT carrid connid bookidINTO (xcarrid, xconnid, xbookid) FROM sflightbook.
WRITE: / xcarrid, xconnid, xbookid.ENDSELECT.
SELECT f~carrid f~connid b~bookidINTO (xcarrid, xconnid, xbookid) FROM sflight AS f INNER JOIN sbook AS b
ON f~carrid = b~carrid AND f~connid = b~connidAND f~fldate = b~fldate.
WRITE: / xcarrid, xconnid, xbookid.ENDSELECT.
![Page 59: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/59.jpg)
© 2004 SAP AG, ABAP210 59
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
![Page 60: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/60.jpg)
© 2004 SAP AG, ABAP210 60
Keep the Number of Round Trips Small: Exercise
Exercise 4: Use an Inner Join1. Open program zwr3d2w3 _4_xx for editing.
(xx = the number of your group)
2. Optimize the select-statements in form "version5":Replace the nested SELECT-ENDSELECT-loops by an inner join.
3. Run the program to see the effect of your optimization.
Exercise 4: Use an Inner Join1. Open program zwr3d2w3 _4_xx for editing.
(xx = the number of your group)
2. Optimize the select-statements in form "version5":Replace the nested SELECT-ENDSELECT-loops by an inner join.
3. Run the program to see the effect of your optimization.
![Page 61: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/61.jpg)
© 2004 SAP AG, ABAP210 61
Rule 4: Keep the Cost of the Search Down
Keep the cost of the search
down!
![Page 62: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/62.jpg)
© 2004 SAP AG, ABAP210 62
Keep the Cost of the Search Down
Specify the WHERE clause to keep the number of searches down and create suitable indices if necessary
SELECT bookidFROM sbook INTO xbookidWHERE carrid = 'LH ' AND
connid = '0300' AND fldate = '20020304'.
WRITE: / xbookid.ENDSELECT.
SELECT bookidFROM sbook INTO xflightWHERE orderdate = '20020304'.
WRITE: / xbookid. ENDSELECT.
![Page 63: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/63.jpg)
© 2004 SAP AG, ABAP210 63
Reasonable Index Design
• Keep in mind, which indices are defined
• Place fields that are effective in the selection process at the beginning
• The following fields are not effective in the selection process: MANDT, BUKRS, GJAHR.
• Create small indices
• Avoid overlaps (create disjunctive indices)
• Up to 4 indices in each table generally are not critical
• Keep in mind, which indices are defined
• Place fields that are effective in the selection process at the beginning
• The following fields are not effective in the selection process: MANDT, BUKRS, GJAHR.
• Create small indices
• Avoid overlaps (create disjunctive indices)
• Up to 4 indices in each table generally are not critical
![Page 64: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/64.jpg)
© 2004 SAP AG, ABAP210 64
Keep the Cost of the Search Down
Make sure that the first n fields of the designated index are stated with EQ within the WHERE clause
SELECT * FROM sflightINTO xflightWHERE carrid = 'LH ' AND
connid = '0300' AND fldate LIKE '2002%'.
WRITE: / xflight-fldate.ENDSELECT.
SELECT * FROM sflightINTO xflightWHERE carrid = 'LH ' AND
fldate LIKE '2002%'.WRITE: / xflight-fldate.
ENDSELECT.
![Page 65: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/65.jpg)
© 2004 SAP AG, ABAP210 65
Keep the Cost of the Search Down
Replace the inner OR with an IN operator
SELECT * FROM sflightINTO xflightWHERE carrid = 'LH ' AND
connid IN ('0300', '0302') ANDfldate LIKE '2002%'.
WRITE: / xflight-fldate.ENDSELECT.
SELECT * FROM sflightINTO xflightWHERE carrid = 'LH ' AND
(connid = '0300' OR connid = '0302') ANDfldate LIKE '2002%'.
WRITE: / xflight-fldate.ENDSELECT.
![Page 66: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/66.jpg)
© 2004 SAP AG, ABAP210 66
Keep the Cost of the Search Down
You cannot process NOT operators in SELECT using an index
SELECT * FROM sflightINTO xflightWHERE carrid IN ('AA ', 'QM ') AND
connid = '0300'.WRITE: / xflight-fldate.
ENDSELECT.
SELECT * FROM sflightINTO xflightWHERE carrid <> 'LH ' AND
connid = '0300'.WRITE: / xflight-fldate.
ENDSELECT.
![Page 67: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/67.jpg)
© 2004 SAP AG, ABAP210 67
Keep the Cost of the Search Down
Think about optimizer hints if the optimizer fails to find a sound execution plan
SELECT carrid connid cityfromFROM spfli INTO (xcarrid, xconnid, xcityfrom) WHERE carrid = 'LH ' AND cityfrom = 'FRANKFURT'%_HINTS ORACLE 'INDEX("SPFLI" "SPFLI~001")'.
WRITE: / xcarrid, xconnid, xcityfrom.ENDSELECT.
SELECT carrid connid cityfromFROM spfli INTO (xcarrid, xconnid, xcityfrom) WHERE carrid = 'LH ' AND cityfrom = 'FRANKFURT'.
WRITE: / xcarrid, xconnid, xcityfrom.ENDSELECT.
![Page 68: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/68.jpg)
© 2004 SAP AG, ABAP210 68
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 69: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/69.jpg)
© 2004 SAP AG, ABAP210 69
Rule 5: Remove the Load From the Database
Remove the load
from the database!
![Page 70: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/70.jpg)
© 2004 SAP AG, ABAP210 70
Remove the Load From the Database
Check whether a table meets the criteria for Table Buffering
SELECT SINGLE * FROM scarrINTO xcarrWHERE carrid = 'LH '.
![Page 71: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/71.jpg)
© 2004 SAP AG, ABAP210 71
Criteria for Table Buffering
When to apply table buffering• Frequently read • Relatively small• Deferred visibility of changes is acceptable
When to apply table buffering• Frequently read • Relatively small• Deferred visibility of changes is acceptable
When to avoid table buffering• Heavily changed• Contents must always be up-to-date
When to avoid table buffering• Heavily changed• Contents must always be up-to-date
![Page 72: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/72.jpg)
© 2004 SAP AG, ABAP210 72
• 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
• 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
![Page 73: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/73.jpg)
© 2004 SAP AG, ABAP210 73
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 scarr
INTO zcarrWHERE carrid = 'LH '.
...
![Page 74: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/74.jpg)
© 2004 SAP AG, ABAP210 74
Remove the Load From the Database
Check whether a SELECT is really needed before an UPDATE is made
UPDATE sflightSET seatsocc = 1 WHERE carrid = 'LH ' AND
connid = '0300' AND fldate = '20021204'.
SELECT SINGLE * FROM sflightINTO xflightWHERE carrid = 'LH ' AND
connid = '0300' AND fldate = '20021204'.
xflight-seatsocc = 1. UPDATE sflight FROM xflight.
![Page 75: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/75.jpg)
© 2004 SAP AG, ABAP210 75
Remove the Load From the Database
Avoid the ORDER BY clause if the desired sorting doesn’t correspond to the index used
SELECT p~airpfrom p~airpto f~fldate p~deptimeINTO TABLE flightsFROM spfli AS p INNER JOIN sflight AS f
ON p~carrid = f~carridAND p~connid = f~connid
WHERE p~carrid = 'LH '.SORT flights BY airpfrom airpto fldate deptime.
LOOP AT flights INTO xflight.WRITE: / xflight-airpfrom, xflight-airpto,
xflight-fldate, xflight-deptime.ENDLOOP.
SELECT p~airpfrom p~airpto f~fldate p~deptimeINTO xflightFROM spfli AS p INNER JOIN sflight AS fON p~carrid = f~carridAND p~connid = f~connid
WHERE p~carrid = 'LH 'ORDER BY p~airpfrom p~airpto f~fldate p~deptime.
WRITE: / xflight-airpfrom, xflight-airpto, xflight-fldate, xflight-deptime.
ENDSELECT.
![Page 76: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/76.jpg)
© 2004 SAP AG, ABAP210 76
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
![Page 77: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/77.jpg)
© 2004 SAP AG, ABAP210 77
Golden Rule: Think and Experiment
Think and experiment!
![Page 78: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/78.jpg)
© 2004 SAP AG, ABAP210 78
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
![Page 79: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/79.jpg)
© 2004 SAP AG, ABAP210 79
Agenda
WAS and Database Architecture
ABAP Open SQL Overview
How to Identify Expensive SQL
Rules for Better SQL ProgrammingTheory andHands-On
Summary
![Page 80: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/80.jpg)
© 2004 SAP AG, ABAP210 80
Summary
There is just one database server
Buffers and indices
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
![Page 81: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/81.jpg)
© 2004 SAP AG, ABAP210 81
Further Information
Related Workshops at TechEd 2002Analyzing Performance with the Code InspectorNov. 12, 16:15-18:15Performance Analysis in a NutshellNov. 13, 8:15-12:15Traps and Pitfalls in ABAPNov. 12, 13:45-15:45Nov. 15, 10:30-12:30ABAP for Power UsersNov. 14, 14:00-18:00Nov. 15, 8:15-12:15
![Page 82: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/82.jpg)
© 2004 SAP AG, ABAP210 82
Q&A
Questions?
![Page 83: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/83.jpg)
© 2004 SAP AG, ABAP210 83
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 ’02 New Orleans Team
![Page 84: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/84.jpg)
© 2004 SAP AG, ABAP210 84
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, SAP Logo, R/2, R/3, mySAP, mySAP.com 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 trademarks of their respective companies.
Copyright 2002 SAP AG. All Rights Reserved
![Page 85: Efficient Database Programming with ABAP - Community … · ©2004 SAP AG, ABAP210 3 Agenda WAS and Database Architecture ABAP Open SQL Overview How to Identify Expensive SQL Rules](https://reader031.vdocument.in/reader031/viewer/2022022509/5ad68b147f8b9a075a8e5c02/html5/thumbnails/85.jpg)
© 2004 SAP AG, ABAP210 85
Weitergabe und Vervielfältigung dieser Publikation oder von Teilen daraus sind, zu welchem Zweck und in welcher Form auch immer, ohne die ausdrückliche schriftliche Genehmigung durch SAP AG nicht gestattet. In dieser Publikation enthaltene Informationen können ohne vorherige Ankündigung geändert werden.
Die von SAP AG oder deren Vertriebsfirmen angebotenen Softwareprodukte können Softwarekomponenten auch anderer Softwarehersteller enthalten.
Microsoft®, WINDOWS®, NT®, EXCEL®, Word®, PowerPoint® und SQL Server® sind eingetragene Marken der 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®, Informixund Informix® Dynamic ServerTM sind Marken der IBM Corporation in den USA und/oder anderen Ländern.
ORACLE® ist eine eingetragene Marke der ORACLE Corporation.
UNIX®, X/Open®, OSF/1® und Motif® sind eingetragene Marken der Open Group.
Citrix®, das Citrix-Logo, ICA®, Program Neighborhood®, MetaFrame®, WinFrame®, VideoFrame®, MultiWin® und andere hier erwähnte Namen von Citrix-Produkten sind Marken von Citrix Systems, Inc.
HTML, DHTML, XML, XHTML sind Marken oder eingetragene Marken des W3C®, World Wide Web Consortium, Massachusetts Institute of Technology.
JAVA® ist eine eingetragene Marke der Sun Microsystems, Inc.
JAVASCRIPT® ist eine eingetragene Marke der Sun Microsystems, Inc., verwendet unter der Lizenz der von Netscape entwickelten und implementierten Technologie.
MarketSet und Enterprise Buyer sind gemeinsame Marken von SAP AG und Commerce One.
SAP, SAP Logo, R/2, R/3, mySAP, mySAP.com und weitere im Text erwähnte SAP-Produkte und -Dienst-leistungensowie die entsprechenden Logos sind Marken oder eingetragene Marken der SAP AG in Deutschland und anderen Ländern weltweit. Alle anderen Namen von Produkten und Dienstleistungen sind Marken der jeweiligen Firmen.
Copyright 2002 SAP AG. Alle Rechte vorbehalten