Download presentation
Presentation is loading. Please wait.
1
CS4432: Database Systems II
Lecture #13 Professor Elke A. Rundensteiner CS 4432 query processing
2
Query Processing Query in SQL Query Plan in Algebra CS 4432
3
Example Data: relation R (A, B, C) relation S (C, D, E) Query:
SELECT B, D FROM R, S WHERE R.A = “c” and S.E = 2 and R.C=S.C CS 4432 query processing
4
SELECT B, D FROM R, S WHERE R.A = “c” and S.E = 2 and R.C=S.C
R A B C S C D E a x 2 b y 2 c z 2 d x 1 e y 3 Answer B D x CS 4432 query processing
5
How do we execute query? One idea
- Form Cartesian product of all tables in FROM-clause - Select tuples that match WHERE-clause - Project columns that occur in SELECT-clause CS 4432 query processing
6
R X S R.A R.B R.C S.C S.D S.E a x 2 a y 2 . C x 2 Bingo! Got one... CS 4432 query processing
7
But ? Performance would be unacceptable!
We need a better approach for: reasoning about queries, their execution orders and their respective costs CS 4432 query processing
8
Relational Algebra ? Query: SELECT B, D FROM R, S
WHERE R.A = “c” and S.E = 2 and R.C=S.C CS 4432 query processing
9
Relational Algebra - can be used to describe plans...
Ex: Plan I B,D sR.A=“c” S.E=2 R.C=S.C X R S OR: B,D [ sR.A=“c” S.E=2 R.C = S.C (RXS)] CS 4432 query processing
10
Example Instances “Sailors” and “Reserves” relations R1 S1 S2 CS 4432
query processing 5
11
Relational Algebra Basic operations: Additional operations:
Selection ( ) Selects a subset of rows from relation. Projection ( ) Deletes unwanted columns from relation. Cross-product ( ) Allows us to combine two relations. Set-difference ( ) Tuples in reln. 1, but not in reln. 2. Union ( ) Tuples in reln. 1 and in reln. 2. Additional operations: Intersection, join, division, renaming: Not essential ! Algebra is “closed”: Since each operation returns a relation, operations can be composed ! CS 4432 query processing 6
12
Projection CS 4432 query processing 7
13
Selection CS 4432 query processing 8
14
Union, Intersection, Set Difference
Operate on two union-compatible relations: Same number of fields. `Corresponding’ fields have same type. CS 4432 query processing 9
15
Cross-Product Each row of S1 is paired with each row of R1.
Conflict: Both S1 and R1 have a field called sid. Renaming operator: CS 4432 query processing 10
16
Joins Condition Join : Result schema same as that of cross-product.
CS 4432 query processing 11
17
Joins Equi-Join: condition contains only equalities.
Result schema only one copy of fields for which equality is specified. Natural Join: Equijoin on all common fields. CS 4432 query processing 12
18
Division Not primitive operator, but useful:
Find sailors who have reserved all boats. A has 2 fields x and y; B has only field y: A/B = i.e., A/B contains all x tuples (sailors) such that for every y tuple (boat) in B, there is an xy tuple in A. CS 4432 query processing 13
19
Examples of Division A/B
CS 4432 query processing 14
20
Expressing A/B Using Basic Operators
Division is useful shorthand. Idea: For A/B, compute all x values that are not `disqualified’ by some y value in B. Disqualified x values: A/B: all disqualified tuples CS 4432 query processing 15
21
Find names of sailors who’ve reserved boat #103
Solution 1: Solution 2: Solution 3: CS 4432 query processing 16
22
Find names of sailors who’ve reserved a red boat
Information about boat color only available in Boats; so need an extra join: A more efficient solution: A query optimizer can find this, given the first solution! CS 4432 query processing 17
23
Find sailors who’ve reserved a red or a green boat
Can identify all red or green boats, then find sailors who’ve reserved one of these boats: Can also define Tempboats using union! (How?) What happens if is replaced by in this query? CS 4432 query processing 18
24
Find sailors who’ve reserved a red and a green boat
Must identify sailors who’ve reserved red boats, sailors who’ve reserved green boats, then find the intersection (sid is a key for Sailors): CS 4432 query processing 19
25
Find names of sailors who’ve reserved all boats
Uses division; schemas of input relations to / must be carefully chosen: To find sailors who’ve reserved all ‘Interlake’ boats: ..... CS 4432 query processing 20
26
Relational Algebra representation used to describe plans...
CS 4432 query processing
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.