public class JoinOptimizer
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
(package private) java.util.Vector<LogicalJoinNode> |
joins |
(package private) LogicalPlan |
p |
Constructor and Description |
---|
JoinOptimizer(LogicalPlan p,
java.util.Vector<LogicalJoinNode> joins)
Constructor
|
Modifier and Type | Method and Description |
---|---|
private CostCard |
computeCostAndCardOfSubplan(java.util.HashMap<java.lang.String,TableStats> stats,
java.util.HashMap<java.lang.String,java.lang.Double> filterSelectivities,
LogicalJoinNode joinToRemove,
java.util.Set<LogicalJoinNode> joinSet,
double bestCostSoFar,
PlanCache pc)
This is a helper method that computes the cost and cardinality of joining
joinToRemove to joinSet (joinSet should contain joinToRemove), given that
all of the subsets of size joinSet.size() - 1 have already been computed
and stored in PlanCache pc.
|
private boolean |
doesJoin(java.util.Vector<LogicalJoinNode> joinlist,
java.lang.String table)
Return true if the specified table is in the list of joins, false
otherwise
|
<T> java.util.Set<java.util.Set<T>> |
enumerateSubsets(java.util.Vector<T> v,
int size)
Helper method to enumerate all of the subsets of a given size of a
specified vector.
|
int |
estimateJoinCardinality(LogicalJoinNode j,
int card1,
int card2,
boolean t1pkey,
boolean t2pkey,
java.util.Map<java.lang.String,TableStats> stats)
Estimate the cardinality of a join.
|
double |
estimateJoinCost(LogicalJoinNode j,
int card1,
int card2,
double cost1,
double cost2)
Estimate the cost of a join.
|
static int |
estimateTableJoinCardinality(Predicate.Op joinOp,
java.lang.String table1Alias,
java.lang.String table2Alias,
java.lang.String field1PureName,
java.lang.String field2PureName,
int card1,
int card2,
boolean t1pkey,
boolean t2pkey,
java.util.Map<java.lang.String,TableStats> stats,
java.util.Map<java.lang.String,java.lang.Integer> tableAliasToId)
Estimate join cardinality
|
private boolean |
hasPkey(java.util.Vector<LogicalJoinNode> joinlist)
Return true if a primary key field is joined by one of the joins in
joinlist
|
static DbIterator |
instantiateJoin(LogicalJoinNode lj,
DbIterator plan1,
DbIterator plan2)
Return best iterator for computing a given logical join, given the
specified statistics, and the provided left and right subplans.
|
private boolean |
isPkey(java.lang.String tableAlias,
java.lang.String field)
Return true if field is a primary key of the specified table, false
otherwise
|
java.util.Vector<LogicalJoinNode> |
orderJoins(java.util.HashMap<java.lang.String,TableStats> stats,
java.util.HashMap<java.lang.String,java.lang.Double> filterSelectivities,
boolean explain)
Compute a logical, reasonably efficient join on the specified tables.
|
private void |
printJoins(java.util.Vector<LogicalJoinNode> js,
PlanCache pc,
java.util.HashMap<java.lang.String,TableStats> stats,
java.util.HashMap<java.lang.String,java.lang.Double> selectivities)
Helper function to display a Swing window with a tree representation of
the specified list of joins.
|
LogicalPlan p
java.util.Vector<LogicalJoinNode> joins
public JoinOptimizer(LogicalPlan p, java.util.Vector<LogicalJoinNode> joins)
p
- the logical plan being optimizedjoins
- the list of joins being performedpublic static DbIterator instantiateJoin(LogicalJoinNode lj, DbIterator plan1, DbIterator plan2) throws ParsingException
lj
- The join being consideredplan1
- The left join node's childplan2
- The right join node's childParsingException
public double estimateJoinCost(LogicalJoinNode j, int card1, int card2, double cost1, double cost2)
j
- A LogicalJoinNode representing the join operation being
performed.card1
- Estimated cardinality of the left-hand side of the querycard2
- Estimated cardinality of the right-hand side of the querycost1
- Estimated cost of one full scan of the table on the left-hand
side of the querycost2
- Estimated cost of one full scan of the table on the right-hand
side of the querypublic int estimateJoinCardinality(LogicalJoinNode j, int card1, int card2, boolean t1pkey, boolean t2pkey, java.util.Map<java.lang.String,TableStats> stats)
j
- A LogicalJoinNode representing the join operation being
performed.card1
- Cardinality of the left-hand table in the joincard2
- Cardinality of the right-hand table in the joint1pkey
- Is the left-hand table a primary-key table?t2pkey
- Is the right-hand table a primary-key table?stats
- The table stats, referenced by table names, not aliaspublic static int estimateTableJoinCardinality(Predicate.Op joinOp, java.lang.String table1Alias, java.lang.String table2Alias, java.lang.String field1PureName, java.lang.String field2PureName, int card1, int card2, boolean t1pkey, boolean t2pkey, java.util.Map<java.lang.String,TableStats> stats, java.util.Map<java.lang.String,java.lang.Integer> tableAliasToId)
joinOp
- table1Alias
- table2Alias
- field1PureName
- field2PureName
- card1
- card2
- t1pkey
- t2pkey
- stats
- tableAliasToId
- public <T> java.util.Set<java.util.Set<T>> enumerateSubsets(java.util.Vector<T> v, int size)
v
- The vector whose subsets are desiredsize
- The size of the subsets of interestpublic java.util.Vector<LogicalJoinNode> orderJoins(java.util.HashMap<java.lang.String,TableStats> stats, java.util.HashMap<java.lang.String,java.lang.Double> filterSelectivities, boolean explain) throws ParsingException
stats
- Statistics for each table involved in the join, referenced by
base table names, not aliasfilterSelectivities
- Selectivities of the filter predicates on each table in the
join, referenced by table alias (if no alias, the base table
name)explain
- Indicates whether your code should explain its query plan or
simply execute itParsingException
- when stats or filter selectivities is missing a table in the
join, or or when another internal error occursprivate CostCard computeCostAndCardOfSubplan(java.util.HashMap<java.lang.String,TableStats> stats, java.util.HashMap<java.lang.String,java.lang.Double> filterSelectivities, LogicalJoinNode joinToRemove, java.util.Set<LogicalJoinNode> joinSet, double bestCostSoFar, PlanCache pc) throws ParsingException
stats
- table stats for all of the tables, referenced by table names
rather than alias (see orderJoins(java.util.HashMap<java.lang.String, simpledb.TableStats>, java.util.HashMap<java.lang.String, java.lang.Double>, boolean)
)filterSelectivities
- the selectivities of the filters over each of the tables
(where tables are indentified by their alias or name if no
alias is given)joinToRemove
- the join to remove from joinSetjoinSet
- the set of joins being consideredbestCostSoFar
- the best way to join joinSet so far (minimum of previous
invocations of computeCostAndCardOfSubplan for this joinSet,
from returned CostCard)pc
- the PlanCache for this join; should have subplans for all
plans of size joinSet.size()-1CostCard
objects desribing the cost, cardinality,
optimal subplanParsingException
- when stats, filterSelectivities, or pc object is missing
tables involved in joinprivate boolean doesJoin(java.util.Vector<LogicalJoinNode> joinlist, java.lang.String table)
private boolean isPkey(java.lang.String tableAlias, java.lang.String field)
tableAlias
- The alias of the table in the queryfield
- The pure name of the fieldprivate boolean hasPkey(java.util.Vector<LogicalJoinNode> joinlist)
private void printJoins(java.util.Vector<LogicalJoinNode> js, PlanCache pc, java.util.HashMap<java.lang.String,TableStats> stats, java.util.HashMap<java.lang.String,java.lang.Double> selectivities)
orderJoins(java.util.HashMap<java.lang.String, simpledb.TableStats>, java.util.HashMap<java.lang.String, java.lang.Double>, boolean)
, which may want to
call this when the analyze flag is true.js
- the join plan to visualizepc
- the PlanCache accumulated whild building the optimal planstats
- table statistics for base tablesselectivities
- the selectivities of the filters over each of the tables
(where tables are indentified by their alias or name if no
alias is given)