0% found this document useful (0 votes)
127 views

Lecture 20+Query+Processing+ +opt

The document discusses query processing and optimization in databases. It covers: - The major phases of query processing: scanning, parsing, validating, optimizing, and executing queries. - The goal of query optimization is to select an efficient execution plan to minimize resource usage and response time. - The main techniques for query optimization are heuristic rules and cost-based optimization. - Heuristic optimization involves translating queries into trees/graphs, applying relational algebra rules, and rearranging operations based on heuristics like pushing down restrictive selections early.

Uploaded by

Zain Ul Islam
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
127 views

Lecture 20+Query+Processing+ +opt

The document discusses query processing and optimization in databases. It covers: - The major phases of query processing: scanning, parsing, validating, optimizing, and executing queries. - The goal of query optimization is to select an efficient execution plan to minimize resource usage and response time. - The main techniques for query optimization are heuristic rules and cost-based optimization. - Heuristic optimization involves translating queries into trees/graphs, applying relational algebra rules, and rearranging operations based on heuristics like pushing down restrictive selections early.

Uploaded by

Zain Ul Islam
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22

Query Processing and

Optimization

July 17, 2016

Databases: Query Proc. &


Opt.

Objectives

Introduction +
Major Phases in Query Processing +
Optimizing Queries +

July 17, 2016

Steps in Query Optimization +


Query Trees and Graphs +
Relational Algebra Transformation +
Query Processing and Optimization +

Databases: Query Proc. & Opt.

- Introduction

With declarative languages such as SQL, the


user specifies what data is required rather
than how it is retrieved

Relieves user of knowing what constitutes a


good execution strategy

Also gives the DBMS more control over system


performance.

July 17, 2016

Databases: Query Proc. & Opt.

-- Processing a High-Level Query


Query in high level language
Scanning, parsing And
Validating
Intermediate form of query
Query Optimizer
Execution plan

Query code
Generator

Code to execute the query


Run time DB Processor
Query result
July 17, 2016

Databases: Query Proc. & Opt.

- Major Phases in Query Processing

Scan, Parse, and Validate Query: Transform query


written in high level language (e g. SQL), into
correct efficient execution strategy expressed in
low-level language (e. g. Relational Algebra)

Optimize query and generate code: produce an


execution strategy or plan and run-time code.

Execute query: Implement the plan to retrieve


required data.

July 17, 2016

Databases: Query Proc. & Opt.

- Optimizing Queries

The objective in query optimization is to select an efficient


execution strategy.
As there are many equivalent transformations from same highlevel query, aim is to choose the one that minimizes resource
usage.
Generally, an efficient execution plan reduces the total execution
time of a query; thereby reducing the response time of a query.
The problem is computationally intractable with large number of
relations, so the strategy adopted is reduced to finding a near
optimum solution.
Two main techniques for query optimization are:

July 17, 2016

Heuristic rules that order operations in a query


Comparing different strategies based on relative cost, and selecting
one that minimizes resource usage.

Databases: Query Proc. & Opt.

- Steps in Query Optimization

In using heuristics during query optimization,


the following steps must be followed:
1.

Translate queries into query trees or query graphs

2.

Apply transformation rules for relational algebra


expression.

3.

Perform heuristic optimization

July 17, 2016

Databases: Query Proc. & Opt.

-- Query Trees and Graphs

A query tree is a tree data structure that corresponds


to a relational algebra expression. It represents the
input relations of the query as leaf nodes of the tree,
and represents the relational algebra operations as
internal nodes

An execution of the query tree consists of executing an


internal node operation whenever its operands are
available and then replacing that internal node by the
relation that results from executing the operation.

The execution terminates when the root node is


executed and produces the result relation for the query.

July 17, 2016

Databases: Query Proc. & Opt.

--- Query Tree Example

For every project located in Stafford, list the project


number, the controlling department number and the
department managers last name, address, and birth date.
SELECT Pnumber, Dnum, Lname, Address, Bdate
FROM Project, Department, Employee
WHERE Dnum=Dnumber
AND MgrSSN = SSN
AND Plocation = Stafford

Equivalent relational algebra expression is:


Pnumber, Dnum, Lname, Address, Bdate
(((Plocation = Stafford(Project))
Dnum=Dnumber(Department))
MgrSSN=SSN(Employee))

Note:
July 17, 2016

stands for JOIN


Databases: Query Proc. & Opt.

--- Query Tree: Example


P.Pnumber, P.Dnum, E.Lname, E.Address, E.Bdate
D.MGRSSN=E.SSN
P.Dnum=D.Dnumber
P.Location=Stafford
P

July 17, 2016

Heuristic
optimization

Note: The symbol


Databases: Query Proc. & Opt.

represents JOIN
10

--- Query Tree: Example


P.Pnumber, P.Dnum, E.Lname, E.Address, E.Bdate
P.Dnum=D.Dnumber AND D.MGRSSN=E.SSN AND P.Location=Stafford
Heuristic
optimization

P
July 17, 2016

Databases: Query Proc. & Opt.

11

--- Query Graph Example

For every project located in Stafford, list the


project number, the controlling department
number, and the department managers last
name, address, and birth date.
SELECT Pnumber, Dnum, Lname, Address, Bdate
FROM Project, Department, Employee
WHERE Dnum = Dnumber
AND MgrSSN = SSN
AND Plocation = Stafford

July 17, 2016

Databases: Query Proc. & Opt.

12

--- Query Graph Example

[E.lname,E.aAddress, E.Bdat

[P.Pnumber, P.Dum]

P.Dum=D.Numbr

D.MgrSSN=E.SSN

P.Plocation = Stafford

Stafford

July 17, 2016

Databases: Query Proc. & Opt.

13

- Relational Algebra Transformation

The following list gives a basic selection of relational


algebra transformation:

Cascade of selection: Conjunctive selection operations can


cascade into individual selection operations

p AND q AND r (R) = p (q (r (R)))


Cascade of projection: In a sequence of projection
operations, only the last in sequence is required.

L(M((N(R) = L(R)

Commutatively of selection: A sequence of selection


operations are commutative

q(p(R)) = p(q(R))

July 17, 2016

Associativity of Natural Join and Cross Product: Natural


join (*) and Cross Product (X) are associative:
(R * S) * T = R * (S * T)
(R X S) X T = R X (S X T)
Databases: Query Proc. & Opt.

14

-- Heuristic Optimization

Perform selection operations as early as possible

Combine Cartesian product with subsequent selection


whose predicate represents join condition into a join
operation.
Use associative of binary operations to rearrange leaf
nodes with most restrictive selection operations first.
Perform projection as early as possible

Keep predicates on same relation together

Keep projection attributes on same relations together.

Compute common expressions once.

July 17, 2016

If common expression appears more than once, and result not


too large, store result and reuse it when required.
Useful when querying views, as same expression is used to
construct view each time.
Databases: Query Proc. & Opt.

15

-- Query Optimization: Example

Query: Find the last name of employees born


after 1975 who work on a project named
Aquarius
SELECT Lname
FROM Employee, Works_on, Project
WHERE Pname = Aquarius
AND ESSN = SSN
AND Pnumber = PNO
AND Bdate > 1975-12-31

July 17, 2016

Databases: Query Proc. & Opt.

16

-- Query Optimization: Example


Lname

Pname=Aquirius AND P.Number=PNO AND ESSN=SSN AND Bdate=Dec-31-1957

Initial Canonical
Query Tree

Employee
July 17, 2016

Heuristic
optimization
Project

Works_On
Databases: Query Proc. & Opt.

17

-- Query Optimization: Example


Lname
Pnumber=PNO

Moving SELECT down

Heuristic
optimization

ESSN=SSN

Pname=Aquarius

BDate>1957-12-13

Project
Works_On

Employee
July 17, 2016

Databases: Query Proc. & Opt.

18

-- Query Optimization: Example


Lname
Apply more restrictive SELECT

ESSN=SSN
Heuristic
optimization

Pnumber=PNO

BDate>1957-12-13

Pname=Aquirius

Employee
Works_On

Project
July 17, 2016

Databases: Query Proc. & Opt.

19

-- Query Optimization: Example

Replace CARTESIAN PRDUCT


And SELECT with JOIN

Lname
ESSN=SSN

Pnumber=Pno

Pname=Aquirius

Heuristic
optimization

BDate>1957-12-13

Works_On

Employee

Project

July 17, 2016

Databases: Query Proc. & Opt.

20

-- Query Optimization: Example


Lname

Move PROJECTION down

ESSN=SSN

SSN,Lname

ESSN
Pnumber=Pno

Pnumber
Pname=Aquirius

Heuristic
optimization

BDate>1957-12-13

ESSN,PNO
Employee
Works_On

Project
July 17, 2016

Databases: Query Proc. & Opt.

21

Exercise:

In context of query optimization, convert the


following left outer join query into equivalent
sequence of relational algebraic expressions
Select emp.ename,dept.dname
From (EMP left outer join DEPT ON
dept.deptno = emp.deptno);

July 17, 2016

Databases: Query Proc. & Opt.

22

You might also like