Query Processing
Transparencies
Chapter 20 - Objectives
Chapter 20 - Objectives
Chapter 20 - Objectives
Introduction
Introduction
more
control
over
system
Query Processing
Activities involved in retrieving data from the
database.
Aims of QP:
transform query written in high-level language
(e.g. SQL), into correct and efficient execution
strategy expressed in low-level language
(implementing RA);
execute strategy to retrieve required data.
7
Query Optimization
Activity of choosing an efficient execution strategy
for processing query.
Staff.branchNo=Branch.branchNo
(3) ( position='Manager'(Staff))
Branch)
Staff.branchNo=Branch.branchNo
( city='London' (Branch))
10
Assume:
1000 tuples in Staff; 50 tuples in Branch;
50 Managers; 5 London branches;
no indexes or sort keys;
results of any intermediate operations stored
on disk;
cost of the final write is ignored;
tuples are accessed one at a time.
11
13
14
16
Query Decomposition
Analysis
18
Analysis - Example
SELECT staff_no
FROM Staff
WHERE position > 10;
Analysis
21
Normalization
22
Semantic Analysis
Semantic Analysis
Normalized attribute
connection graph
27
Simplification
30
For example:
branchNo='B003'( salary>15000(Staff)) =
salary>15000( branchNo='B003'(Staff))
31
For example:
lName branchNo, lName(Staff) = lName (Staff)
32
For example:
fName, lName( lName='Beech'(Staff)) =
lName='Beech'( fName,lName(Staff))
33
staff.branchNo=branch.branchNo
Staff
34
S) = ( p(R))
( q(S))
36
example:
position='Manager' city='London'(Staff
Staff.branchNo=Branch.branchNo Branch) =
( position='Manager'(Staff))
( city='London' (Branch))
Staff.branchNo=Branch.branchNo
37
S) = ( L1(R))
( L2(S))
38
39
For example:
position,city,branchNo(Staff
Staff.branchNo=Branch.branchNo
Branch) =
( position, branchNo(Staff))
Staff.branchNo=Branch.branchNo
Staff.branchNo=Branch.branchNo
40
41
42
For example:
(Staff
Staff.staffNo=PropertyForRent.staffNo
PropertyForRent)
ownerNo=Owner.ownerNo staff.lName=Owner.lName
Staff
Owner =
staff.staffNo=PropertyForRent.staffNo staff.lName=lName
(PropertyForRent
ownerNo
Owner)
45
46
47
48
49
51
Database Statistics
54
56
Selection Operation
Selection Operation
Inequality condition on primary key.
Equality condition on clustering (secondary)
index.
Equality condition on a non-clustering
(secondary) index.
Inequality condition on a secondary B+-tree
index.
58
nDistinctB(R)
[(nTuples(S) + nDistinctB(R))/3]
otherwise
59
60
61
62
63
65
66
67
69
Join Operation
70
71
or
nTuples(T) = SCB(S)*nTuples(R)
72
74
75
Sort-Merge Join
Sort-Merge Join
77
Hash Join
Projection Operation
80
nBlocks(R)*[log2(nBlocks(R))].
Combined cost is:
nBlocks(R) + nBlocks(R)*[log2(nBlocks(R))]
81
Set Operations
Aggregate Operations
SELECT AVG(salary)
FROM Staff;
85
Aggregate Operations
SELECT AVG(salary)
FROM Staff
GROUP BY branchNo;
86
Pipelining
Types of Trees
88
Pipelining
to
query
Rulebased
15 rules, ranked in order of efficiency. Particular
access path for a table only chosen if statement
contains a predicate or other construct that makes
thataccesspathavailable.
Score assigned to each execution strategy using these
rankingsandstrategywithbest(lowest)scoreselected.
90
QO in Oracle Rule-Based
91
FROMPropertyForRent
WHERErooms>7ANDcity=London
Singlecolumn access path using index on city from WHERE
condition(city=London).Rank9.
Unbounded range scan using index on rooms from WHERE
condition(rooms>7).Rank11.
Fulltablescanrank15.
AlthoughthereisindexonpropertyNo,columndoesnotappear
inWHEREclauseandsoisnotconsideredbyoptimizer.
Based on these paths, rulebased optimizer will choose to use
indexbasedoncitycolumn.
92
QO in Oracle Cost-Based
QO in Oracle Statistics
94
QO in Oracle Histograms
95
QO in Oracle Histograms
actualnonuniformdistribution.
Firstcanbestoredcompactlyaslowvalue(1)andahigh
value (10), and as total count of all frequencies (in this
case,100).
96
QO in Oracle Histograms
97
QO in Oracle Histograms
(a)widthbalancedforroomswith5buckets.Eachbucket
ofequalwidthwith2values(12,34,etc.)
(b)heightbalancedheightofeachcolumnis20(100/5).
98
99