Query Processing
Query Processing
Relational Algebra
University of Waterloo
1-1
List of Slides
1
2 How do we Execute Queries?
3 Relational Algebra
4 Examples
5 Projection
6 Selection
7 Product
8 Union
9 Difference
10 Calculus-to-Algebra Translation
11 Joins
12 Duplicate Operations
13 Example
14 Algebra Equivalences
15 Implementation of the Operators
16 Atomic Relations
17 Joins
18 Duplicates and Aggregates
19 The rest of the lot
20 Summary
Query Processing: Relational Algebra: 2
3. Optimization:
generates an efficient query plan
uses statistics collected about the stored data
4. Plan execution:
access methods to access stored relations
physical relational operators to combine relations
Relational Algebra
is implemented as
universe : finite relations over DOM
and relational operations:
Projection: ! " ,
Selection: #$ "
Product: % % "
Union: & % "
Difference: ' % "
Algebraic Approach:
Tarski, 1931
Cylindric Algebra
Codd, 1970
Relational Algebra
matches range-restricted queries
defined only for finite relations (no top)
Query Processing: Relational Algebra: 4
Examples
Account
Acnt# Type Balance Bank Branch
1234 CHK $1000 TD 1
1235 SAV $20000 TD 2
1236 CHK $2500 CIBC 1
1237 CHK $2500 Royal 5
2000 BUS $10000 Royal 5
2001 BUS $10000 TD 3
Bank
Name Address
TD TD Centre
CIBC CIBC Tower
Projection
Definition:
Example:
!#"%$
1234 CHK
1235 SAV
1236 CHK
1237 CHK
2000 BUS
2001 BUS
Selection
Definition:
#
Example:
#
!#" $
Product
Definition:
%
Example: #" $ %
"
Union
Definition:
&
Example:
# ! " $ & #
!#"%$
1234 CHK
1236 CHK
1237 CHK
1235 SAV
Difference
Definition:
Example:
Is there an account without a bank?
!#"%$ ' #
!#"%$ %
"
1237 Royal
2000 Royal
Calculus-to-Algebra Translation
Theorem [Codd]:
For every (safe) relational calculus
query there is an equivalent RA expression
Joins
An equality condition after product (common situation):
1234 CHK $1000 TD 1 TD TD Centre
1235 SAV $20000 TD 2 TD TD Centre
1236 CHK $2500 CIBC 1 TD TD Centre
1237 CHK $2500 Royal 5 TD TD Centre
2000 BUS $10000 Royal 5 TD TD Centre
2001 BUS $10000 TD 3 TD TD Centre
1234 CHK $1000 TD 1 CIBC CIBC Tower
1235 SAV $20000 TD 2 CIBC CIBC Tower
1236 CHK $2500 CIBC 1 CIBC CIBC Tower
1237 CHK $2500 Royal 5 CIBC CIBC Tower
2000 BUS $10000 Royal 5 CIBC CIBC Tower
2001 BUS $10000 TD 3 CIBC CIBC Tower
1234 CHK $1000 TD 1 TD TD Centre
1235 SAV $20000 TD 2 TD TD Centre
2001 BUS $10000 TD 3 TD TD Centre
1236 CHK $2500 CIBC 1 CIBC CIBC Tower
Duplicate Operations
Example
SELECT V.Vno, Vname, Count(*), Sum{Amount}
FROM Vendor V, Transaction T
WHERE V.Vno = T.Vno
AND V.Vno Between 1000 and 2000
GROUP BY V.Vno, Vname
HAVING sum(Amount) > 100
Result
Scan
(Sum(Amount) > 100)
Grouping
(Vno, Vname)
Join
(V.Vno = T.Vno)
Scan
(Vno between 1000 and 2000)
Vendor Transaction
Algebra Equivalences
Atomic Relations
#
Joins
Similar solution:
Summary