Beruflich Dokumente
Kultur Dokumente
Optimizer Operators
Objectives
After completing this lesson, you should be able to:
Describe most of the SQL operators
List the possible access paths
Explain how join operations are performed
4-2
Binary operations
Joins
N-ary operations
4-3
Access Paths
1. Full Table Scan
Tables
2. Rowid Scan
3. Sample Table Scan
4. Index Scan (Unique)
5. Index Scan (Range)
6. Index Scan (Full)
Indexes
4-4
4-5
4-6
No suitable index
Low selectivity filters (or no filters)
Small table
High degree of parallelism
Full table scan hint: FULL (<table name>)
ROWID Scan
select * from scott.emp where rowid='AAAQ+LAAEAAAAAfAAJ';
Block 6959Row 2
4-7
2145,MH,V,20,
1034,JF,V,10,
-----------------------------------------------------------------| Id | Operation
| Name | Rows | Bytes | Cost |
-----------------------------------------------------------------|
0 | SELECT STATEMENT
|
|
1 |
37 |
1|
|
1 | TABLE ACCESS BY USER ROWID| EMP |
1 |
37 |
1|
------------------------------------------------------------------
Row migration
4-8
Indexes: Overview
Index storage techniques:
B*-tree indexes: The default and the most common
Normal
Function based: Precomputed value of a function or
expression
Index-organized table (IOT)
Bitmap indexes
Cluster indexes: Defined specifically for cluster
Index attributes:
Key compression
Reverse key
Ascending, descending
Branch
Index entry header
Key column length
Leaf
Index Scans
Types of index scans:
Unique
Min/Max
Range (Descending)
Skip
Full and fast full
Index join
B : block
Table EMP
B
4 - 13
4 - 14
4 - 15
4 - 16
4 - 17
4 - 18
--------------------------------------------------------------------| Id | Operation
| Name
| Rows | Bytes |Cost|
--------------------------------------------------------------------|
0 | SELECT STATEMENT
|
|
12 |
444 |
2|
|
1 | TABLE ACCESS BY INDEX ROWID| EMP
|
12 |
444 |
2|
|
2 |
INDEX FULL SCAN
| I_DEPTNO |
14 |
|
1|
--------------------------------------------------------------------Predicate Information (identified by operation id):
--------------------------------------------------1 - filter("SAL">1000)
2 - filter("DEPTNO" IS NOT NULL)
4 - 19
db_file_multiblock_read_count = 4
multiblock read
SH
R
discard
SH=segment header
R=root block
B=branch block
L=leaf block
multiblock read
B
discard
...
discard
4 - 20
B1
Min F16 F20 F26 F30
4 - 21
B2
F10
F11
F12
F13
F14
F15
F16
F17
F18
F19
F20
F21
F22
F23
F24
F25
F26
F27
F28
F29
F30
F31
F32
F33
F34
F35
M10
M11
M12
M13
M14
M15
M16
M17
M18
M19
M20
M21
M22
M23
M24
M25
M26
M27
M28
M29
M30
M31
M32
M33
M34
M35
L1
L2
L3
L4
L5
L6
L7
L8
L9
L10
4 - 23
--------------------------------------------------------------------| Id | Operation
| Name
| Rows | Bytes |
--------------------------------------------------------------------|
0 | SELECT STATEMENT
|
|
14 |
140 |
|
1 | VIEW
| index$_join$_001 |
14 |
140 |
|
2 |
HASH JOIN
|
|
|
|
|
3 |
INDEX FAST FULL SCAN| IX_SS
|
14 |
140 |
|
4 |
14 |
140 |
--------------------------------------------------------------------| Id | Operation
| Name | Rows | Bytes | Cost
|
--------------------------------------------------------------------|
0 | SELECT STATEMENT
|
|
1 |
87 |
2 |
|
1 | TABLE ACCESS BY INDEX ROWID| EMP |
1 |
87 |
2 |
|
2 |
AND-EQUAL
|
|
|
|
|
|
3 |
INDEX RANGE SCAN
| ISAL |
1 |
|
1 |
|
4 |
INDEX RANGE SCAN
| IJOB |
4 |
|
1 |
--------------------------------------------------------------------Predicate Information (identified by operation id):
--------------------------------------------------1 - filter("SAL"=1000 AND "JOB"='CLERK')
3 - access("SAL"=1000)
4 - access("JOB"='CLERK')
4 - 25
4 - 26
Null?
SSN
FNAME
LNAME
Y
Y
N
PERSON
Column
Null?
SSN
FNAME
LNAME
N
Y
N
PERSON
4 - 27
Index-Organized Tables
Indexed
access on table
Accessing
index-organized table
ROWID
Nonkey columns
Key column
Row header
4 - 28
4 - 29
Bitmap Indexes
Table
File 3
Block 10
Index
Block 11
Block 12
<Blue,
<Green,
<Red,
<Yellow,
Key
4 - 30
10.0.3,
10.0.3,
10.0.3,
10.0.3,
Start
ROWID
12.8.3,
12.8.3,
12.8.3,
12.8.3,
100010000
000101000
010000000
001000000
010000000
000000000
001100000
100000000
End
ROWID
Bitmap
010010100>
100100000>
000001001>
001000010>
1 |
2 |
1 |
45 |
|
3 |
BITMAP INDEX SINGLE VALUE | IX_B2
|
|
|
SELECT * FROM PERF_TEAM WHERE country>'FR';
--------------------------------------------------------------------Predicate:
3 - access("COUNTRY"='FR')
| Id | Operation
| Name
| Rows | Bytes |
--------------------------------------------------------------------|
0 | SELECT STATEMENT
|
|
1 |
45 |
|
1 | TABLE ACCESS BY INDEX ROWID | PERF_TEAM |
1 |
45 |
|
2 |
3 |
| IX_B2
FR 0 0 1 1 1 1 0 0 0 0 0 0
OR
0 1 1 1 1 1 0 0 0 0 0
DE 0 1 0 0 0 0 0 0 0 0 0 0
F 0 0 1 1 1 1 0 0 0 0 0 0
AND
0 0 1 0 1 1 0 0 0 0 0
M 1 1 1 0 1 1 0 1 0 1 1 1
SELECT * FROM EMEA_PERF_TEAM T WHERE country='FR' and gender='M';
4 - 32
4 - 33
Bitmap Operations
BITMAP CONVERSION:
TO ROWIDS
FROM ROWIDS
COUNT
BITMAP INDEX:
SINGLE VALUE
RANGE SCAN
FULL SCAN
4 - 34
BITMAP
BITMAP
BITMAP
BITMAP
MERGE
AND/OR
MINUS
KEY ITERATION
Copyright 2008, Oracle. All rights reserved.
1.2.3
Sales
10.8000.3
<Rognes, 1.2.3, 10.8000.3, 100010010010100>
<Aix-en-Provence, 1.2.3, 10.8000.3, 000101000100000>
<Marseille, 1.2.3, 10.8000.3, 010000001000001>
4 - 35
Composite Indexes
MAKE
MODEL
CARS
Index columns
create index cars_make_model_idx on cars(make, model);
select *
from cars
where make = 'CITRON' and model = '2CV';
----------------------------------------------------------------| Id | Operation
| Name
|
----------------------------------------------------------------|
0 | SELECT STATEMENT
|
|
|
1 | TABLE ACCESS BY INDEX ROWID| CUSTOMERS
|
|* 2 |
INDEX RANGE SCAN
| CARS_MAKE_MODEL_IDX
|
-----------------------------------------------------------------
4 - 36
VISIBLE
Index
Index
OPTIMIZER_USE_INVISIBLE_INDEXES=FALSE
Update index.
Update index.
Update table.
Update table.
4 - 37
4 - 38
4 - 39
4 - 41
Practice 4: Overview
This practice covers using different access paths for better
optimization.
Case 1 through case 13
4 - 43
Clusters
ORD_NO
-----101
102
102
102
101
101
PROD
-----A4102
A2091
G7830
N9587
A5675
W0824
QTY
-----20
11
20
26
19
10
...
ORD_NO
-----101
102
ORD_DT
CUST_CD
----------05-JAN-97
R01
07-JAN-97
N45
4 - 44
Cluster Key
(ORD_NO)
101 ORD_DT
CUST_CD
05-JAN-97
R01
PROD
QTY
A4102
20
A5675
19
W0824
10
102
ORD_DT
CUST_CD
07-JAN-97
N45
PROD
QTY
A2091
11
G7830
20
N9587
26
Hash cluster:
Tables always joined on the same keys
Storage for all cluster keys allocated initially
In either equality (=) or nonequality (<>) searches
4 - 45
4 - 46
4 - 47
Sorting Operators
SORT operator:
HASH operator:
GROUP BY: For this operator
UNIQUE: Equivalent to SORT UNIQUE
4 - 48
4 - 49
Inlist Iterator
Every value executed separately
deptno=1
deptno=2
View Operator
create view V as select /*+ NO_MERGE */ DEPTNO, sal from emp ;
select * from V;
--------------------------------------------------------------------| Id | Operation
| Name | Rows | Bytes | Cost (%CPU)| Time
|
0 | SELECT STATEMENT |
|
14 |
364 |
1
(0)| 0:01
|
1 | VIEW
| V
|
14 |
364 |
1
(0)| 0:01
|
2 |
INDEX FULL SCAN| IX_SS |
14 |
98 |
1
(0)| 0:01
--------------------------------------------------------------------select v.*,d.dname from (select DEPTNO, sum(sal) SUM_SAL
from emp group by deptno) v, dept d where v.deptno=d.deptno;
--------------------------------------------------------------------| Id | Operation
| Name | Rows | Bytes | Cost (%CPU)|
|
0 | SELECT STATEMENT
|
|
3 |
144 |
5 (20)|
|
1 | HASH JOIN
|
|
3 |
144 |
5 (20)|
|
2 |
VIEW
|
|
3 |
78 |
1
(0)|
|
3 |
HASH GROUP BY
|
|
3 |
21 |
1
(0)|
|
4 |
INDEX FULL SCAN| IX_SS |
14 |
98 |
1
(0)|
|
5 |
TABLE ACCESS FULL| DEPT |
4 |
88 |
3
(0)|
--------------------------------------------------------------------Predicate: 1 - access("V"."DEPTNO"="D"."DEPTNO")
4 - 51
4 - 52
4 - 53
Join Methods
A join defines the relationship between two row sources.
A join is a method of combining data from two data sources.
It is controlled by join predicates, which define how the
objects are related.
Join methods:
Nested loops
Sort-merge join
Hash join
SELECT e.ename, d.dname
FROM dept d JOIN emp e USING (deptno)
WHERE e.job = 'ANALYST' OR e.empno = 9999;
Join predicate
Nonjoin predicate
SELECT e.ename,d.dname
FROM
emp e, dept d
WHERE e.deptno = d.deptno AND
(e.job = 'ANALYST' OR e.empno = 9999);
Join predicate
Nonjoin predicate
4 - 54
TAF
TAR
Driving
For
each
IS
Inner
4 - 55
TAR
Driving
IRS
Inner
4 - 56
NL
TAF
IRS
Driving
Inner
4 - 57
Merged
SJ
SJ
TAF
TAF
Independent
4 - 58
Sorted
Sorted
MJ
Hash Join
The smallest row source is used
to build a hash table.
The second row source is hashed
and checked against the hash table.
HJ
Driving TAF
Build hash
table in
memory
TAR
IS
Probe
4 - 59
Cartesian Join
4 - 60
Join Types
A join operation combines the output from two row sources
and returns one resulting row source.
Join operation types include the following :
4 - 61
Nonequijoin
4 - 62
Equijoin
Outer Joins
An outer join also returns a row
if no match is found.
DEPT
10
20
30
40
EMP
20
10
20
30
10
SELECT d.deptno,d.dname,e.empno,e.ename
FROM
emp e, dept d
WHERE e.deptno(+)=d.deptno;
---------------------------------------------| Id | Operation
| Name |
---------------------------------------------|
0 | SELECT STATEMENT
|
|
|
1 | NESTED LOOPS OUTER
|
|
|
2 |
TABLE ACCESS FULL
| DEPT |
|
3 |
TABLE ACCESS BY INDEX ROWID| EMP
|
|
4 |
INDEX RANGE SCAN
| EDEPT |
---------------------------------------------4 - access("E"."DEPTNO"(+)="D"."DEPTNO")
4 - 63
SELECT d.deptno,d.dname,e.empno,e.ename
FROM
emp e, dept d
WHERE e.deptno(+)=d.deptno;
--------------------------------------| Id | Operation
| Name |
--------------------------------------|
0 | SELECT STATEMENT
|
|
|
1 | HASH JOIN RIGHT OUTER |
|
|
2 |
TABLE ACCESS FULL
| EMP |
|
3 |
TABLE ACCESS FULL
| DEPT |
----------------------------------1 - access("E"."DEPTNO"(+)="D"."DEPTNO")
SELECT d.deptno,d.dname,e.empno,e.ename
FROM
emp e, dept d
WHERE e.deptno(+)=d.deptno;
----------------------------------| Id | Operation
| Name |
----------------------------------|
0 | SELECT STATEMENT
|
|
|
1 | HASH JOIN OUTER
|
|
|
2 |
TABLE ACCESS FULL| DEPT |
|
3 |
TABLE ACCESS FULL| EMP |
----------------------------------1 - access("E"."DEPTNO"(+)="D"."DEPTNO")
Semijoins
Semijoins only look for the first match.
DEPT
10
20
30
40
EMP
20
10
10
30
10
4 - 64
Antijoins
Reverse of what would have been returned by a join
SELECT deptno, dname
FROM
dept
WHERE deptno not in
(SELECT deptno FROM emp);
--------------------------------------| Id | Operation
| Name
|
--------------------------------------|
0 | SELECT STATEMENT
|
|
|
1 | NESTED LOOPS ANTI |
|
|
2 |
TABLE ACCESS FULL| DEPT
|
|
3 |
INDEX RANGE SCAN | I_DEPTNO |
--------------------------------------3 - access("DEPTNO"="DEPTNO")
EMP
4 - 65
DEPT
DEPT
10
20
30
40
EMP
20
10
20
30
10
4 - 66
FILTER
CONCATENATION
UNION ALL/UNION
INTERSECT
MINUS
Filter Operations
Accepts a set of rows
Eliminates some of them
Returns the rest
SELECT
deptno, sum(sal) SUM_SAL
FROM
emp
GROUP BY deptno
HAVING sum(sal) > 9000;
-----------------------------------| Id | Operation
| Name |
-----------------------------------|
0 | SELECT STATEMENT
|
|
|
1 | FILTER
|
|
|
2 |
HASH GROUP BY
|
|
|
3 |
TABLE ACCESS FULL| EMP |
-----------------------------------1 - filter(SUM("SAL")>9000)
4 - 67
Concatenation Operation
4 - 68
UNION
UNION ALL
SORT UNIQUE
UNION-ALL
INDEX FULL SCAN
INDEX FAST FULL SCAN
INTERSECT
INTERSECTION
SORT UNIQUE NOSORT
INDEX FULL SCAN
SORT UNIQUE
INDEX FAST FULL SCAN
MINUS
4 - 69
1
2
3
3
MINUS
SORT UNIQUE NOSORT
INDEX FULL SCAN
SORT UNIQUE
INDEX FAST FULL SCAN
1
2
1
2
1
2
3
3
3
3
3
3
4
5
| Operation
| Name
|Rows
-------------------------------------------------------------|
0 | SELECT STATEMENT
1 |
| 8fpza04gtwsfr6n595au15yj4y |
2 |
3 |
RESULT CACHE
HASH GROUP BY
11
11
| 107
--------------------------------------------------------------
4 - 70
Summary
In this lesson, you should have learned to:
Describe most of the SQL operators
List the possible access paths
Explain how join operations are performed
4 - 71
Practice 4: Overview
This practice covers the following topics:
Using different access paths for better optimization
Case 14 to case 16
4 - 72