Chapter 2-Query Processing_110554
Chapter 2-Query Processing_110554
PR 40 13 Programming
Co 50 80 Computer Organization
Con’t…
3. Query evaluation plan:
• Query tree: is represent the relational algebraic expression in the
form of:
•Tree data structure
•Input as leaf node
•Operators as internal node
Con’t…
• Different evaluation plans for a given query can have different cost.it
is responsibility of query optimizer to generate least costly plan.
Con’t
4. Query evaluation engine:
• Evaluates the above plan and gets the results.
• Is directly accessing data warehouse.
Translating SQL queries into Relational
Algebra
Relational Algebra
• Is a procedural query language, which takes instance of relations as
input and yields instance of relation as output.
• It use operator to perform queries.
• The fundamental operation of relational algebra are:
- select - union
- project - set different
Select Operation (o)
• It selects tuples that satisfy the given predicate from a relation.
• Notation: where: o – selection predicate
p – prepositional logic
formula (condition)
r – for relation
• Example:
• Output: select tuples from books where subject is database.
Project Operation (π)
• It project columns that satisfy a given predicate .
• Notation:
• where: A1, A2,…. An. attribute names of relation r. duplicated rows
are automatically eliminated, as relation is a set.
• example: (π subject, Author (books))
• output: select and project columns named as subject and author from the relation
books.
Union Operator (u)
• It performs binary union between two given relations and is defined
as - r u s ={t\t € r or t € s}
• Notation: r u s r and s are relations.
• For a union operations to be valid, the following condition must be
hold.
• r and s must have the same number of attribute
• Attribute domains must be compatible
• Duplicate tuples are automatically eliminated.
π author (books) u π author (articles)
• Output: yields a relation, which shows all the books and articles written by tutorials
point.
Rename (p)
• The result of relational algebra are also relation but without any
name.
• The rename operation (p) allow us to rename the output relation.
• Notation: px(E) where: the result of expression E is saved with
name x.
Natural Join (⋈)
• Natural join can only be performed if there is a common attribute
(column) between the relations.
• The name and type of the attribute must be same.
• Example: consider the following two tables.
C D C ⋈D
num square num cube num square cube
2 4 2 8 2 4 8
3 9 3 27 3 9 27
Outer Join
• Along with tuples that satisfy the matching criteria, we also include
some or all tuples that do not match the criteria.
• Names and GPA of students with HS > 1000 who applied to cs and
were rejected.
foreign keys
Query:
• The manager of the company working on road construction would like
to view employees name born before January 1 1965 who are
working on the project name ring road.
• Solution: relational algebra representation of the query is:
(DoB<Jan 1 1965)
((WEmpID=EEmpID)((PProjID=WProjID)
((PName=’Ring Road’) (EMPLOYEE X WORKS_ON X
PROJECT ) ) )
(WEmp ID=EEmpID )
( PName=’Ring Road’ )
PROJECT
X
EMPLOYEE
Con’t…
• Using the third step, perform most restrictive operations first.
• From the query given we can see that selection on PROJECT is most
restrictive than selection on EMPLOYEE.
• Thus, it is better to perform selection on PROJECT BEFORE on
EMPLOYEE. rearrange the nodes to achieve this.
<FName, LName
>
( WEmpID=EEmpID
)
( PProjID=WProjID
)
(DoB<Jan1 1965
)
EMPLOY EE
X
(PName=’Ring
Road’) WORKS_ON
PROJECT
• Using the forth step, Perform Cartesian Operations with the
subsequent Selection Operation. < FName, LName >
( WEmpID=EE mpID )
(DoB<Jan 1 1965)
( PProjID=WProjID )
EMPLOY EE
(PName=’Ring Road ‘)
WORKS_ON
PROJECT
• Using the fifth step, Perform the projection as early as possible.
<FName, LName>
( WEmpID=EEmpID )
( PProjID=WProjID )
<FName,LName,EEmpID>
WORKS_ON
(DoB<Jan 1 1965)
(PName=’Ring Road’)
PROJECT
Reading Assignment
• Cost Estimation Approach to Query Optimization
END!