Top Description Fields Methods
org.apache.derby.iapi.sql.compile

public Interface Optimizer

Known Direct Implementers
org.apache.derby.impl.sql.compile.OptimizerImpl
Imports
org.apache.derby.iapi.sql.dictionary.DataDictionary, .TableDescriptor, .ConglomerateDescriptor, org.apache.derby.shared.common.error.StandardException, org.apache.derby.iapi.util.JBitSet

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

Modifier and TypeField and Description
public static final String
JOIN_ORDER_OPTIMIZATION

Property name for controlling whether to do join order optimization.

public static final int
MAX_DYNAMIC_MATERIALIZED_ROWS

Maximum size of dynamically created materialized rows.

public 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.

public static final String
MODULE

Module name for the monitor's module locating system.

public 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.

public static final int
NORMAL_PLAN

Indicates a "normal" plan that is not optimized to do sort avoidance

public static final String
RULE_BASED_OPTIMIZATION

Property name for controlling whether to do rule-based optimization, as opposed to cost-based optimization.

public static final int
SORT_AVOIDANCE_PLAN

Indicates a sort-avoidance plan

public static final String
USE_STATISTICS

Property name for disabling statistics use for all queries.

Method Summary

Modifier and TypeMethod and Description
public void
considerCost(Optimizable
The Optimizable
optimizable
,
OptimizablePredicateList
The OptimizablePredicateList to apply
predList
,
CostEstimate
The estimated cost of the given optimizable
estimatedCost
,
CostEstimate
The cost of the tables outer to the one being optimizer - tells how many outer rows there are.
outerCost
)

Consider the cost of the given optimizable.

public void
costOptimizable(Optimizable
The Optimizable
optimizable
,
TableDescriptor
TableDescriptor of the Optimizable
td
,
ConglomerateDescriptor
The ConglomerateDescriptor for the conglom to cost (This should change to an object to represent access paths, but for now this is OK).
cd
,
OptimizablePredicateList
The OptimizablePredicateList to apply
predList
,
CostEstimate
The cost of the tables outer to the one being optimizer - tells how many outer rows there are.
outerCost
)

Cost the current Optimizable with the specified OPL.

public void
costPermutation()

Cost the current permutation.

public DataDictionary

Returns:

DataDictionary DataDictionary that the Optimizer is using.
getDataDictionary
()

Return the DataDictionary that the Optimizer is using.

public CostEstimate
getFinalCost()

Get the final estimated cost of the optimized query.

public JoinStrategy
getJoinStrategy(int whichStrategy)

Gets a join strategy by number (zero-based).

public JoinStrategy
getJoinStrategy(String whichStrategy)

Gets a join strategy by name.

public int

Returns:

The level of this optimizer.
getLevel
()

Get the level of this optimizer.

public int

Returns:

the maximum number of bytes to be used per table.
getMaxMemoryPerTable
()

public boolean

Returns:

boolean True - An optimizable decorated permutation remains. False - Decorated permutations are exhausted.
getNextDecoratedPermutation
()

Iterate through the "decorated permutations", returning false when they are exhausted.

public boolean

Returns:

boolean True - An optimizable permutation remains. False - Permutations are exhausted.
getNextPermutation
()

Iterate through the permutations, returning false when the permutations are exhausted.

public int
getNumberOfJoinStrategies()

Get the number of join strategies supported by this optimizer.

public Optimizable
getOptimizable(int idx)

Get the ith (0-based) Optimizable being considered by this Optimizer.

public int
getOptimizableCount()

Get the number of optimizables being considered by this Optimizer.

public CostEstimate
getOptimizedCost()

Get the estimated cost of the optimized query

public void
modifyAccessPaths()

Modify the access path for each Optimizable, as necessary.

public void
prepForNextRound()

Prepare for another round of optimization.

public void
setOuterRows(double outerRowCount)

Set the estimated number of outer rows - good for optimizing nested optimizables like subqueries and join nodes.

public 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.

public double

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.
uniqueJoinWithOuterTable
(OptimizablePredicateList
The predicate list to check
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.

public void
updateBestPlanMaps(short
Indicates whether to add, load, or remove the plan
action
,
Object
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.
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.

public boolean
useStatistics()

If statistics should be considered by the optimizer while optimizing a query.

Field Detail

JOIN_ORDER_OPTIMIZATIONback to summary
public static final String JOIN_ORDER_OPTIMIZATION

Property name for controlling whether to do join order optimization.

MAX_DYNAMIC_MATERIALIZED_ROWSback to summary
public 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

MAX_MEMORY_PER_TABLEback to summary
public 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).

MODULEback to summary
public static final String MODULE

Module name for the monitor's module locating system.

NO_TIMEOUTback to summary
public 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.

NORMAL_PLANback to summary
public static final int NORMAL_PLAN

Indicates a "normal" plan that is not optimized to do sort avoidance

RULE_BASED_OPTIMIZATIONback to summary
public static final String RULE_BASED_OPTIMIZATION

Property name for controlling whether to do rule-based optimization, as opposed to cost-based optimization.

SORT_AVOIDANCE_PLANback to summary
public static final int SORT_AVOIDANCE_PLAN

Indicates a sort-avoidance plan

USE_STATISTICSback to summary
public static final String USE_STATISTICS

Property name for disabling statistics use for all queries.

Method Detail

considerCostback to summary
public 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:Optimizable

The Optimizable

predList:OptimizablePredicateList

The OptimizablePredicateList to apply

estimatedCost:CostEstimate

The estimated cost of the given optimizable

outerCost:CostEstimate

The cost of the tables outer to the one being optimizer - tells how many outer rows there are.

Exceptions
StandardException:
Thrown on error
costOptimizableback to summary
public 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:Optimizable

The Optimizable

td:TableDescriptor

TableDescriptor of the Optimizable

cd:ConglomerateDescriptor

The ConglomerateDescriptor for the conglom to cost (This should change to an object to represent access paths, but for now this is OK).

predList:OptimizablePredicateList

The OptimizablePredicateList to apply

outerCost:CostEstimate

The cost of the tables outer to the one being optimizer - tells how many outer rows there are.

Exceptions
StandardException:
Thrown on error
costPermutationback to summary
public void costPermutation() throws StandardException

Cost the current permutation. Caller is responsible for pushing all predicates which can be evaluated prior to costing.

Exceptions
StandardException:
Thrown on error
getDataDictionaryback to summary
public 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 DataDictionary that the Optimizer is using.

getFinalCostback to summary
public 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).

getJoinStrategyback to summary
public JoinStrategy getJoinStrategy(int whichStrategy)

Gets a join strategy by number (zero-based).

getJoinStrategyback to summary
public JoinStrategy getJoinStrategy(String whichStrategy)

Gets a join strategy by name. Returns null if not found. The look-up is case-insensitive.

getLevelback to summary
public int getLevel()

Get the level of this optimizer.

Returns:int

The level of this optimizer.

getMaxMemoryPerTableback to summary
public int getMaxMemoryPerTable()
Returns:int

the maximum number of bytes to be used per table.

getNextDecoratedPermutationback to summary
public 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

boolean True - An optimizable decorated permutation remains. False - Decorated permutations are exhausted.

Exceptions
StandardException:
Thrown on error
getNextPermutationback to summary
public 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

boolean True - An optimizable permutation remains. False - Permutations are exhausted.

Exceptions
StandardException:
Thrown on error
getNumberOfJoinStrategiesback to summary
public int getNumberOfJoinStrategies()

Get the number of join strategies supported by this optimizer.

getOptimizableback to summary
public Optimizable getOptimizable(int idx)

Get the ith (0-based) Optimizable being considered by this Optimizer.

getOptimizableCountback to summary
public int getOptimizableCount()

Get the number of optimizables being considered by this Optimizer.

getOptimizedCostback to summary
public CostEstimate getOptimizedCost()

Get the estimated cost of the optimized query

modifyAccessPathsback to summary
public 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.

Exceptions
StandardException:
Thrown on error
prepForNextRoundback to summary
public 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.

setOuterRowsback to summary
public void setOuterRows(double outerRowCount)

Set the estimated number of outer rows - good for optimizing nested optimizables like subqueries and join nodes.

tableLockThresholdback to summary
public 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.

uniqueJoinWithOuterTableback to summary
public 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:OptimizablePredicateList

The predicate list to check

Returns:double

<= 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.

Exceptions
StandardException:
Thrown on error
updateBestPlanMapsback to summary
public 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:short

Indicates whether to add, load, or remove the plan

planKey:Object

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.

useStatisticsback to summary
public 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