class OptimizerImpl extends java.lang.Object implements Optimizer
JOIN_ORDER_OPTIMIZATION, MAX_DYNAMIC_MATERIALIZED_ROWS, MAX_MEMORY_PER_TABLE, MODULE, NO_TIMEOUT, NORMAL_PLAN, RULE_BASED_OPTIMIZATION, SORT_AVOIDANCE_PLAN, USE_STATISTICS
Constructor and Description |
---|
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) |
Modifier and Type | Method and Description |
---|---|
(package private) void |
addScopedPredicatesToList(PredicateList pList,
ContextManager cm)
Add scoped predicates to this optimizer's predicateList.
|
void |
considerCost(Optimizable optimizable,
OptimizablePredicateList predList,
CostEstimate estimatedCost,
CostEstimate outerCost)
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.
|
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.
|
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.
|
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() |
(package private) CostEstimateImpl |
getNewCostEstimate(double theCost,
double theRowCount,
double theSingleScanRowCount) |
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 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 |
isPushable(OptimizablePredicate pred) |
(package private) 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.
|
void |
modifyAccessPaths()
Modify the access path for each Optimizable, as necessary.
|
private CostEstimate |
newCostEstimate() |
void |
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.
|
(package private) 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 |
rewindJoinOrder() |
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.
|
void |
setOuterRows(double outerRows)
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.
|
private OptTrace |
tracer()
Get the trace machinery
|
private boolean |
tracingIsOn() |
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
OptimizerImpl or an instance of FromTable, respectively.
|
boolean |
useStatistics()
If statistics should be considered by the optimizer while optimizing
a query.
|
private LanguageConnectionContext lcc
private DataDictionary dDictionary
private int numTablesInQuery
private int numOptimizables
private JBitSet assignedTableMap
private OptimizableList optimizableList
private OptimizerPlan overridingPlan
private OptimizerPlan currentPlan
private OptimizablePredicateList predicateList
private JBitSet nonCorrelatedTableMap
private int[] proposedJoinOrder
private int[] bestJoinOrder
private int joinPosition
private boolean desiredJoinOrderFound
private static final int NO_JUMP
private static final int READY_TO_JUMP
private static final int JUMPING
private static final int WALK_HIGH
private static final int WALK_LOW
private int permuteState
private int[] firstLookOrder
private boolean ruleBasedOptimization
private CostEstimateImpl outermostCostEstimate
private CostEstimateImpl currentCost
private CostEstimateImpl currentSortAvoidanceCost
private CostEstimateImpl bestCost
private long timeOptimizationStarted
private long currentTime
private boolean timeExceeded
private boolean noTimeout
private boolean useStatistics
private int tableLockThreshold
private JoinStrategy[] joinStrategies
private RequiredRowOrdering requiredRowOrdering
private boolean foundABestPlan
private CostEstimate sortCost
private RowOrdering currentRowOrdering
private RowOrdering bestRowOrdering
private int maxMemoryPerTable
private boolean reloadBestPlan
private java.util.HashMap<java.lang.Object,int[]> savedJoinOrders
private double timeLimit
private CostEstimate finalCostEstimate
private boolean usingPredsPushedFromAbove
private boolean bestJoinOrderUsedPredsFromAbove
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
StandardException
public void prepForNextRound()
prepForNextRound
in interface Optimizer
private int initJumpState()
private boolean tracingIsOn()
public int getMaxMemoryPerTable()
getMaxMemoryPerTable
in interface Optimizer
public boolean getNextPermutation() throws StandardException
Optimizer
getNextPermutation
in interface Optimizer
StandardException
- Thrown on errorOptimizer.getNextPermutation()
private void rewindJoinOrder() throws StandardException
StandardException
private void endOfRoundCleanup() throws StandardException
StandardException
private double recoverCostFromProposedJoinOrder(boolean sortAvoidance) throws StandardException
StandardException
private boolean joinOrderMeetsDependencies(int optNumber) throws StandardException
StandardException
private void pullOptimizableFromJoinOrder() throws StandardException
StandardException
void pushPredicates(Optimizable curTable, JBitSet outerTables) throws StandardException
StandardException
public boolean getNextDecoratedPermutation() throws StandardException
Optimizer
getNextDecoratedPermutation
in interface Optimizer
StandardException
- Thrown on errorOptimizer.getNextDecoratedPermutation()
private UniqueTupleDescriptor getTupleDescriptor(Optimizable optimizable) throws StandardException
StandardException
static boolean isTableFunction(Optimizable optimizable)
private void rememberBestCost(CostEstimate currentCost, int planType) throws StandardException
StandardException
- Thrown on errorpublic void costPermutation() throws StandardException
Optimizer
costPermutation
in interface Optimizer
StandardException
- Thrown on errorOptimizer.costPermutation()
public void costOptimizable(Optimizable optimizable, TableDescriptor td, ConglomerateDescriptor cd, OptimizablePredicateList predList, CostEstimate outerCost) throws StandardException
Optimizer
costOptimizable
in interface Optimizer
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.StandardException
- Thrown on errorOptimizer.costOptimizable(org.apache.derby.iapi.sql.compile.Optimizable, org.apache.derby.iapi.sql.dictionary.TableDescriptor, org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor, org.apache.derby.iapi.sql.compile.OptimizablePredicateList, org.apache.derby.iapi.sql.compile.CostEstimate)
private void ruleBasedCostOptimizable(Optimizable optimizable, TableDescriptor td, ConglomerateDescriptor cd, OptimizablePredicateList predList, CostEstimate outerCost) throws StandardException
StandardException
private void costBasedCostOptimizable(Optimizable optimizable, TableDescriptor td, ConglomerateDescriptor cd, OptimizablePredicateList predList, CostEstimate outerCost) throws StandardException
StandardException
public void considerCost(Optimizable optimizable, OptimizablePredicateList predList, CostEstimate estimatedCost, CostEstimate outerCost) throws StandardException
considerCost
in interface Optimizer
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.StandardException
- Thrown on errorOptimizer.considerCost(org.apache.derby.iapi.sql.compile.Optimizable, org.apache.derby.iapi.sql.compile.OptimizablePredicateList, org.apache.derby.iapi.sql.compile.CostEstimate, org.apache.derby.iapi.sql.compile.CostEstimate)
public DataDictionary getDataDictionary()
Optimizer
getDataDictionary
in interface Optimizer
Optimizer.getDataDictionary()
public void modifyAccessPaths() throws StandardException
Optimizer
modifyAccessPaths
in interface Optimizer
StandardException
- Thrown on errorOptimizer.modifyAccessPaths()
private CostEstimate newCostEstimate()
public CostEstimate getOptimizedCost()
Optimizer
getOptimizedCost
in interface Optimizer
Optimizer.getOptimizedCost()
public CostEstimate getFinalCost()
Optimizer
public void setOuterRows(double outerRows)
Optimizer
setOuterRows
in interface Optimizer
Optimizer.setOuterRows(double)
public int tableLockThreshold()
Optimizer
tableLockThreshold
in interface Optimizer
Optimizer.tableLockThreshold()
public int getNumberOfJoinStrategies()
getNumberOfJoinStrategies
in interface Optimizer
public JoinStrategy getJoinStrategy(int whichStrategy)
Optimizer
getJoinStrategy
in interface Optimizer
Optimizer.getJoinStrategy(int)
public JoinStrategy getJoinStrategy(java.lang.String whichStrategy)
Optimizer
getJoinStrategy
in interface Optimizer
Optimizer.getJoinStrategy(int)
public double uniqueJoinWithOuterTable(OptimizablePredicateList predList) throws StandardException
Optimizer
uniqueJoinWithOuterTable
in interface Optimizer
predList
- The predicate list to checkStandardException
- Thrown on errorOptimizer.uniqueJoinWithOuterTable(org.apache.derby.iapi.sql.compile.OptimizablePredicateList)
private boolean isPushable(OptimizablePredicate pred)
private CostEstimate estimateTotalCost(OptimizablePredicateList predList, ConglomerateDescriptor cd, CostEstimate outerCost, Optimizable optimizable) throws StandardException
StandardException
- Thrown on errorpublic int getLevel()
Optimizer
getLevel
in interface Optimizer
Optimizer.getLevel()
CostEstimateImpl getNewCostEstimate(double theCost, double theRowCount, double theSingleScanRowCount)
public boolean useStatistics()
Optimizer
useStatistics
in interface Optimizer
Optimizer.useStatistics()
public void updateBestPlanMaps(short action, java.lang.Object planKey) throws StandardException
updateBestPlanMaps
in interface Optimizer
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 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.StandardException
void addScopedPredicatesToList(PredicateList pList, ContextManager cm) throws StandardException
pList
- List of predicates to add to this OptimizerImpl's
own list for pushing.StandardException
private OptTrace tracer()
public int getOptimizableCount()
Optimizer
getOptimizableCount
in interface Optimizer
public Optimizable getOptimizable(int idx)
Optimizer
getOptimizable
in interface Optimizer
Apache Derby V10.13 Internals - Copyright © 2004,2016 The Apache Software Foundation. All Rights Reserved.