heuristic approch monika sanghani

26
Click to add Title Query Optimization Using Heuristic Approach e-Infochips Institute of Training Research and Aca Prepared By: Monika Sanghani

Upload: monika-sanghani

Post on 11-Feb-2017

196 views

Category:

Education


4 download

TRANSCRIPT

Page 1: Heuristic approch  monika sanghani

Click to add Title

Query Optimization Using Heuristic Approach

e-Infochips Institute of Training Research and Academics Limited

Prepared By:Monika Sanghani

Page 2: Heuristic approch  monika sanghani

2

Query Optimization

Query optimization Techniques

Translating SQL into Relational Algebra Operations

Heuristic examples

Summary

Outlines

Page 3: Heuristic approch  monika sanghani

What is Query Optimization and Why

• The activity of choosing an efficient execution strategy for processing a query.

• An important aspect of query processing is query optimization.

• The aim of query optimization is to choose the one that minimizes resource usage.

• The part of the DBMS responsible for selecting an execution strategy is the query optimizer, and it passes the selected strategy to the query-code generator which creates the code and sends it off to be processed.

Page 4: Heuristic approch  monika sanghani

Typical Steps In Processing

1.Parsing & translation2.Optimization3.Evaluation4. Execution

Page 5: Heuristic approch  monika sanghani

5

Heuristic query optimization Cost-based query optimization

-Logical optimization-Oracle calls this Rule Based optimization.Two Ways:• Query tree (relational algebra)

optimization• Query graph optimization

-Physical optimization-Several Cost components to be consider: • Access cost  • Storage Cost • Computation costs• Communications Costs 

Query optimization Techniques

Page 6: Heuristic approch  monika sanghani

Query block: The basic unit that can be translated into the algebraic operators and optimized.

A query block contains a single SELECT-FROM-WHERE expression, as well as GROUP BY and HAVING clause if these are part of the block.

Nested queries within a query are identified as separate query blocks.

Translating SQL Queries into Relational Algebra

Page 7: Heuristic approch  monika sanghani

Simple Example of Query Block

Page 8: Heuristic approch  monika sanghani

Translating SQL into Relational Algebra

Page 9: Heuristic approch  monika sanghani

Query tree

Tree that represents a relational algebra expression.• Leaves = base tables.• Internal nodes = relational algebra operators applied to the node’s children.• The tree is executed from leaves to root.

Page 10: Heuristic approch  monika sanghani

1. Break up conjunctive select into cascade

2. Move down select as far as possible in the tree

3. Rearrange select operations: The most restrictive(ones that produce a relation with the fewest tuples or with the smallest absolute size.) should be executed first

4. Convert Cartesian product followed by selection into join

5. Move down project operations as far as possible in the tree. Create new projections so that only the required attributes are involved in the tree

6. Identify sub trees that can be executed by a single algorithm

Heuristic Algorithm

Page 11: Heuristic approch  monika sanghani

Structure of the Initial Query Tree

Page 12: Heuristic approch  monika sanghani

Heuristic Query Tree Optimization

Example :SELECT LNAMEFROM EMPLOYEE, WORKS_ON, PROJECTWHERE PNAME= ‘AQUARIUS’ AND PNUMBER=PNO AND ESSN=SSN AND BDATE > ‘1957-12-31’

A. Initial Query tree

Page 13: Heuristic approch  monika sanghani

Analysis of the Initial Query Tree

(Set operations : Unions, Intersections, Complements, Cartesian product, etc. Aggregate functions : Average, Count, Max, Min, Sum, etc.)

Page 14: Heuristic approch  monika sanghani

B. Moving SELECT operations down the query tree

Page 15: Heuristic approch  monika sanghani

Question for You

Page 16: Heuristic approch  monika sanghani

C. Applying the more restrictive SELECT operation first

Page 17: Heuristic approch  monika sanghani

D. Replacing CARTESIAN PRODUCT and SELECT with JOIN operations

Page 18: Heuristic approch  monika sanghani

E. Moving PROJECT operations down the query tree

Page 19: Heuristic approch  monika sanghani

Effect of Project Operations

Page 20: Heuristic approch  monika sanghani

Query Graph

Query graph: a graph data structure that does not indicate an order on which operations to perform first. There is only a single graph corresponding to each query.

• Nodes represents Relations.

• Ovals represents constant nodes.

• Edges represents Join & Selection conditions.

• Attributes to be retrieved from relations represented in square brackets.

Page 21: Heuristic approch  monika sanghani

Example :For every project located in ‘Stafford’, retrieve the project number, the

controlling department number and the department manager’s last name, address and birthdate.

SQL query: SELECT P.NUMBER,P.DNUM,E.LNAME, E.ADDRESS, E.BDATE

FROM PROJECT AS P,DEPARTMENT AS D, EMPLOYEE AS E WHERE P.DNUM=D.DNUMBER AND D.MGRSSN=E.SSN AND

P.PLOCATION=‘STAFFORD’;

Page 22: Heuristic approch  monika sanghani

Cost Based Optimization

Page 23: Heuristic approch  monika sanghani

How Optimization Make Efficient Database

Page 24: Heuristic approch  monika sanghani
Page 25: Heuristic approch  monika sanghani

Summary (heuristic optimization)

Page 26: Heuristic approch  monika sanghani

Thank you