query optimization [compatibility mode]

Upload: shalw

Post on 03-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 Query Optimization [Compatibility Mode]

    1/23

    Query Processing

  • 7/29/2019 Query Optimization [Compatibility Mode]

    2/23

    Basic Steps in Query Processing

    1.Parsing and translation2. Optimization3. Evaluation

  • 7/29/2019 Query Optimization [Compatibility Mode]

    3/23

    Basic Steps in Query Processing (Cont.)

    Parsing and translation

    translate the query into its internal form. This isthen translated into relational algebra.

    Parser checks syntax, verifies relations

    Evaluation

    The query-execution engine takes a query-evaluation plan, executes that plan, and returns theanswers to the query.

  • 7/29/2019 Query Optimization [Compatibility Mode]

    4/23

    Basic Steps in Query Processing : Optimization

    A relational algebra expression may have many equivalent expressions

    E.g., balance2500(balance(account)) is equivalent tobalance(balance2500(account))

    Each relational algebra operation can be evaluated using one of severaldifferent algorithms

    Correspondingly, a relational-algebra expression can be evaluated inmany ways.

    Detailed evaluation strategy is called anevaluation-plan. E.g., can use an index on balance to find accounts with balance 1957-12-31;

  • 7/29/2019 Query Optimization [Compatibility Mode]

    17/23

    Using Heuristics in Query Optimization

  • 7/29/2019 Query Optimization [Compatibility Mode]

    18/23

    Using Heuristics in Query Optimization

  • 7/29/2019 Query Optimization [Compatibility Mode]

    19/23

    Using Heuristics in Query Optimization

    Summary of Heurist ics for Algebraic Optimization:

    1. The main heuristic is to apply first the operations that reduce the sizeof intermediate results.

    2. Perform select operations as early as possible to reduce the numberof tuples and perform project operations as early as possible toreduce the number of attributes. (This is done by moving select andproject operations as far down the tree as possible.)

    3. The select and join operations that are most restrictive should be

    executed before other similar operations. (This is done by reorderingthe leaf nodes of the tree among themselves and adjusting the restof the tree appropriately.)

  • 7/29/2019 Query Optimization [Compatibility Mode]

    20/23

    Using Heuristics in Query Optimization

    Query Execution Plans

    An execution plan for a relational algebra query consistsof a combination of the relational algebra query tree andinformation about the access methods to be used for

    each relation as well as the methods to be used incomputing the relational operators stored in the tree.

    Materialized evaluation: the result of an operation isstored as a temporary relation.

    Pipelined evaluation: as the result of an operator is

    produced, it is forwarded to the next operator insequence.

  • 7/29/2019 Query Optimization [Compatibility Mode]

    21/23

    Heuristic Optimization

    Cost-based optimization is expensive, even with dynamicprogramming.

    Systems may use heuristics to reduce the number of choicesthat must be made in a cost-based fashion.

    Heuristic optimization transforms the query-tree by using a setof rules that typically (but not in all cases) improve executionperformance:

    Perform selection early (reduces the number of tuples)

    Perform projection early (reduces the number of attributes)

    Perform most restrictive selection and join operations (i.e.with smallest result size) before other similar operations.

    Some systems use only heuristics, others combineheuristics with partial cost-based optimization.

  • 7/29/2019 Query Optimization [Compatibility Mode]

    22/23

    Introduction (Cont.)

    An evaluation plan defines exactly what algorithm is usedfor each operation, and how the execution of the operationsis coordinated.

  • 7/29/2019 Query Optimization [Compatibility Mode]

    23/23

    Introduction (Cont.)

    Cost difference between evaluation plans for a query can beenormous

    e.g. seconds vs. days in some cases

    Steps in cost-based query optimization

    1. Generate logically equivalent expressions using equivalence

    rules2. Annotate resultant expressions to get alternative query plans

    3. Choose the cheapest plan based on estimated cost

    Estimation of plan cost based on:

    Statistical information about relations. Examples:

    number of tuples, number of distinct values for an attribute Statistics estimation for intermediate results

    to compute cost of complex expressions

    Cost formula for algorithms, computed using statistics