CO3 Session 7
CO3 Session 7
Query Processing
Agen
da Translating SQL Queries into
Relational Algebra
Other Operations
DBMS Layers
Queries
Query Optimization
and Execution
Relational Operators
Transaction Files and Access Methods
Manager
Recovery
Buffer Management Manager
Lock
Manager Disk Space Management
DB
Query processing:
Introduction
Is the list of activities that are perform to obtain
to Query the required tuples that satisfy a given query.
Processing Query optimization:
The process of choosing a suitable execution
strategy for processing a query.
Two internal representations of a query:
Query Tree
Query Graph
Introduction
to Query
Processing
1. Parsing and translation
2. Optimization
3. Evaluation
Basic Steps
in Query
Processing
Parser & Translator:
Syntax
Schema element
Converts the query into R.A expression.
Translating translate the query into its internal form.This is then translated into
SQL Queries relational algebra.
Parser checks syntax, verifies relations
into
Relational Optimizer
Find all equivalent R.A expressions
Algebra
Find the R.A expression with least cost
Cost(CPU, Block access, time spent)
Will create query evaluation plan which tell what R.A and what
algorithm is used.
Query evaluation plan:
Evaluate the above plan and get the result
The query-execution engine takes a query-evaluation plan, executes
that plan, and returns the answers to the query.
A query block contains a single
SELECT-FROM-WHERE
expression (may contain GROUP BY and
HAVING)
9
The relational operators have the
property of closure; that is, the use of
relational algebra operators on
existing relations (tables) produces
new relations.
10
Basic
Operations
• Unary algebra
operations:
– Selection
– Projection
• Binary algebra
operations:
– Union
– Set difference
– Cross-product 1
1
Translating SQL Queries Into
Relational Algebra Trees
select name
from STUDENT, TAKES
where c-id=‘415’ and
STUDENT.ssn=TAKES.ssn
STUDENT TAKES
An SQL block can be thought of as an algebra expression containing:
A cross-product of all relations in the FROM clause
Selections in the WHERE clause
Projections in the SELECT clause
13
Algorithms for SELECT
Implementing the SELECT Operation
Examples:
(OP1): SSN='123456789' (EMPLOYEE)
(OP2): DNUMBER>5(DEPARTMENT)
(OP3): DNO=5(EMPLOYEE)
(OP4): DNO=5 AND SALARY>30000 AND SEX=F(EMPLOYEE)
16
PROJEC
T
Yields all values for selected
attributes.
17
Algorithm for PROJECT operations
Algorithm <attribute list>(R)
for PROJECT
1. If <attribute list> has a key of relation R, extract
all tuples from R with only the values for the
attributes in <attribute list>.
Operations 2. If <attribute list> does NOT include a key of
relation R, duplicated tuples must be removed from
the results.
P_CODE PRICE
123456 $5.26
PROJECT P_CODE and PRICE 123457 $25.15
yields 123458 $10.99
213345 $1.92
254467 $1.47
311452 $34.99
19
Translating SQL Queries into
Relational Algebra
SELECT LNAME, FNAME
FROM EMPLOYEE
WHERE SALARY > ( SELECT MAX (SALARY)
FROM EMPLOYEE
WHERE DNO = 5);
FIGURE
Query tree corresponding to the relational algebra expression for the
SELEC SQL query
PNUMBER, DNUM, LNAME, ADDRESS, BDATE PROJECT,
T DEPARTMENT, EMPLOYEE
FROM DNUM=DNUMBER AND MGRSSN=SSN AND
WHER PLOCATION=‘Stafford’
E
FIGURE
Execution of the query
tree:
Execute an internal node operation whenever
Using •
its operands are available and then replace
the internal node by the resulting operation.
Heuristics in Repeat step 1 as long as there are leaves in the
Query •
tree, that is, the execution terminates the root
node is executed and produces the result
Optimization relation for the query.
A more natural representation of a query
is the
query graph notation.
Query graph corresponding to the SQL
SELEC query
T PNUMBER, DNUM, LNAME, ADDRESS, BDATE PROJECT,
FROM DEPARTMENT, EMPLOYEE
WHER DNUM=DNUMBER AND MGRSSN=SSN AND
E PLOCATION=‘Stafford’
FIGURE
Example of Transforming a Query:
SELECT LNAME
FROMEMPLOYEE, WORKS_ON, PROJECT
WHERE PNAME=‘Aquarius’ AND
ESSN=SSN
AND PNUMBER=PNO
AND BDATE > ‘1957-12-31’;
Steps in converting a query tree during heuristic
optimization.
(b) Initial (canonical) query tree for SQL query Q.
Figure
Moving
SELECT
operations
down the
query tree.
FIGURE
Applying
the more
restrictive
SELECT
operation
FIGURE
first.
Replacing CARTESIAN PRODUCT and SELECT
with JOIN operations.
FIGURE
Moving
PROJECT
operation
s down
the
FIGURE
query
tree.
THE END