public class OptimizerImpl extends java.lang.Object implements Optimizer
ADDING_UNORDERED_OPTIMIZABLE, CALLING_NEXT_ACCESS_PATH, CALLING_ON_JOIN_NODE, CHANGING_ACCESS_PATH_FOR_TABLE, CHEAPEST_PLAN_SO_FAR, COMPLETE_JOIN_ORDER, COMPOSITE_SEL_FROM_STATS, CONSIDERING_CONGLOMERATE, CONSIDERING_JOIN_ORDER, CONSIDERING_JOIN_STRATEGY, COST_INCLUDING_COMPOSITE_SEL_FROM_STATS, COST_INCLUDING_EXTRA_1ST_COL_SELECTIVITY, COST_INCLUDING_EXTRA_NONQUALIFIER_SELECTIVITY, COST_INCLUDING_EXTRA_QUALIFIER_SELECTIVITY, COST_INCLUDING_EXTRA_START_STOP, COST_INCLUDING_STATS_FOR_INDEX, COST_OF_CHEAPEST_PLAN_SO_FAR, COST_OF_CONGLOMERATE_SCAN1, COST_OF_CONGLOMERATE_SCAN2, COST_OF_CONGLOMERATE_SCAN3, COST_OF_CONGLOMERATE_SCAN4, COST_OF_CONGLOMERATE_SCAN5, COST_OF_CONGLOMERATE_SCAN6, COST_OF_CONGLOMERATE_SCAN7, COST_OF_N_SCANS, COST_OF_NONCOVERING_INDEX, COST_OF_SORTING, CURRENT_PLAN_IS_SA_PLAN, ESTIMATING_COST_OF_CONGLOMERATE, HJ_HASH_KEY_COLUMNS, HJ_SKIP_NO_JOIN_COLUMNS, HJ_SKIP_NOT_MATERIALIZABLE, ILLEGAL_USER_JOIN_ORDER, JOIN_ORDER_OPTIMIZATION, LOOKING_FOR_SPECIFIED_INDEX, MATCH_SINGLE_ROW_COST, MAX_DYNAMIC_MATERIALIZED_ROWS, MAX_MEMORY_PER_TABLE, MODIFYING_ACCESS_PATHS, MODULE, NO_BEST_PLAN, NO_MORE_CONGLOMERATES, NO_TABLES, NO_TIMEOUT, NON_COVERING_INDEX_COST, NORMAL_PLAN, PLAN_TYPE, REMEMBERING_BEST_ACCESS_PATH, REMEMBERING_BEST_ACCESS_PATH_SUBSTRING, REMEMBERING_BEST_JOIN_ORDER, REMEMBERING_BEST_SORT_AVOIDANCE_ACCESS_PATH_SUBSTRING, REMEMBERING_BEST_UNKNOWN_ACCESS_PATH_SUBSTRING, REMEMBERING_JOIN_STRATEGY, ROW_LOCK_ALL_CONSTANT_START_STOP, ROW_LOCK_UNDER_THRESHOLD, RULE_BASED_OPTIMIZATION, SCANNING_HEAP_FULL_MATCH_ON_UNIQUE_KEY, SHORT_CIRCUITING, SKIPPING_DUE_TO_EXCESS_MEMORY, SKIPPING_JOIN_ORDER, SORT_AVOIDANCE_PLAN, SORT_NEEDED_FOR_ORDERING, STARTED, TABLE_LOCK_NO_START_STOP, TABLE_LOCK_OVER_THRESHOLD, TIME_EXCEEDED, TOTAL_COST_NON_SA_PLAN, TOTAL_COST_SA_PLAN, TOTAL_COST_WITH_SORTING, USE_STATISTICS, USER_JOIN_ORDER_OPTIMIZED| Modifier | Constructor and Description |
|---|---|
protected |
OptimizerImpl(OptimizableList optimizableList,
OptimizablePredicateList predicateList,
DataDictionary dDictionary,
boolean ruleBasedOptimization,
boolean noTimeout,
boolean useStatistics,
int maxMemoryPerTable,
JoinStrategy[] joinStrategies,
int tableLockThreshold,
RequiredRowOrdering requiredRowOrdering,
int numTablesInQuery) |
| Modifier and Type | Method and Description |
|---|---|
protected void |
addScopedPredicatesToList(PredicateList pList)
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() |
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.
|
CostEstimate |
getOptimizedCost()
Get the estimated cost of the optimized query
|
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) |
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.
|
CostEstimate |
newCostEstimate()
Get a new CostEstimate object
|
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.
|
void |
trace(int traceFlag,
int intParam1,
int intParam2,
double doubleParam,
java.lang.Object objectParam1)
Optimizer trace.
|
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.
|
protected 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.
|
DataDictionary dDictionary
int numTablesInQuery
int numOptimizables
protected JBitSet assignedTableMap
protected OptimizableList optimizableList
OptimizablePredicateList predicateList
JBitSet nonCorrelatedTableMap
protected int[] proposedJoinOrder
protected int[] bestJoinOrder
protected int joinPosition
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
protected CostEstimateImpl currentCost
protected CostEstimateImpl currentSortAvoidanceCost
protected CostEstimateImpl bestCost
protected long timeOptimizationStarted
protected long currentTime
protected boolean timeExceeded
private boolean noTimeout
private boolean useStatistics
private int tableLockThreshold
private JoinStrategy[] joinStrategies
protected RequiredRowOrdering requiredRowOrdering
private boolean foundABestPlan
protected CostEstimate sortCost
private RowOrdering currentRowOrdering
private RowOrdering bestRowOrdering
private boolean conglomerate_OneRowResultSet
protected boolean optimizerTrace
protected boolean optimizerTraceHtml
protected int maxMemoryPerTable
private boolean reloadBestPlan
private java.util.HashMap savedJoinOrders
protected double timeLimit
CostEstimate finalCostEstimate
private boolean usingPredsPushedFromAbove
private boolean bestJoinOrderUsedPredsFromAbove
protected OptimizerImpl(OptimizableList optimizableList, OptimizablePredicateList predicateList, DataDictionary dDictionary, boolean ruleBasedOptimization, boolean noTimeout, boolean useStatistics, int maxMemoryPerTable, JoinStrategy[] joinStrategies, int tableLockThreshold, RequiredRowOrdering requiredRowOrdering, int numTablesInQuery) throws StandardException
StandardExceptionpublic void prepForNextRound()
prepForNextRound in interface Optimizerprivate int initJumpState()
public int getMaxMemoryPerTable()
getMaxMemoryPerTable in interface Optimizerpublic boolean getNextPermutation()
throws StandardException
OptimizergetNextPermutation in interface OptimizerStandardException - Thrown on errorOptimizer.getNextPermutation()private void rewindJoinOrder()
throws StandardException
StandardExceptionprivate void endOfRoundCleanup()
throws StandardException
StandardExceptionprivate double recoverCostFromProposedJoinOrder(boolean sortAvoidance)
throws StandardException
StandardExceptionprivate boolean joinOrderMeetsDependencies(int optNumber)
throws StandardException
StandardExceptionprivate void pullOptimizableFromJoinOrder()
throws StandardException
StandardExceptionvoid pushPredicates(Optimizable curTable, JBitSet outerTables) throws StandardException
StandardExceptionpublic boolean getNextDecoratedPermutation()
throws StandardException
OptimizergetNextDecoratedPermutation in interface OptimizerStandardException - Thrown on errorOptimizer.getNextDecoratedPermutation()private void rememberBestCost(CostEstimate currentCost, int planType) throws StandardException
StandardException - Thrown on errorpublic void costPermutation()
throws StandardException
OptimizercostPermutation in interface OptimizerStandardException - Thrown on errorOptimizer.costPermutation()public void costOptimizable(Optimizable optimizable, TableDescriptor td, ConglomerateDescriptor cd, OptimizablePredicateList predList, CostEstimate outerCost) throws StandardException
OptimizercostOptimizable in interface Optimizeroptimizable - 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
StandardExceptionprivate void costBasedCostOptimizable(Optimizable optimizable, TableDescriptor td, ConglomerateDescriptor cd, OptimizablePredicateList predList, CostEstimate outerCost) throws StandardException
StandardExceptionpublic void considerCost(Optimizable optimizable, OptimizablePredicateList predList, CostEstimate estimatedCost, CostEstimate outerCost) throws StandardException
considerCost in interface Optimizeroptimizable - 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()
OptimizergetDataDictionary in interface OptimizerOptimizer.getDataDictionary()public void modifyAccessPaths()
throws StandardException
OptimizermodifyAccessPaths in interface OptimizerStandardException - Thrown on errorOptimizer.modifyAccessPaths()public CostEstimate newCostEstimate()
OptimizernewCostEstimate in interface OptimizerOptimizer.newCostEstimate()public CostEstimate getOptimizedCost()
OptimizergetOptimizedCost in interface OptimizerOptimizer.getOptimizedCost()public CostEstimate getFinalCost()
Optimizerpublic void setOuterRows(double outerRows)
OptimizersetOuterRows in interface OptimizerOptimizer.setOuterRows(double)public int tableLockThreshold()
OptimizertableLockThreshold in interface OptimizerOptimizer.tableLockThreshold()public int getNumberOfJoinStrategies()
getNumberOfJoinStrategies in interface Optimizerpublic JoinStrategy getJoinStrategy(int whichStrategy)
OptimizergetJoinStrategy in interface OptimizerOptimizer.getJoinStrategy(int)public JoinStrategy getJoinStrategy(java.lang.String whichStrategy)
OptimizergetJoinStrategy in interface OptimizerOptimizer.getJoinStrategy(int)public double uniqueJoinWithOuterTable(OptimizablePredicateList predList) throws StandardException
OptimizeruniqueJoinWithOuterTable in interface OptimizerpredList - 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()
OptimizergetLevel in interface OptimizerOptimizer.getLevel()public CostEstimateImpl getNewCostEstimate(double theCost, double theRowCount, double theSingleScanRowCount)
public void trace(int traceFlag,
int intParam1,
int intParam2,
double doubleParam,
java.lang.Object objectParam1)
Optimizerpublic boolean useStatistics()
OptimizeruseStatistics in interface OptimizerOptimizer.useStatistics()protected void updateBestPlanMaps(short action,
java.lang.Object planKey)
throws StandardException
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.StandardExceptionprotected void addScopedPredicatesToList(PredicateList pList) throws StandardException
pList - List of predicates to add to this OptimizerImpl's
own list for pushing.StandardExceptionApache Derby V10.10 Internals - Copyright © 2004,2014 The Apache Software Foundation. All Rights Reserved.