1 Intro Select Project
1 Intro Select Project
SQL Commands
DBMS
Query
Evaluation Engine
1
Steps in Processing a Query
SQL statement
Query
Query Optimizer
Optimizer
Execution plan
Runtime DB Processor
3
Example of Conceptual Evaluation
SELECT S.name
FROM Sailors S, Reserves R
WHERE S.sid=R.sid AND R.bid=103
4
Conceptual Evaluation Strategy
This strategy is probably the least efficient
way to compute a query!
5
Query Optimization Overview
Query Optimization Overview Cont.
Represented as a:
Query Tree
8
Relational Algebra
Relational algebra
9
Relational Algebra - Example
SELECT name, rating name,rating( Rating > 8(S))
FROM Sailors S
WHERE rating > 8
11
Query Tree
S.sname
SELECT S.sname
S.id = E.sid FROM Student S, Enrolled E
WHERE S.id = E.sid
AND S.gpa > 4
S.gpa > 4 E.cid = CSE454
AND E.cid = CS545
S E
12
Optimization of Query Trees
Get initial Query Tree
Apply Cartesian Product of relations in FROM
Selection and Join conditions of WHERE is applied
Project on attributes in SELECT
How?
Next slides..
13
Several alternatives!
14
Equivalent Query Trees
Result tuples
of first join
pipelined into
join with C C
A B
Left Deep Plans
Plan Space: Too large, must be pruned
In left-deep trees, the right child of each join node is a base table
Only the space of left-deep plans is considered as they allow
output of each operator to be pipelined into the next operator
fully pipelined plans
D D
C C A B C D
A B A B
Cost Estimation of a Plan
In this
Lecture
19
Operations of Relational Algebra
20
Projection () - Example
SID Name Rating Age
28 Yuppy 9 35
Name,Rating(S)
31 Lubber 8 55
44 Guppy 5 35
58 Rusty 10 35
S
Name Rating
The result of an operation is a: Yuppy 9
new relation
Lubber 8
Guppy 5
Rusty 10
21
Projection () - Example
SID Name Rating Age
28 Yuppy 9 35
Age(S)
31 Lubber 8 55
44 Guppy 5 35
58 Rusty 10 35
Age
35
55
22
Projection ()
23
Selection () - Example
SID Name Rating Age
28 Yuppy 9 35 σRating > 8(S)
31 Lubber 8 55
44 Guppy 5 35
58 Rusty 10 35
24
Selection ()
25
Relational Algebra Expression - Example
SID Name Rating Age
28 Yuppy 9 35 name,rating( Rating > 8(S))
31 Lubber 8 55
44 Guppy 5 35
58 Rusty 10 35
26
SELECT vs. WHERE
In SQL:
Selection (σ) is expressed by the WHERE clause
SELECT clause actually does Projection (π)
It is a historical accident
27
SELECT Queries - Examples
Point Query (equality search):
SSN='123456789' (Employee)
Range Query
DNUMBER>5 (Department)
Conjunction Query
DNO=5 AND SALARY>30000 AND GENDER=F (Employee)
Disjunction Query
DNO=5 OR SALARY>30000 OR GENDER=F (Employee)