Professional Documents
Culture Documents
Chapter 15
Algorithms
for
Query Processing
And
Optimization
Elmasri/Navathe, Fundamentals of D
Chapter Outline
Elmasri/Navathe, Fundamentals of D
Translating SQL Queries Into Relational Algebra
Elmasri/Navathe, Fundamentals of D
Translating SQL Queries Into Relational Algebra
Elmasri/Navathe, Fundamentals of D
Translating SQL Queries Into Relational Algebra
Elmasri/Navathe, Fundamentals of D
Algorithms For External Sorting
The result of a SQL query containing ORDERED BY-
clause needs to be sorted
Elmasri/Navathe, Fundamentals of D
Algorithms For SELECT and JOIN Operations
Elmasri/Navathe, Fundamentals of D
Implementing the JOIN Operation
Methods for implementing Joins
Nested-loop join (brute force) –for each record in outer-
loop retrieve every records from inner-loop
Elmasri/Navathe, Fundamentals of D
Algorithms For PROJECT and SET Operations
Elmasri/Navathe, Fundamentals of D
Aggregate Operations and OUTER JOINS
The aggregate operations MIN, MAX, COUNT,
AVERAGE, and SUM can be computed by scanning
the whole records (the worst case)
Elmasri/Navathe, Fundamentals of D
Using Heuristics in Query Optimization
The parser first generates an initial internal
representation, the uses heuristic rules to optimize
Elmasri/Navathe, Fundamentals of D
FIGURE 15.4 (b)
Initial (canonical) query tree for SQL query
SELECT PNUMBER, DNUM, LNAME, ADDRESS, BDATE
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE DNUM=DNUMBER AND MGRSSN=SSN AND PLOCATION=‘Stafford’
Elmasri/Navathe, Fundamentals of D
FIGURE 15.4 (a)
Query tree corresponding to the relational algebra expression for the SQL query
SELECT PNUMBER, DNUM, LNAME, ADDRESS, BDATE
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE DNUM=DNUMBER AND MGRSSN=SSN AND PLOCATION=‘Stafford’
Elmasri/Navathe, Fundamentals of D
Using Heuristics in Query Optimization
Execution of the query tree:
Elmasri/Navathe, Fundamentals of D
FIGURE 15.4 (c)
Query graph corresponding to the SQL query
SELECT PNUMBER, DNUM, LNAME, ADDRESS, BDATE
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE DNUM=DNUMBER AND MGRSSN=SSN AND PLOCATION=‘Stafford’
Elmasri/Navathe, Fundamentals of D
Using Heuristics in Query Optimization
Example of Transforming a Query:
SELECT LNAME
FROM EMPLOYEE, WORKS_ON, PROJECT
WHERE PNAME=‘Aquarius’ AND ESSN=SSN
AND PNUMBER=PNO
AND BDATE > ‘1957-12-31’;
Elmasri/Navathe, Fundamentals of D
FIGURE 15.5
Steps in converting a query tree during heuristic optimization.
(a) Initial (canonical) query tree for SQL query Q.
Elmasri/Navathe, Fundamentals of D
FIGURE 15.5 (b)
Moving SELECT
operations
down the
query tree.
Elmasri/Navathe, Fundamentals of D
FIGURE 15.5 (c)
Applying
the more
restrictive
SELECT
operation first.
Elmasri/Navathe, Fundamentals of D
FIGURE 15.5 (d)
Replacing CARTESIAN PRODUCT and SELECT with JOIN
operations.
Elmasri/Navathe, Fundamentals of D
FIGURE 15.5 (e)
Moving
PROJECT
operations
down the
query tree.
Elmasri/Navathe, Fundamentals of D
Outline of a Heuristic Algebraic optimization Algorithm
Elmasri/Navathe, Fundamentals of D
Converting Query Trees into Query Execution Plan
Elmasri/Navathe, Fundamentals of D
Using Selectivity and Cost in Query Optimization
Elmasri/Navathe, Fundamentals of D
Overview of Query Optimization in ORACLE
The ORACLE DBMS provides two different
approaches to query optimization:
Elmasri/Navathe, Fundamentals of D
Semantic Query Optimization
An alternative approach to query optimization is
the semantic query optimization. It uses
constraints specified on the database scheme.
Elmasri/Navathe, Fundamentals of D