heuristic approch monika sanghani
Post on 11-Feb-2017
193 Views
Preview:
TRANSCRIPT
Click to add Title
Query Optimization Using Heuristic Approach
e-Infochips Institute of Training Research and Academics Limited
Prepared By:Monika Sanghani
2
Query Optimization
Query optimization Techniques
Translating SQL into Relational Algebra Operations
Heuristic examples
Summary
Outlines
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.
Typical Steps In Processing
1.Parsing & translation2.Optimization3.Evaluation4. Execution
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
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
Simple Example of Query Block
Translating SQL into Relational Algebra
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.
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
Structure of the Initial Query Tree
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
Analysis of the Initial Query Tree
(Set operations : Unions, Intersections, Complements, Cartesian product, etc. Aggregate functions : Average, Count, Max, Min, Sum, etc.)
B. Moving SELECT operations down the query tree
Question for You
C. Applying the more restrictive SELECT operation first
D. Replacing CARTESIAN PRODUCT and SELECT with JOIN operations
E. Moving PROJECT operations down the query tree
Effect of Project Operations
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.
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’;
Cost Based Optimization
How Optimization Make Efficient Database
Summary (heuristic optimization)
Thank you
top related