QUERY OPTIMIZATION
What is Query Optimization?
• Query Processing: The process by which the query results are retrieved from a
high-level query such as SQL.
• Query Optimization: The process of choosing a suitable execution strategy for
retrieving results of query from database files for processing a query is known as
Query Optimization.
• Query optimization is a function of many relational database management
systems. The query optimizer attempts to determine the most efficient way to
execute a given query by considering the possible query plans.
Query Optimization Criteria
• Optimization criteria:
• Reduce total execution time of the query:
• Minimize the sum of the execution times of all individual operations
• Reduce the number of disk accesses
• Reduce response time of the query:
• Maximize parallel operations
Query Optimization Issues
• Query rewriting:
• transformations from one SQL query to another one using semantic properties.
• Selecting query execution plan:
• done on single query blocks
• Cost estimation:
• to compare between plans we need to estimate their cost using statistics on the
database.
Steps in Query Optimization
• Query optimization involves three steps:
1. Query Tree Generation:
A Query Tree is a tree data structure representing a relational algebra expression. The tables of
the query are represented as leaf nodes. The relational algebra operations are represented a
internal nodes. The root represents the query as a whole.
2. Query Plan Generation:
After the Query Tree is generated, a query plan is made. A query plan is an extended query tree
that includes access paths for all operation in the query tree. Access paths specify how the
relational operations in the tree should be performed.
3. Query Plan Code Generation:
Code Generation is the final step in the Query Optimization. It is the executable form of the
query. Once the query code is generated, the execution manager runs it and produces the results.
Two main Techniques for Query Optimization
 Heuristic Rules:
• Rules for ordering the operations in query optimization.
 Systematical estimation:
It estimates cost of different execution strategies and chooses the execution
plan with lowest execution cost.
Heuristic Approach
• Heuristic - problem-solving by experimental methods
• Applying general rules to choose the most appropriate internal query
representation
• Based on transformation rules for relational algebra operations
Transformation Rules
• Cascade of selection operations:
• Commutativity of selection operations
• Sequence of projection operations
where
)))((()( RR rqprqp  
))(())(( RR pqqp  
)...(
)(...
NML
R LNML


Heuristic Rules
• Perform selection as early as possible
• Combine Cross product with a subsequent selection
• Rearrange base relations so that the most restrictive selection is executed first.
• Perform projection as early as possible
• Compute common expressions once.
Systematical Estimation
• Cost Estimation Components:
• Cost of access to secondary storage
• Storage cost – cost of storing intermediate results
• Computation cost
• Memory usage cost – usage of RAM buffers
Systematical Estimation (Conti.)
• Cost Estimation for Relational Algebra Expressions:
• Formulae for cost estimation of each operation
• Estimation of relational algebra expression
• Choosing the expression with the lowest cost
Systematical Estimation (Conti.)
• Cost Estimation in Query Optimization:
• Based on relational algebra tree
• For each node in the tree the estimation is to be done for:
• the cost of performing the operation;
• the size of the result of the operation;
• whether the result is sorted.
Advantages of Query Optimization
1. Faster processing of Query.
2. Lesser cost per Query.
3. High performance of the system.
4. Lesser stress on the database.
5. Efficient usage of database engine.
6. Lesser memory is consumed.
Thank You!

Query optimization

  • 1.
  • 2.
    What is QueryOptimization? • Query Processing: The process by which the query results are retrieved from a high-level query such as SQL. • Query Optimization: The process of choosing a suitable execution strategy for retrieving results of query from database files for processing a query is known as Query Optimization. • Query optimization is a function of many relational database management systems. The query optimizer attempts to determine the most efficient way to execute a given query by considering the possible query plans.
  • 3.
    Query Optimization Criteria •Optimization criteria: • Reduce total execution time of the query: • Minimize the sum of the execution times of all individual operations • Reduce the number of disk accesses • Reduce response time of the query: • Maximize parallel operations
  • 4.
    Query Optimization Issues •Query rewriting: • transformations from one SQL query to another one using semantic properties. • Selecting query execution plan: • done on single query blocks • Cost estimation: • to compare between plans we need to estimate their cost using statistics on the database.
  • 5.
    Steps in QueryOptimization • Query optimization involves three steps: 1. Query Tree Generation: A Query Tree is a tree data structure representing a relational algebra expression. The tables of the query are represented as leaf nodes. The relational algebra operations are represented a internal nodes. The root represents the query as a whole. 2. Query Plan Generation: After the Query Tree is generated, a query plan is made. A query plan is an extended query tree that includes access paths for all operation in the query tree. Access paths specify how the relational operations in the tree should be performed. 3. Query Plan Code Generation: Code Generation is the final step in the Query Optimization. It is the executable form of the query. Once the query code is generated, the execution manager runs it and produces the results.
  • 6.
    Two main Techniquesfor Query Optimization  Heuristic Rules: • Rules for ordering the operations in query optimization.  Systematical estimation: It estimates cost of different execution strategies and chooses the execution plan with lowest execution cost.
  • 7.
    Heuristic Approach • Heuristic- problem-solving by experimental methods • Applying general rules to choose the most appropriate internal query representation • Based on transformation rules for relational algebra operations
  • 8.
    Transformation Rules • Cascadeof selection operations: • Commutativity of selection operations • Sequence of projection operations where )))((()( RR rqprqp   ))(())(( RR pqqp   )...( )(... NML R LNML  
  • 9.
    Heuristic Rules • Performselection as early as possible • Combine Cross product with a subsequent selection • Rearrange base relations so that the most restrictive selection is executed first. • Perform projection as early as possible • Compute common expressions once.
  • 10.
    Systematical Estimation • CostEstimation Components: • Cost of access to secondary storage • Storage cost – cost of storing intermediate results • Computation cost • Memory usage cost – usage of RAM buffers
  • 11.
    Systematical Estimation (Conti.) •Cost Estimation for Relational Algebra Expressions: • Formulae for cost estimation of each operation • Estimation of relational algebra expression • Choosing the expression with the lowest cost
  • 12.
    Systematical Estimation (Conti.) •Cost Estimation in Query Optimization: • Based on relational algebra tree • For each node in the tree the estimation is to be done for: • the cost of performing the operation; • the size of the result of the operation; • whether the result is sorted.
  • 13.
    Advantages of QueryOptimization 1. Faster processing of Query. 2. Lesser cost per Query. 3. High performance of the system. 4. Lesser stress on the database. 5. Efficient usage of database engine. 6. Lesser memory is consumed.
  • 14.