Beruflich Dokumente
Kultur Dokumente
DB2
SELECTING
Table scan
Table scan or probe via bitmap or RRN list
Index scan or probe
JOINING
Index scan or probe
Hash table probe
Sorted list probe
GROUPING
Index scan or probe
Hash table scan
ORDERING
Index scan or probe
Sorted list scan
DB2
Selection Optimization
DB2
DB2
DB2
Meaning
Join Position
Join Dial
Join Order
Average Duplicates
DB2
Cascade
Table A
Table C
Table D
*
Dial 3
Dial 1
Dial 2
Dial 4
Table C
Table B
Star
or
1 to many
Table A
Dial 2
*
Table D
Dial 3
Dial 1
Dial 4
DB2
Join Order
Assume average duplicates of 1 for headers and 3 for details...
SELECT *
FROM ORDER_HEADERS A,
ORDER_DETAILS B
WHERE A.ORDER_KEY = B.ORDER_KEY
AND CUSTOMER_NO = 112358
What
What is
is the
the total
total cost
cost
of
of selecting
selecting from,
from, and
and
joining
joining to,
to, each
each dial?
dial?
Headers
Details
Join Order 1
(A, B)
Join fan out is 1 to 3
Dial 1
Join Order 2
(B, A)
Join fan out is 1 to 1
Dial 2
Details
Headers
Dial 1
Dial 2
DB2
For left outer and exception join, tables are joined from left to right
Right outer joins are implemented by conversion into left outer joins
Index only access not available for CQE
For inner join, optimizer not biased toward using specified join
order
Must use QAQQINI file settings to influence join order chosen by optimizer
FORCE_JOIN_ORDER set to *YES, *SQL, *PRIMARY can force join order
Not recommended
DB2
Step 2
Step 3
Select row
and build
key
Position into
index
Random read
row from
table
Table 2
Index 3
Table 3
What
Whattype
typeof
ofaccess
accessis
isused
used
for
forTable
Table33and
andTable
Table1...?
1...?
Step 4
Step 5
Random
read row
from table
Index 1
Table 1
Repeat
Steps 2 - 3
SELECT *
FROM TABLE_1,
TABLE_2,
TABLE_3
WHERE FKEY1 = PKEY3
AND FKEY2 = PKEY3
Repeat
Steps 4 - 5
until key not found
DB2
Disadvantages:
Creating a temporary index is very CPU and resource intensive
If index is built using host variable selection, then the query is not
reusable
DB2
Index with three keys: STORE, DATE, and EMPNUM (in any
order) can be used to satisfy both the join and non-join selection
predicates on the table SALES in one query step.
Copyright 2006 - IBM Corporation - Systems and Technology Group
DB2
IX 2
Keyed
selection
via index
Hash Table
Probe
hash
tables
Table 1
Hashing
Algorithm
Part 1
Hashing
Algorithm
Select rows
and build hash
tables
Hashing
Algorithm
Hash Table
Part 2
Select Table 1
rows and join
to hash tables
Result
Skip seq
selection
via EVI
and bitmap
EVI 3
Bitmap
Table 3
DB2
Join Optimization
Join Optimization
DB2
DB2
DB2
DB2
Multi-key join: probe index with both selection and join columns
Temporary index creation (CQE)
Typically small table to large table
Parallelism (SQE only)
DB2
Group-By Optimization
DB2
Hash Group by
All I/O
ALWCPYDTA(*OPTIMIZE)
10
DB2
Index
Index probe
probe on
on ARIZONA
ARIZONA
and
and the
the minimum
minimum or
or first
first value
value
for
for SALES
SALES by
by traversing
traversing the
the
radix
radix tree
tree in
in ascending
ascending order,
order,
then
then on
on to
to California
California
STATE
Alabama
Alabama
Alabama
Alaska
Alaska
Alaska
Alaska
Arizona
Arizona
Arizona
Arizona
Arizona
Arkansas
Arkansas
Arkansas
California
California
SALES
110.00
150.00
375.00
10.00
55.00
120.00
400.00
50.00
80.00
210.00
360.00
540.00
5.00
25.00
90.00
30.00
75.00
CUSTOMER
Jones
Smith
Doe
Johnson
Smith
Alexander
Lee
White
Doe
Brown
Jacobson
Milligan
Weatherby
Smith
Pippen
Lee
Wayne
DB2
Index
Index probe
probe on
on ARIZONA
ARIZONA
and
and the
the maximum
maximum or
or first
first value
value
for
for SALES
SALES by
by traversing
traversing the
the
radix
radix tree
tree in
in descending
descending order,
order,
then
then on
on to
to California
California
STATE
Alabama
Alabama
Alabama
Alaska
Alaska
Alaska
Alaska
Arizona
Arizona
Arizona
Arizona
Arizona
Arkansas
Arkansas
Arkansas
California
California
SALES
375.00
150.00
110.00
400.00
120.00
55.00
10.00
540.00
360.00
210.00
80.00
50.00
90.00
25.00
5.00
75.00
30.00
CUSTOMER
Jones
Smith
Doe
Johnson
Smith
Alexander
Lee
White
Doe
Brown
Jacobson
Milligan
Weatherby
Smith
Pippen
Lee
Wayne
11
Grouping Optimization
DB2
DB2
12
Ordering Optimization
DB2
Sort
All I/O
ALWCPYDTA(*OPTIMIZE)
Ordering Optimization
DB2
13
DB2
DB2
Query
Query plan
plan is
is executed
executed
from
from left
left to
to right,
right,
bottom
bottom to
to top
top
Start here
14
A Closer Look
DB2
DB2
15
DB2
DB2
16
Generated local
selection
provided by
optimizer
Generate list of
distinct key
values based
on the join
column
LPG
Big_Table
Small_Table
Storekeys
00001
00002
00003
00004
Storekey
Storekey
Join Condition
DB2
Store
Dial 2
Dial 1
Local selection
provided by query
No local selection
provided by query
Copyright 2006 - IBM Corporation - Systems and Technology Group
DB2
Query
Rewrite
with LPG
LPG
Select
from small_table s,
big_table b
where (s.store = 'Store 1'
or
s.store = 'Store 2'
or
s.store = 'Store 3'
or
s.store = 'Store 4')
and s.storekey = b.storekey
and b.storekey in (00001, 00002, 00003, 00004)
17
DB2
Results
Results
Join
Join
Big 1
Temp IX
Hash 2
Hash 1
Small 2
Small 2
Big 1
Local
select
Local
select
Local
select
DB2
Leverages...
Specific join order (fact table as dial 1, joined to dimension table(s) CQE)
Hash join
Skip sequential or clustered I/O access method
EVIs and dynamic bitmaps
Symmetric Multiprocessing and database parallelism
ibm.com/servers/enable/site/education/abstracts/16fa_abs.html
Copyright 2006 - IBM Corporation - Systems and Technology Group
18
DB2
The Process
DB2
Feedback
Determine what index you believe should be used for the query and then
create that index to see if the Query Optimizer will choose the new index
Exposes production data and workloads to changes
Reflects actual behavior based upon production data
19
DB2
Art
What can be done...?
Change the request or SQL coding
Change the design or influence the implementation
Database design
Tuning "knobs" and indexes
Upgrade OS to obtain new features
DB2
Art
Environments
A few long running or complex requests
Dedicate all resources
SMP database parallelism
Highly tuned
Mixture
Separate environments
Separate systems or logical partitions
20
DB2
SELECTING
Table scan
Table scan or probe via bitmap or RRN list
Index scan or probe
JOINING
Index scan or probe
Hash table probe
Sorted list probe
GROUPING
Index scan or probe
Hash table scan
ORDERING
Index scan or probe
Sorted list scan
21