Interface Optimizer

All Known Implementing Classes:
OptimizerImpl

public interface Optimizer
Optimizer provides services for optimizing a query. RESOLVE: o Need to figure out what to do about subqueries, figuring out their attachment points and how to communicate them back to the caller.
  • Field Details

    • MODULE

      static final String MODULE
      Module name for the monitor's module locating system.
      See Also:
    • JOIN_ORDER_OPTIMIZATION

      static final String JOIN_ORDER_OPTIMIZATION
      Property name for controlling whether to do join order optimization.
      See Also:
    • RULE_BASED_OPTIMIZATION

      static final String RULE_BASED_OPTIMIZATION
      Property name for controlling whether to do rule-based optimization, as opposed to cost-based optimization.
      See Also:
    • NO_TIMEOUT

      static final String NO_TIMEOUT
      Property name for controlling whether the optimizer ever times out while optimizing a query and goes with the best plan so far.
      See Also:
    • MAX_MEMORY_PER_TABLE

      static final String MAX_MEMORY_PER_TABLE
      Property name for controlling the maximum size of memory (in KB) the optimizer can use for each table. If an access path takes memory larger than that size for a table, the access path is skipped. Default is 1024 (KB).
      See Also:
    • MAX_DYNAMIC_MATERIALIZED_ROWS

      static final int MAX_DYNAMIC_MATERIALIZED_ROWS
      Maximum size of dynamically created materialized rows. Caching large results use lot of memory and can cause stack overflow. See DERBY-634
      See Also:
    • USE_STATISTICS

      static final String USE_STATISTICS
      Property name for disabling statistics use for all queries.
      See Also:
    • NORMAL_PLAN

      static final int NORMAL_PLAN
      Indicates a "normal" plan that is not optimized to do sort avoidance
      See Also:
    • SORT_AVOIDANCE_PLAN

      static final int SORT_AVOIDANCE_PLAN
      Indicates a sort-avoidance plan
      See Also:
  • Method Details

    • getNextPermutation

      boolean getNextPermutation() throws StandardException
      Iterate through the permutations, returning false when the permutations are exhausted. NOTE - Implementers are responsible for hiding tree pruning of permutations behind this method call.
      Returns:
      boolean True - An optimizable permutation remains. False - Permutations are exhausted.
      Throws:
      StandardException - Thrown on error
    • getNextDecoratedPermutation

      boolean getNextDecoratedPermutation() throws StandardException
      Iterate through the "decorated permutations", returning false when they are exhausted. NOTE - Implementers are responsible for hiding tree pruning of access methods behind this method call.
      Returns:
      boolean True - An optimizable decorated permutation remains. False - Decorated permutations are exhausted.
      Throws:
      StandardException - Thrown on error
    • costPermutation

      void costPermutation() throws StandardException
      Cost the current permutation. Caller is responsible for pushing all predicates which can be evaluated prior to costing.
      Throws:
      StandardException - Thrown on error
    • costOptimizable

      void costOptimizable(Optimizable optimizable, TableDescriptor td, ConglomerateDescriptor cd, OptimizablePredicateList predList, CostEstimate outerCost) throws StandardException
      Cost the current Optimizable with the specified OPL. Caller is responsible for pushing all predicates which can be evaluated prior to costing.
      Parameters:
      optimizable - The Optimizable
      td - TableDescriptor of the Optimizable
      cd - The ConglomerateDescriptor for the conglom to cost (This should change to an object to represent access paths, but for now this is OK).
      predList - The OptimizablePredicateList to apply
      outerCost - The cost of the tables outer to the one being optimizer - tells how many outer rows there are.
      Throws:
      StandardException - Thrown on error
    • considerCost

      void considerCost(Optimizable optimizable, OptimizablePredicateList predList, CostEstimate estimatedCost, CostEstimate outerCost) throws StandardException
      Consider the cost of the given optimizable. This method is like costOptimizable, above, but it is used when the Optimizable does not need help from the optimizer in costing the Optimizable (in practice, all Optimizables except FromBaseTable use this method. Caller is responsible for pushing all predicates which can be evaluated prior to costing.
      Parameters:
      optimizable - The Optimizable
      predList - The OptimizablePredicateList to apply
      estimatedCost - The estimated cost of the given optimizable
      outerCost - The cost of the tables outer to the one being optimizer - tells how many outer rows there are.
      Throws:
      StandardException - Thrown on error
    • getDataDictionary

      DataDictionary getDataDictionary()
      Return the DataDictionary that the Optimizer is using. This is useful when an Optimizable needs to call optimize() on a child ResultSetNode.
      Returns:
      DataDictionary DataDictionary that the Optimizer is using.
    • modifyAccessPaths

      void modifyAccessPaths() throws StandardException
      Modify the access path for each Optimizable, as necessary. This includes things like adding result sets to translate from index rows to base rows.
      Throws:
      StandardException - Thrown on error
    • getOptimizedCost

      CostEstimate getOptimizedCost()
      Get the estimated cost of the optimized query
    • getFinalCost

      CostEstimate getFinalCost()
      Get the final estimated cost of the optimized query. This should be the cost that corresponds to the best overall join order chosen by the optimizer, and thus this method should only be called after optimization is complete (i.e. when modifying access paths).
    • prepForNextRound

      void prepForNextRound()
      Prepare for another round of optimization. This method is called before every "round" of optimization, where we define a "round" to be the period between the last time a call to getOptimizer() (on either a ResultSetNode or an OptimizerFactory) returned _this_ Optimizer and the time a call to this Optimizer's getNextPermutation() method returns FALSE. Any re-initialization of state that is required before each round should be done in this method.
    • setOuterRows

      void setOuterRows(double outerRowCount)
      Set the estimated number of outer rows - good for optimizing nested optimizables like subqueries and join nodes.
    • getNumberOfJoinStrategies

      int getNumberOfJoinStrategies()
      Get the number of join strategies supported by this optimizer.
    • tableLockThreshold

      int tableLockThreshold()
      Get the maximum number of estimated rows touched in a table before we decide to open the table with table locking (as opposed to row locking.
    • getJoinStrategy

      JoinStrategy getJoinStrategy(int whichStrategy)
      Gets a join strategy by number (zero-based).
    • getJoinStrategy

      JoinStrategy getJoinStrategy(String whichStrategy)
      Gets a join strategy by name. Returns null if not found. The look-up is case-insensitive.
    • getLevel

      int getLevel()
      Get the level of this optimizer.
      Returns:
      The level of this optimizer.
    • uniqueJoinWithOuterTable

      double uniqueJoinWithOuterTable(OptimizablePredicateList predList) throws StandardException
      Tells whether any of the tables outer to the current one has a uniqueness condition on the given predicate list, and if so, how many times each unique key can be seen by the current table.
      Parameters:
      predList - The predicate list to check
      Returns:
      <= 0 means there is no uniqueness condition > 0 means there is a uniqueness condition on an outer table, and the return value is the reciprocal of the maximum number of times the optimizer estimates that each unique key will be returned. For example, 0.5 means the optimizer thinks each distinct join key will be returned at most twice.
      Throws:
      StandardException - Thrown on error
    • useStatistics

      boolean useStatistics()
      If statistics should be considered by the optimizer while optimizing a query. The user may disable the use of statistics by setting the property derby.optimizer.useStatistics or by using the property useStatistics in a query.
      See Also:
    • getMaxMemoryPerTable

      int getMaxMemoryPerTable()
      Returns:
      the maximum number of bytes to be used per table.
    • getOptimizableCount

      int getOptimizableCount()
      Get the number of optimizables being considered by this Optimizer.
    • getOptimizable

      Optimizable getOptimizable(int idx)
      Get the ith (0-based) Optimizable being considered by this Optimizer.
    • updateBestPlanMaps

      void updateBestPlanMaps(short action, Object planKey) throws StandardException
      Process (i.e. add, load, or remove) current best join order as the best one for some outer query or ancestor node, represented by another Optimizer or an instance of FromTable, respectively. Then iterate through our optimizableList and tell each Optimizable to do the same. See Optimizable.updateBestPlan() for more on why this is necessary.
      Parameters:
      action - Indicates whether to add, load, or remove the plan
      planKey - Object to use as the map key when adding/looking up a plan. If this is an instance of Optimizer then it corresponds to an outer query; otherwise it's some Optimizable above this Optimizer that could potentially reject plans chosen by this Optimizer.
      Throws:
      StandardException