0% found this document useful (0 votes)
11 views32 pages

CO3 Session 7

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views32 pages

CO3 Session 7

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

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)

Nested queries are not query blocks, but are


identified as separate query blocks

Aggregate operators in SQL must be included in


the extended algebra

SQL queries are first decomposed into query


blocks, then translated into equivalent
extended relational algebra expressions
Relational
Algebra
• Relational algebra defines basic
operations on relation instances
• Results of operations are also relation
instances

9
 The relational operators have the
property of closure; that is, the use of
relational algebra operators on
existing relations (tables) produces
new relations.

 There is no need to examine the


mathematical definitions, properties
and characteristics of those relational
algebra operators.

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

 Remaining operators can be carried out on the result of such


SQL block
SELEC
T
 Also known as RESTRICT
 Yields values for all rows found in a
table.
 Used to list all of the row values, or
can yield only those row values that
match a specified criterion.
 SELECT yields a horizontal subset of
a table.

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)

 (OP5):  ESSN=123456789 AND PNO=10(WORKS_ON)

Copyright © 2007 Ramez Elmasri and Shamkant B. Slide 15- 14


Translating SQL Queries into
Relational Algebra
SELECT LNAME, FNAME
FROM EMPLOYEE
WHERE SALARY > ( SELECT MAX (SALARY)
FROM EMPLOYEE
WHERE DNO = 5);

SELECT LNAME, FNAME SELECT MAX


FROM EMPLOYEE FROM (SALARY)
WHERE SALARY > C WHERE EMPLOYEE
DNO = 5
ℱMAX SALARY (σDNO=5 (EMPLOYEE))
πLNAME, FNAME (σSALARY>C(EMPLOYEE))
Copyright © 2007 Ramez Elmasri and Shamkant B. Slide 15- 15
ORIGINAL TABLE

P_CODE P_DESCRIPT PRICE P_CODE P_DESCRIPT PRICE


123456 Flashlight $5.26
123456 Flashlight $5.26
123457 Lamp $25.15
SELECT ALL 123457 Lamp $25.15
123458 Box Fan $10.99
yields 123458 Box Fan $10.99
123459 9V Battery $1.29
123459 9V Battery $1.29
254688 100W Bulb $1.47
254688 100W Bulb $1.47
311452 Powerdrill $34.99
311452 Powerdrill $34.99

SELECT only PRICE less than $2.00 P_CODE P_DESCRIPT PRICE


yields 123459 9V Battery $1.29
254688 100W Bulb $1.47

SELECT only P_CODE = 311452 yields P_CODE P_DESCRIPT PRICE

311452 Powerdrill $34.99

16
PROJEC
T
 Yields all values for selected
attributes.

 It yields a vertical subset of a


table.

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.

 Methods to remove duplicate tuples


1. Sorting
2. Hashing
ORIGINAL NEW TABLE
TABLE
P_CODE P_DESCRIPT PRICE PRICE
123456 Flashlight $5.26
PROJECT PRICE $5.26
123457 Lamp $25.15
yields $25.15

123458 Box Fan $10.99 $10.99


$1.92
123459 9V Battery $1.29
$1.47
254688 100W Bulb $1.47
$34.99
311452 Powerdrill $34.99
P_DESCRIPT PRICE
Flashlight $5.26
Lamp $25.15
PROJECT P_DESCRIPT and PRICE Box Fan $10.99
yields 9v battery $1.92
100w bulb $1.47
Powerdrill $34.99

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);

SELECT LNAME, FNAME SELECT MAX


FROM EMPLOYEE FROM (SALARY)
WHERE SALARY > C WHERE EMPLOYEE
DNO = 5
ℱMAX SALARY (σDNO=5 (EMPLOYEE))
πLNAME, FNAME (σSALARY>C(EMPLOYEE))
The inner query block (which need to be
calculated first) could be translated into the
expression

δ <MAX SALARY> (σ <DNO=5> (EMPLOYEE))

And the outer block into the expression

π <FNAME, LNAME> (σ<SALARY > c> (EMPLOYEE))

The query optimizer would then chooses an


execution plan for each block.
Initial (canonical) query tree for SQL
SELEC query
PNUMBER, DNUM, LNAME, ADDRESS, BDATE PROJECT,
T DEPARTMENT, EMPLOYEE
FROM DNUM=DNUMBER AND MGRSSN=SSN AND
WHER PLOCATION=‘Stafford’
E

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:

Using Consider the query Q that states “Find


the last names of employees born after
Heuristics in 1957 who work on a project named
‘Aquarius’.”
Query
Optimization In SQL, this query can be specified as:

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

You might also like