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 Summary
Fields Modifier and Type Field Description static java.lang.String
JOIN_ORDER_OPTIMIZATION
Property name for controlling whether to do join order optimization.static int
MAX_DYNAMIC_MATERIALIZED_ROWS
Maximum size of dynamically created materialized rows.static java.lang.String
MAX_MEMORY_PER_TABLE
Property name for controlling the maximum size of memory (in KB) the optimizer can use for each table.static java.lang.String
MODULE
Module name for the monitor's module locating system.static java.lang.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.static int
NORMAL_PLAN
Indicates a "normal" plan that is not optimized to do sort avoidancestatic java.lang.String
RULE_BASED_OPTIMIZATION
Property name for controlling whether to do rule-based optimization, as opposed to cost-based optimization.static int
SORT_AVOIDANCE_PLAN
Indicates a sort-avoidance planstatic java.lang.String
USE_STATISTICS
Property name for disabling statistics use for all queries.
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
considerCost(Optimizable optimizable, OptimizablePredicateList predList, CostEstimate estimatedCost, CostEstimate outerCost)
Consider the cost of the given optimizable.void
costOptimizable(Optimizable optimizable, TableDescriptor td, ConglomerateDescriptor cd, OptimizablePredicateList predList, CostEstimate outerCost)
Cost the current Optimizable with the specified OPL.void
costPermutation()
Cost the current permutation.DataDictionary
getDataDictionary()
Return the DataDictionary that the Optimizer is using.CostEstimate
getFinalCost()
Get the final estimated cost of the optimized query.JoinStrategy
getJoinStrategy(int whichStrategy)
Gets a join strategy by number (zero-based).JoinStrategy
getJoinStrategy(java.lang.String whichStrategy)
Gets a join strategy by name.int
getLevel()
Get the level of this optimizer.int
getMaxMemoryPerTable()
boolean
getNextDecoratedPermutation()
Iterate through the "decorated permutations", returning false when they are exhausted.boolean
getNextPermutation()
Iterate through the permutations, returning false when the permutations are exhausted.int
getNumberOfJoinStrategies()
Get the number of join strategies supported by this optimizer.Optimizable
getOptimizable(int idx)
Get the ith (0-based) Optimizable being considered by this Optimizer.int
getOptimizableCount()
Get the number of optimizables being considered by this Optimizer.CostEstimate
getOptimizedCost()
Get the estimated cost of the optimized queryvoid
modifyAccessPaths()
Modify the access path for each Optimizable, as necessary.void
prepForNextRound()
Prepare for another round of optimization.void
setOuterRows(double outerRowCount)
Set the estimated number of outer rows - good for optimizing nested optimizables like subqueries and join nodes.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.double
uniqueJoinWithOuterTable(OptimizablePredicateList predList)
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.void
updateBestPlanMaps(short action, java.lang.Object planKey)
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.boolean
useStatistics()
If statistics should be considered by the optimizer while optimizing a query.
-
-
-
Field Detail
-
MODULE
static final java.lang.String MODULE
Module name for the monitor's module locating system.- See Also:
- Constant Field Values
-
JOIN_ORDER_OPTIMIZATION
static final java.lang.String JOIN_ORDER_OPTIMIZATION
Property name for controlling whether to do join order optimization.- See Also:
- Constant Field Values
-
RULE_BASED_OPTIMIZATION
static final java.lang.String RULE_BASED_OPTIMIZATION
Property name for controlling whether to do rule-based optimization, as opposed to cost-based optimization.- See Also:
- Constant Field Values
-
NO_TIMEOUT
static final java.lang.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:
- Constant Field Values
-
MAX_MEMORY_PER_TABLE
static final java.lang.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:
- Constant Field Values
-
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:
- Constant Field Values
-
USE_STATISTICS
static final java.lang.String USE_STATISTICS
Property name for disabling statistics use for all queries.- See Also:
- Constant Field Values
-
NORMAL_PLAN
static final int NORMAL_PLAN
Indicates a "normal" plan that is not optimized to do sort avoidance- See Also:
- Constant Field Values
-
SORT_AVOIDANCE_PLAN
static final int SORT_AVOIDANCE_PLAN
Indicates a sort-avoidance plan- See Also:
- Constant Field Values
-
-
Method Detail
-
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 Optimizabletd
- TableDescriptor of the Optimizablecd
- 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 applyouterCost
- 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 OptimizablepredList
- The OptimizablePredicateList to applyestimatedCost
- The estimated cost of the given optimizableouterCost
- 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(java.lang.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:
USE_STATISTICS
-
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, java.lang.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 planplanKey
- 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
-
-