Top Description Fields Constructors Methods
org.apache.derby.impl.sql.compile

pack-priv Class OptimizerImpl

extends Object
implements Optimizer
Class Inheritance
All Implemented Interfaces
org.apache.derby.iapi.sql.compile.Optimizer
Imports
java.util.HashMap, org.apache.derby.shared.common.error.StandardException, org.apache.derby.shared.common.reference.SQLState, org.apache.derby.iapi.services.context.ContextManager, org.apache.derby.shared.common.util.ArrayUtil, org.apache.derby.shared.common.sanity.SanityManager, org.apache.derby.iapi.sql.compile.AccessPath, .CostEstimate, .JoinStrategy, .OptTrace, .Optimizable, .OptimizableList, .OptimizablePredicate, .OptimizablePredicateList, .Optimizer, .OptimizerPlan, .RequiredRowOrdering, .RowOrdering, org.apache.derby.iapi.sql.conn.LanguageConnectionContext, org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor, .DataDictionary, .TableDescriptor, .UniqueTupleDescriptor, org.apache.derby.iapi.util.JBitSet, .StringUtil

Optimizer uses OptimizableList to keep track of the best join order as it builds it. For each available slot in the join order, we cost all of the Optimizables from that slot til the end of the OptimizableList. Later, we will choose the best Optimizable for that slot and reorder the list accordingly. In order to do this, we probably need to move the temporary pushing and pulling of join clauses into Optimizer, since the logic will be different for other implementations. (Of course, we're not pushing and pulling join clauses between permutations yet.)

Field Summary

Modifier and TypeField and Description
private JBitSet
private CostEstimateImpl
private int[]
private boolean
private RowOrdering
private CostEstimateImpl
private OptimizerPlan
private RowOrdering
private CostEstimateImpl
private long
private DataDictionary
private boolean
private CostEstimate
private int[]
private boolean
private int
private JoinStrategy[]
private static final int
private LanguageConnectionContext
private int
private static final int
private JBitSet
private boolean
private int
private int
private OptimizableList
private CostEstimateImpl
private OptimizerPlan
private int
private OptimizablePredicateList
private int[]
private static final int
private boolean
private RequiredRowOrdering
private boolean
private HashMap<Object, int[]>
private CostEstimate
private int
private boolean
private double
private long
private boolean
private boolean
private static final int
private static final int

Constructor Summary

AccessConstructor and Description
pack-priv
OptimizerImpl(OptimizableList optimizableList, OptimizablePredicateList predicateList, DataDictionary dDictionary, boolean ruleBasedOptimization, boolean noTimeout, boolean useStatistics, int maxMemoryPerTable, JoinStrategy[] joinStrategies, int tableLockThreshold, RequiredRowOrdering requiredRowOrdering, int numTablesInQuery, OptimizerPlan overridingPlan, LanguageConnectionContext lcc)

Method Summary

Modifier and TypeMethod and Description
pack-priv void
addScopedPredicatesToList(PredicateList
List of predicates to add to this OptimizerImpl's own list for pushing.
pList
,
ContextManager cm)

Add scoped predicates to this optimizer's predicateList.

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
)

Implements org.apache.derby.iapi.sql.compile.Optimizer.considerCost.

This is the version of costOptimizable for non-base-tables.

private void
costBasedCostOptimizable(Optimizable optimizable, TableDescriptor td, ConglomerateDescriptor cd, OptimizablePredicateList predList, CostEstimate outerCost)

This method decides whether the given conglomerate descriptor is cheapest based on cost, rather than based on rules.

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
)

Implements org.apache.derby.iapi.sql.compile.Optimizer.costOptimizable.

Cost the current Optimizable with the specified OPL.

public void
private void
endOfRoundCleanup()

Do any work that needs to be done after the current round of optimization has completed.

private CostEstimate
estimateTotalCost(OptimizablePredicateList predList, ConglomerateDescriptor cd, CostEstimate outerCost, Optimizable optimizable)

Estimate the total cost of doing a join with the given optimizable.

public DataDictionary
getDataDictionary()

Implements org.apache.derby.iapi.sql.compile.Optimizer.getDataDictionary.

Return the DataDictionary that the Optimizer is using.

public CostEstimate
getFinalCost()

Implements org.apache.derby.iapi.sql.compile.Optimizer.getFinalCost.

Get the final estimated cost of the optimized query.

public JoinStrategy
getJoinStrategy(int whichStrategy)

Implements org.apache.derby.iapi.sql.compile.Optimizer.getJoinStrategy.

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

public JoinStrategy
getJoinStrategy(String whichStrategy)

Implements org.apache.derby.iapi.sql.compile.Optimizer.getJoinStrategy.

Gets a join strategy by name.

public int
getLevel()

Implements org.apache.derby.iapi.sql.compile.Optimizer.getLevel.

Get the level of this optimizer.

public int
pack-priv CostEstimateImpl
getNewCostEstimate(double theCost, double theRowCount, double theSingleScanRowCount)

public boolean
getNextDecoratedPermutation()

Implements org.apache.derby.iapi.sql.compile.Optimizer.getNextDecoratedPermutation.

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

public boolean
getNextPermutation()

Implements org.apache.derby.iapi.sql.compile.Optimizer.getNextPermutation.

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

public int
getNumberOfJoinStrategies()

Implements org.apache.derby.iapi.sql.compile.Optimizer.getNumberOfJoinStrategies.

Get the number of join strategies supported by this optimizer.

public Optimizable
getOptimizable(int idx)

Implements org.apache.derby.iapi.sql.compile.Optimizer.getOptimizable.

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

public int
getOptimizableCount()

Implements org.apache.derby.iapi.sql.compile.Optimizer.getOptimizableCount.

Get the number of optimizables being considered by this Optimizer.

public CostEstimate
getOptimizedCost()

Implements org.apache.derby.iapi.sql.compile.Optimizer.getOptimizedCost.

Get the estimated cost of the optimized query

private UniqueTupleDescriptor
getTupleDescriptor(Optimizable optimizable)

Get the unique tuple descriptor of the current access path for an Optimizable.

private int
initJumpState()

Determine if we want to try "jumping" permutations with this OptimizerImpl, and (re-)initialize the permuteState field accordingly.

private boolean
pack-priv static boolean
isTableFunction(Optimizable optimizable)

Return true if the optimizable is a table function

private boolean
joinOrderMeetsDependencies(int optNumber)

Check to see if the optimizable corresponding to the received optNumber can legally be placed within the current join order.

public void
modifyAccessPaths()

Implements org.apache.derby.iapi.sql.compile.Optimizer.modifyAccessPaths.

Modify the access path for each Optimizable, as necessary.

private CostEstimate
public void
prepForNextRound()

Implements org.apache.derby.iapi.sql.compile.Optimizer.prepForNextRound.

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_ OptimizerImpl and the time a call to this OptimizerImpl's getNextPermutation() method returns FALSE.

private void
pullOptimizableFromJoinOrder()

Pull whatever optimizable is at joinPosition in the proposed join order from the join order, and update all corresponding state accordingly.

pack-priv void
pushPredicates(Optimizable curTable, JBitSet outerTables)

private double
recoverCostFromProposedJoinOrder(boolean sortAvoidance)

Iterate through all optimizables in the current proposedJoinOrder and find the accumulated sum of their estimated costs.

private void
rememberBestCost(CostEstimate currentCost, int planType)

Is the cost of this join order lower than the best one we've found so far?

private void
private void
ruleBasedCostOptimizable(Optimizable optimizable, TableDescriptor td, ConglomerateDescriptor cd, OptimizablePredicateList predList, CostEstimate outerCost)

This method decides whether the given conglomerate descriptor is cheapest based on rules, rather than based on cost estimates.

public void
setOuterRows(double outerRows)

Implements org.apache.derby.iapi.sql.compile.Optimizer.setOuterRows.

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

public int
tableLockThreshold()

Implements org.apache.derby.iapi.sql.compile.Optimizer.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.

private OptTrace
tracer()

Get the trace machinery

private boolean
public double
uniqueJoinWithOuterTable(OptimizablePredicateList
The predicate list to check
predList
)

Implements org.apache.derby.iapi.sql.compile.Optimizer.uniqueJoinWithOuterTable.

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 OptimizerImpl then it corresponds to an outer query; otherwise it's some Optimizable above this OptimizerImpl that could potentially reject plans chosen by this OptimizerImpl.
planKey
)

Implements org.apache.derby.iapi.sql.compile.Optimizer.updateBestPlanMaps.

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 OptimizerImpl or an instance of FromTable, respectively.

public boolean
useStatistics()

Implements org.apache.derby.iapi.sql.compile.Optimizer.useStatistics.

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

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

assignedTableMapback to summary
private JBitSet assignedTableMap
bestCostback to summary
private CostEstimateImpl bestCost
bestJoinOrderback to summary
private int[] bestJoinOrder
bestJoinOrderUsedPredsFromAboveback to summary
private boolean bestJoinOrderUsedPredsFromAbove
bestRowOrderingback to summary
private RowOrdering bestRowOrdering
currentCostback to summary
private CostEstimateImpl currentCost
currentPlanback to summary
private OptimizerPlan currentPlan
currentRowOrderingback to summary
private RowOrdering currentRowOrdering
currentSortAvoidanceCostback to summary
private CostEstimateImpl currentSortAvoidanceCost
currentTimeback to summary
private long currentTime
dDictionaryback to summary
private DataDictionary dDictionary
desiredJoinOrderFoundback to summary
private boolean desiredJoinOrderFound
finalCostEstimateback to summary
private CostEstimate finalCostEstimate
firstLookOrderback to summary
private int[] firstLookOrder
foundABestPlanback to summary
private boolean foundABestPlan
joinPositionback to summary
private int joinPosition
joinStrategiesback to summary
private JoinStrategy[] joinStrategies
JUMPINGback to summary
private static final int JUMPING
lccback to summary
private LanguageConnectionContext lcc
maxMemoryPerTableback to summary
private int maxMemoryPerTable
NO_JUMPback to summary
private static final int NO_JUMP
nonCorrelatedTableMapback to summary
private JBitSet nonCorrelatedTableMap
noTimeoutback to summary
private boolean noTimeout
numOptimizablesback to summary
private int numOptimizables
numTablesInQueryback to summary
private int numTablesInQuery
optimizableListback to summary
private OptimizableList optimizableList
outermostCostEstimateback to summary
private CostEstimateImpl outermostCostEstimate
overridingPlanback to summary
private OptimizerPlan overridingPlan
permuteStateback to summary
private int permuteState
predicateListback to summary
private OptimizablePredicateList predicateList
proposedJoinOrderback to summary
private int[] proposedJoinOrder
READY_TO_JUMPback to summary
private static final int READY_TO_JUMP
reloadBestPlanback to summary
private boolean reloadBestPlan
requiredRowOrderingback to summary
private RequiredRowOrdering requiredRowOrdering
ruleBasedOptimizationback to summary
private boolean ruleBasedOptimization
savedJoinOrdersback to summary
private HashMap<Object, int[]> savedJoinOrders
sortCostback to summary
private CostEstimate sortCost
tableLockThresholdback to summary
private int tableLockThreshold
timeExceededback to summary
private boolean timeExceeded
timeLimitback to summary
private double timeLimit
timeOptimizationStartedback to summary
private long timeOptimizationStarted
useStatisticsback to summary
private boolean useStatistics
usingPredsPushedFromAboveback to summary
private boolean usingPredsPushedFromAbove
WALK_HIGHback to summary
private static final int WALK_HIGH
WALK_LOWback to summary
private static final int WALK_LOW

Constructor Detail

OptimizerImplback to summary
pack-priv OptimizerImpl(OptimizableList optimizableList, OptimizablePredicateList predicateList, DataDictionary dDictionary, boolean ruleBasedOptimization, boolean noTimeout, boolean useStatistics, int maxMemoryPerTable, JoinStrategy[] joinStrategies, int tableLockThreshold, RequiredRowOrdering requiredRowOrdering, int numTablesInQuery, OptimizerPlan overridingPlan, LanguageConnectionContext lcc) throws StandardException

Method Detail

addScopedPredicatesToListback to summary
pack-priv void addScopedPredicatesToList(PredicateList pList, ContextManager cm) throws StandardException

Add scoped predicates to this optimizer's predicateList. This method is intended for use during the modifyAccessPath() phase of compilation, as it allows nodes (esp. SelectNodes) to add to the list of predicates available for the final "push" before code generation. Just as the constructor for this class allows a caller to specify a predicate list to use during the optimization phase, this method allows a caller to specify a predicate list to use during the modify-access-paths phase. Before adding the received predicates, this method also clears out any scoped predicates that might be sitting in OptimizerImpl's list from the last round of optimizing. This method should be in the Optimizer interface, but it relies on an argument type (PredicateList) which lives in an impl package.

Parameters
pList:PredicateList

List of predicates to add to this OptimizerImpl's own list for pushing.

considerCostback to summary
public void considerCost(Optimizable optimizable, OptimizablePredicateList predList, CostEstimate estimatedCost, CostEstimate outerCost) throws StandardException

Implements org.apache.derby.iapi.sql.compile.Optimizer.considerCost.

This is the version of costOptimizable for non-base-tables.

Parameters
optimizable:Optimizable

Doc from org.apache.derby.iapi.sql.compile.Optimizer.considerCost.

The Optimizable

predList:OptimizablePredicateList

Doc from org.apache.derby.iapi.sql.compile.Optimizer.considerCost.

The OptimizablePredicateList to apply

estimatedCost:CostEstimate

Doc from org.apache.derby.iapi.sql.compile.Optimizer.considerCost.

The estimated cost of the given optimizable

outerCost:CostEstimate

Doc from org.apache.derby.iapi.sql.compile.Optimizer.considerCost.

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

Exceptions
StandardException:
Thrown on error
See Also
Optimizer#considerCost
costBasedCostOptimizableback to summary
private void costBasedCostOptimizable(Optimizable optimizable, TableDescriptor td, ConglomerateDescriptor cd, OptimizablePredicateList predList, CostEstimate outerCost) throws StandardException

This method decides whether the given conglomerate descriptor is cheapest based on cost, rather than based on rules. It compares the cost of using the given ConglomerateDescriptor with the cost of using the best ConglomerateDescriptor so far.

costOptimizableback to summary
public void costOptimizable(Optimizable optimizable, TableDescriptor td, ConglomerateDescriptor cd, OptimizablePredicateList predList, CostEstimate outerCost) throws StandardException

Implements org.apache.derby.iapi.sql.compile.Optimizer.costOptimizable.

Doc from org.apache.derby.iapi.sql.compile.Optimizer.costOptimizable.

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
See Also
org.apache.derby.iapi.sql.compile.Optimizer#costOptimizable
costPermutationback to summary
public void costPermutation() throws StandardException

Implements org.apache.derby.iapi.sql.compile.Optimizer.costPermutation.

Doc from org.apache.derby.iapi.sql.compile.Optimizer.costPermutation.

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

Exceptions
StandardException:
Thrown on error
See Also
org.apache.derby.iapi.sql.compile.Optimizer#costPermutation
endOfRoundCleanupback to summary
private void endOfRoundCleanup() throws StandardException

Do any work that needs to be done after the current round of optimization has completed. For now this just means walking the subtrees for each optimizable and removing the "bestPlan" that we saved (w.r.t to this OptimizerImpl) from all of the nodes. If we don't do this post-optimization cleanup we can end up consuming a huge amount of memory for deeply- nested queries, which can lead to OOM errors. DERBY-1315.

estimateTotalCostback to summary
private CostEstimate estimateTotalCost(OptimizablePredicateList predList, ConglomerateDescriptor cd, CostEstimate outerCost, Optimizable optimizable) throws StandardException

Estimate the total cost of doing a join with the given optimizable.

Exceptions
StandardException:
Thrown on error
getDataDictionaryback to summary
public DataDictionary getDataDictionary()

Implements org.apache.derby.iapi.sql.compile.Optimizer.getDataDictionary.

Doc from org.apache.derby.iapi.sql.compile.Optimizer.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.

See Also
org.apache.derby.iapi.sql.compile.Optimizer#getDataDictionary
getFinalCostback to summary
public CostEstimate getFinalCost()

Implements org.apache.derby.iapi.sql.compile.Optimizer.getFinalCost.

Doc from org.apache.derby.iapi.sql.compile.Optimizer.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).

See Also
Sum up the cost of all of the trulyTheBestAccessPaths for the Optimizables in our list. Assumption is that we only get here after optimization has completed--i.e. while modifying access paths.
getJoinStrategyback to summary
public JoinStrategy getJoinStrategy(int whichStrategy)

Implements org.apache.derby.iapi.sql.compile.Optimizer.getJoinStrategy.

Doc from org.apache.derby.iapi.sql.compile.Optimizer.getJoinStrategy.

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

See Also
Optimizer#getJoinStrategy
getJoinStrategyback to summary
public JoinStrategy getJoinStrategy(String whichStrategy)

Implements org.apache.derby.iapi.sql.compile.Optimizer.getJoinStrategy.

Doc from org.apache.derby.iapi.sql.compile.Optimizer.getJoinStrategy.

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

See Also
Optimizer#getJoinStrategy
getLevelback to summary
public int getLevel()

Implements org.apache.derby.iapi.sql.compile.Optimizer.getLevel.

Doc from org.apache.derby.iapi.sql.compile.Optimizer.getLevel.

Get the level of this optimizer.

Returns:int

The level of this optimizer.

See Also
Optimizer#getLevel
getMaxMemoryPerTableback to summary
public int getMaxMemoryPerTable()

Implements org.apache.derby.iapi.sql.compile.Optimizer.getMaxMemoryPerTable.

Returns:int

Doc from org.apache.derby.iapi.sql.compile.Optimizer.getMaxMemoryPerTable.

the maximum number of bytes to be used per table.

getNewCostEstimateback to summary
pack-priv CostEstimateImpl getNewCostEstimate(double theCost, double theRowCount, double theSingleScanRowCount)
getNextDecoratedPermutationback to summary
public boolean getNextDecoratedPermutation() throws StandardException

Implements org.apache.derby.iapi.sql.compile.Optimizer.getNextDecoratedPermutation.

Doc from org.apache.derby.iapi.sql.compile.Optimizer.getNextDecoratedPermutation.

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
See Also
Optimizer#getNextDecoratedPermutation
getNextPermutationback to summary
public boolean getNextPermutation() throws StandardException

Implements org.apache.derby.iapi.sql.compile.Optimizer.getNextPermutation.

Doc from org.apache.derby.iapi.sql.compile.Optimizer.getNextPermutation.

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
See Also
Optimizer#getNextPermutation
getNumberOfJoinStrategiesback to summary
public int getNumberOfJoinStrategies()

Implements org.apache.derby.iapi.sql.compile.Optimizer.getNumberOfJoinStrategies.

Get the number of join strategies supported by this optimizer.

getOptimizableback to summary
public Optimizable getOptimizable(int idx)

Implements org.apache.derby.iapi.sql.compile.Optimizer.getOptimizable.

Doc from org.apache.derby.iapi.sql.compile.Optimizer.getOptimizable.

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

getOptimizableCountback to summary
public int getOptimizableCount()

Implements org.apache.derby.iapi.sql.compile.Optimizer.getOptimizableCount.

Doc from org.apache.derby.iapi.sql.compile.Optimizer.getOptimizableCount.

Get the number of optimizables being considered by this Optimizer.

getOptimizedCostback to summary
public CostEstimate getOptimizedCost()

Implements org.apache.derby.iapi.sql.compile.Optimizer.getOptimizedCost.

Doc from org.apache.derby.iapi.sql.compile.Optimizer.getOptimizedCost.

Get the estimated cost of the optimized query

See Also
Optimizer#getOptimizedCost
getTupleDescriptorback to summary
private UniqueTupleDescriptor getTupleDescriptor(Optimizable optimizable) throws StandardException

Get the unique tuple descriptor of the current access path for an Optimizable.

initJumpStateback to summary
private int initJumpState()

Determine if we want to try "jumping" permutations with this OptimizerImpl, and (re-)initialize the permuteState field accordingly.

isPushableback to summary
private boolean isPushable(OptimizablePredicate pred)
isTableFunctionback to summary
pack-priv static boolean isTableFunction(Optimizable optimizable)

Return true if the optimizable is a table function

joinOrderMeetsDependenciesback to summary
private boolean joinOrderMeetsDependencies(int optNumber) throws StandardException

Check to see if the optimizable corresponding to the received optNumber can legally be placed within the current join order. More specifically, if the optimizable has any dependencies, check to see if those dependencies are satisified by the table map representing the current join order.

modifyAccessPathsback to summary
public void modifyAccessPaths() throws StandardException

Implements org.apache.derby.iapi.sql.compile.Optimizer.modifyAccessPaths.

Doc from org.apache.derby.iapi.sql.compile.Optimizer.modifyAccessPaths.

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
See Also
Optimizer#modifyAccessPaths
newCostEstimateback to summary
private CostEstimate newCostEstimate()
prepForNextRoundback to summary
public void prepForNextRound()

Implements org.apache.derby.iapi.sql.compile.Optimizer.prepForNextRound.

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_ OptimizerImpl and the time a call to this OptimizerImpl's getNextPermutation() method returns FALSE. Any re-initialization of state that is required before each round should be done in this method.

pullOptimizableFromJoinOrderback to summary
private void pullOptimizableFromJoinOrder() throws StandardException

Pull whatever optimizable is at joinPosition in the proposed join order from the join order, and update all corresponding state accordingly.

pushPredicatesback to summary
pack-priv void pushPredicates(Optimizable curTable, JBitSet outerTables) throws StandardException
recoverCostFromProposedJoinOrderback to summary
private double recoverCostFromProposedJoinOrder(boolean sortAvoidance) throws StandardException

Iterate through all optimizables in the current proposedJoinOrder and find the accumulated sum of their estimated costs. This method is used to 'recover' cost estimate sums that have been lost due to the addition/subtraction of the cost estimate for the Optimizable at position "joinPosition". Ex. If the total cost for Optimizables at positions < joinPosition is 1500, and then the Optimizable at joinPosition has an estimated cost of 3.14E40, adding those two numbers effectively "loses" the 1500. When we later subtract 3.14E40 from the total cost estimate (as part of "pull" processing), we'll end up with 0 as the result--which is wrong. This method allows us to recover the "1500" that we lost in the process of adding and subtracting 3.14E40.

rememberBestCostback to summary
private void rememberBestCost(CostEstimate currentCost, int planType) throws StandardException

Is the cost of this join order lower than the best one we've found so far? If so, remember it.

Note

If the user has specified a join order, it will be the only join order the optimizer considers, so it is OK to use costing to decide that it is the "best" join order.

Exceptions
StandardException:
Thrown on error
rewindJoinOrderback to summary
private void rewindJoinOrder() throws StandardException
ruleBasedCostOptimizableback to summary
private void ruleBasedCostOptimizable(Optimizable optimizable, TableDescriptor td, ConglomerateDescriptor cd, OptimizablePredicateList predList, CostEstimate outerCost) throws StandardException

This method decides whether the given conglomerate descriptor is cheapest based on rules, rather than based on cost estimates. The rules are: Covering matching indexes are preferred above all Non-covering matching indexes are next in order of preference Covering non-matching indexes are next in order of preference Heap scans are next in order of preference Non-covering, non-matching indexes are last in order of preference. In the current language architecture, there will always be a heap, so a non-covering, non-matching index scan will never be chosen. However, the optimizer may see a non-covering, non-matching index first, in which case it will choose it temporarily as the best conglomerate seen so far.

Note

This method sets the cost in the optimizable, even though it doesn't use the cost to determine which access path to choose. There are two reasons for this: the cost might be needed to determine join order, and the cost information is copied to the query plan.

setOuterRowsback to summary
public void setOuterRows(double outerRows)

Implements org.apache.derby.iapi.sql.compile.Optimizer.setOuterRows.

Doc from org.apache.derby.iapi.sql.compile.Optimizer.setOuterRows.

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

See Also
Optimizer#setOuterRows
tableLockThresholdback to summary
public int tableLockThreshold()

Implements org.apache.derby.iapi.sql.compile.Optimizer.tableLockThreshold.

Doc from org.apache.derby.iapi.sql.compile.Optimizer.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.

See Also
Optimizer#tableLockThreshold
tracerback to summary
private OptTrace tracer()

Get the trace machinery

tracingIsOnback to summary
private boolean tracingIsOn()
uniqueJoinWithOuterTableback to summary
public double uniqueJoinWithOuterTable(OptimizablePredicateList predList) throws StandardException

Implements org.apache.derby.iapi.sql.compile.Optimizer.uniqueJoinWithOuterTable.

Doc from org.apache.derby.iapi.sql.compile.Optimizer.uniqueJoinWithOuterTable.

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
See Also
Optimizer#uniqueJoinWithOuterTable
updateBestPlanMapsback to summary
public void updateBestPlanMaps(short action, Object planKey) throws StandardException

Implements org.apache.derby.iapi.sql.compile.Optimizer.updateBestPlanMaps.

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 OptimizerImpl 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 OptimizerImpl then it corresponds to an outer query; otherwise it's some Optimizable above this OptimizerImpl that could potentially reject plans chosen by this OptimizerImpl.

useStatisticsback to summary
public boolean useStatistics()

Implements org.apache.derby.iapi.sql.compile.Optimizer.useStatistics.

Doc from org.apache.derby.iapi.sql.compile.Optimizer.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
Optimizer#useStatistics