Figure 18.1 Typical steps when processing a high

Figure 18.1 Typical steps when processing a high
Figure 18.1
Typical steps when processing a high-level query.
Query in a high-level language
SCANNING,
PARSING, AND
VALIDATING
Intermediate form of query
QUERY OPTIMIZER
Execution plan
QUERY CODE
GENERATOR
Code can be:
Code to execute the query
° Executed directly (interpreted mode)
° Stored and executed later whenever
needed (compiled mode)
RUNTIME DATABASE
PROCESSOR
Result of query
© Addison Wesley Longman, Inc. 2000, Elmasri/Navathe, Fundamentals of Database Systems, Third Edition
Figure 18.2
Outline of the sort-merge algorithm for external sorting.
i ➝ 1;
j ➝ b;
{size of the file in blocks}
k ➝ n B;
{size of buffer in blocks}
m ➝ (j/k) ;
{Sort Phase}
while (i <= m)
do {
read next k blocks of the file into the buffer or if there are less than k blocks remaining,
then read in the remaining blocks;
sort the blocks in the buffer and write as a temporary subfile;
i ➝ i + 1;
}
{Merge Phase: merge subfiles until only 1 remains}
set
i ➝1;
p ➝ logk–1m; {p is the number of passes for the merging phase}
j ➝ m;
while (i <= p)
do {
n ➝1;
q ➝ (j / (k–1)) ; {number of subfiles to write in this pass}
while (n <= q)
do {
read next k–1 subfiles or remaining subfiles (from previous pass) one block at a time;
merge and write as new subfile;
n ➝ n + 1;
}
j ➝q;
i ➝ i + 1;
}
set
© Addison Wesley Longman, Inc. 2000, Elmasri/Navathe, Fundamentals of Database Systems, Third Edition
Figure 18.3 Implementing JOIN, PROJECT, UNION, INTERSECTION,
and SET DIFFERENCE by using sort-merge, where R has n tuples and S
has m tuples. (a) Implementing the operation T ← R A=B S. (b)
Implementing the operation T ← π<attribute list>(R).
(a)
sort the tuples in R on attribute A; (*assume R has n tuples (records) *)
sort the tuples in S on attribute B; (*assume S has m tuples (records) *)
set i ➝ 1, j ➝ 1;
while (i ≤ n) and (j ≤ m)
do{
if R(i)[A] > S(j)[B]
then
set j ➝ j +1
elseif R(i)[A] < S(j)[B]
then
set i ➝ i +1
else {
(* R(i)[A] < S(j)[B], so we output a matched tuple*)
output the combined tuple <R(i ), S(j)> to T;
(*output other tuples that match R(i ), if any*)
set l ➝ j +1;
while (l ≤ m) and (R(i)[A] < S(l )[B])
do
{
output the combined tuple <R(i), S(l)> to T;
set l ➝ l+1
}
(*output other tuples that match S( j), if any*)
set k ➝ i +1;
while (k ≤ n) and (R(k)[A] < S(j)[B])
do
{
output the combined tuple <R(k), S(j )> to T;
set k ➝ k+1
}
set i ➝ k, j ➝ l
}
}
(b) create a tuple t [<attribute list>] in T ' for each tuple t in R;
(*T ' contains the projection result before duplicate elimination*)
if <attribute list> includes a key of R
the T ➝ T '
else {
sort the tuples in T ';
set i ➝1, j ➝ 2;
while i ≤ n
do
{
output the tuple T '[i ] to T;
while T '[i ] = T '[ j ] and j ≤ n do j➝ j+1; (*eliminate duplicates*)
i ➝ j; j ➝ i +1
}
}
(* T contains the projection result after duplicate elimination *)
© Addison Wesley Longman, Inc. 2000, Elmasri/Navathe, Fundamentals of Database Systems, Third Edition
Figure 18.3 Implementing JOIN, PROJECT, UNION, INTERSECTION, and SET
DIFFERENCE by using sort-merge, where R has n tuples and S has m tuples.
(c) Implementing the operation T ← R ∪ S. (d) Implementing the
operation T ← R ∩ S. (e) Implementing the operation T ← R S.
(c) sort the tuples in R and S using the same unique sort attributes;
set i ➝1, j ➝1;
while (i ≤ n) and (j ≤ m)
do {
if R(i ) > S(j )
then {
output S(j ) to T;
set j ➝ j +1
}
elseif R(i ) > S( j )
then {
output R(i ) to T ;
set i ➝ i +1
}
else set j ➝ j +1 (*R(i )=S( j ), so we skip one of the duplicate tuples*)
}
if (i ≤ n) then add tuples R(i ) to R(n) to T ;
if (j ≤ m) then add tuples S(j ) to S(m) to T ;
(d) sort the tuples in R and R using the same unique sort attributes;
set i ➝ 1, j ➝ 1;
while (i ≤ n) and (j ≤ m)
do {
if R(i ) > S(j )
then
set j ➝ j +1
elseif R(i ) < S(j )
then
set i ➝ i +1
else {
output R(i ) to T; (* R(i )=S(j ), so we output the tuple *)
set i ➝ i +1, j ➝ j +1
}
}
(e) sort the tuples in R and S using the same unique sort attributes;
set i ➝ 1, j ➝1;
while (i ≤ n) and (j ≤ m)
do {
if R(i ) > S(j )
then
set j ➝ j +1
elseif R(i ) < S(j )
then {
output R(i )to T; (* R(i ) has no matching S(j ), so output R(i )*)
set i ➝ i +1
}
else
set i ➝ i +1, j ➝ j +1
}
if (i ≤ n) then add tuples R(i ) to R(n) to T;
© Addison Wesley Longman, Inc. 2000, Elmasri/Navathe, Fundamentals of Database Systems, Third Edition
Figure 18.4 Two query trees for the query Q2. (a) Query tree
corresponding to the relational algebra expression for Q2.
(b) Initial (canonical) query tree for SQL query Q2.
π
(a)
P.PNUMBER,P.DNUM,E.LNAME,E.ADDRESS,E.BDATE
(3)
D.MGRSSN=E.SSN
(2)
E
P.DNUM=D.DNUMBER
(1)
σ
D
P.PLOCATION= 'Stafford'
P
(b)
σ
π
P.PNUMBER,P.DNUM,E.LNAME,E.ADDRESS,E.BDATE
P.DNUM=D.DNUMBER AND D.MGRSSN=E.SSN AND P.PLOCATION='Stafford'
X
X
P
E
D
© Addison Wesley Longman, Inc. 2000, Elmasri/Navathe, Fundamentals of Database Systems, Third Edition
Figure 18.4
(c) Query graph for Q2.
[P.PNUMBER,P.DNUM]
[E.LNAME,E.ADDRESS,E.BDATE]
P.DNUM=D.DNUMBER
P
D.MGRSSN=E.SSN
D
E
P.PLOCATION='Stafford'
'Stafford'
© Addison Wesley Longman, Inc. 2000, Elmasri/Navathe, Fundamentals of Database Systems, Third Edition
WORKS_ON
PROJECT
σ
© Addison Wesley Longman, Inc. 2000, Elmasri/Navathe, Fundamentals of Database Systems, Third Edition
EMPLOYEE
X
BDATE>‘DEC-31-1957’
PNAME=‘Aquarius’
σ
EMPLOYEE
σ
ESSN=SSN
X
PNUMBER=PNO
σ
π LNAME
WORKS_ON
PROJECT
X
σ PNAME=‘Aquarius’ AND PNUMBER=PNO AND ESSN=SSN AND BDATE>‘DEC-31-1957’
X
(b)
(a)
π LNAME
Figure 18.5 Steps in converting a query tree during heuristic
optimization. (a) Initial (canonical) query tree for SQL query Q.
(b) Moving SELECT operations down the query tree.
Figure 18.5 Steps in converting a query tree during heuristic
optimization. (c) Applying the more restrictive SELECT operation first.
(d) Replacing CARTESIAN PRODUCT and SELECT with JOIN operations.
π LNAME
(c)
π LNAME
(d)
σ ESSN=SSN
ESSN=SSN
x
PNUMBER=PNO
σ BDATE>'DEC-31-1957'
σ PNUMBER=PNO σ BDATE>'DEC-31-1957'
σ PNAME='Aquarius'
x
WORKS_ON
EMPLOYEE
PROJECT
σ PNAME='Aquarius'
WORKS_ON
PROJECT
© Addison Wesley Longman, Inc. 2000, Elmasri/Navathe, Fundamentals of Database Systems, Third Edition
EMPLOYEE
Figure 18.5 Steps in converting a query tree during heuristic
optimization. (e) Moving PROJECT operations down the query tree.
π LNAME
(e)
ESSN=SSN
π ESSN
πSSN,LNAME
PNUMBER=PNO
π PNUMBER
σ PNAME='Aquarius'
σ BDATE>'DEC-31-1957'
π ESSN,PNO
EMPLOYEE
WORKS_ON
PROJECT
© Addison Wesley Longman, Inc. 2000, Elmasri/Navathe, Fundamentals of Database Systems, Third Edition
Figure 18.6
A query tree for query Q1.
π FNAME,LNAME,ADDRESS
DNUMBER=DNO
σ DNAME='Research'
EMPLOYEE
DEPARTMENT
© Addison Wesley Longman, Inc. 2000, Elmasri/Navathe, Fundamentals of Database Systems, Third Edition
Figure 18.7
Two left-deep (join) query trees.
R1
R4
R2
R3
R1
R2
R4
R3
© Addison Wesley Longman, Inc. 2000, Elmasri/Navathe, Fundamentals of Database Systems, Third Edition
Figure 18.8 Sample statistical information for relations in Q2. (a)
Column information. (b) Table information. (c) Index information.
TABLE_NAME
COLUMN_NAME
PROJECT
PROJECT
PROJECT
DEPARTMENT
DEPARTMENT
EMPLOYEE
EMPLOYEE
EMPLOYEE
PLOCATION
PNUMBER
DNUM
DNUMBER
MGRSSN
SSN
DNO
SALARY
(b) TABLE_NAME
NUM_ROWS
BLOCKS
PROJECT
DEPARTMENT
EMPLOYEE
2000
50
10000
100
5
2000
(a)
(c) INDEX_NAME
PROJ_PLOC
EMP_SSN
EMP_SAL
NUM_DISTINCT
LOW_VALUE
HIGH_VALUE
200
2000
50
50
50
10000
50
500
1
1
1
1
1
1
1
1
200
2000
50
50
50
10000
50
500
UNIQUENES
BLEVEL*
LEAF_
BLOCKS
DISTINCT
_KEYS
NONUNIQUE
UNIQUE
NONUNIQUE
1
1
1
4
50
50
200
10000
500
*BLEVEL is the number of levels without the leaf level.
© Addison Wesley Longman, Inc. 2000, Elmasri/Navathe, Fundamentals of Database Systems, Third Edition
Was this manual useful for you? yes no
Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Download PDF

advertising