| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.table; |
| 7 |
|
| 8 |
import java.util.ArrayList; |
| 9 |
import java.util.Arrays; |
| 10 |
import java.util.HashMap; |
| 11 |
|
| 12 |
import org.h2.command.query.AllColumnsForPlan; |
| 13 |
import org.h2.engine.SessionLocal; |
| 14 |
import org.h2.expression.Expression; |
| 15 |
import org.h2.expression.ExpressionVisitor; |
| 16 |
import org.h2.message.Trace; |
| 17 |
|
| 18 |
/** |
| 19 |
* A possible query execution plan. The time required to execute a query depends |
| 20 |
* on the order the tables are accessed. |
| 21 |
*/ |
| 22 |
public class Plan { |
| 23 |
|
| 24 |
private final TableFilter[] filters; |
| 25 |
private final HashMap<TableFilter, PlanItem> planItems = new HashMap<>(); |
| 26 |
private final Expression[] allConditions; |
| 27 |
private final TableFilter[] allFilters; |
| 28 |
|
| 29 |
/** |
| 30 |
* Create a query plan with the given order. |
| 31 |
* |
| 32 |
* @param filters the tables of the query |
| 33 |
* @param count the number of table items |
| 34 |
* @param condition the condition in the WHERE clause |
| 35 |
*/ |
| 36 |
public Plan(TableFilter[] filters, int count, Expression condition) { |
| 37 |
this.filters = new TableFilter[count]; |
| 38 |
System.arraycopy(filters, 0, this.filters, 0, count); |
| 39 |
final ArrayList<Expression> allCond = new ArrayList<>(); |
| 40 |
final ArrayList<TableFilter> all = new ArrayList<>(); |
| 41 |
if (condition != null) { |
| 42 |
allCond.add(condition); |
| 43 |
} |
| 44 |
for (int i = 0; i < count; i++) { |
| 45 |
TableFilter f = filters[i]; |
| 46 |
f.visit(f1 -> { |
| 47 |
all.add(f1); |
| 48 |
if (f1.getJoinCondition() != null) { |
| 49 |
allCond.add(f1.getJoinCondition()); |
| 50 |
} |
| 51 |
}); |
| 52 |
} |
| 53 |
allConditions = allCond.toArray(new Expression[0]); |
| 54 |
allFilters = all.toArray(new TableFilter[0]); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Get the plan item for the given table. |
| 59 |
* |
| 60 |
* @param filter the table |
| 61 |
* @return the plan item |
| 62 |
*/ |
| 63 |
public PlanItem getItem(TableFilter filter) { |
| 64 |
return planItems.get(filter); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* The the list of tables. |
| 69 |
* |
| 70 |
* @return the list of tables |
| 71 |
*/ |
| 72 |
public TableFilter[] getFilters() { |
| 73 |
return filters; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Remove all index conditions that can not be used. |
| 78 |
*/ |
| 79 |
public void removeUnusableIndexConditions() { |
| 80 |
for (int i = 0; i < allFilters.length; i++) { |
| 81 |
TableFilter f = allFilters[i]; |
| 82 |
setEvaluatable(f, true); |
| 83 |
if (i < allFilters.length - 1) { |
| 84 |
// the last table doesn't need the optimization, |
| 85 |
// otherwise the expression is calculated twice unnecessarily |
| 86 |
// (not that bad but not optimal) |
| 87 |
f.optimizeFullCondition(); |
| 88 |
} |
| 89 |
f.removeUnusableIndexConditions(); |
| 90 |
} |
| 91 |
for (TableFilter f : allFilters) { |
| 92 |
setEvaluatable(f, false); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Calculate the cost of this query plan. |
| 98 |
* |
| 99 |
* @param session the session |
| 100 |
* @param allColumnsSet calculates all columns on-demand |
| 101 |
* @return the cost |
| 102 |
*/ |
| 103 |
public double calculateCost(SessionLocal session, AllColumnsForPlan allColumnsSet) { |
| 104 |
Trace t = session.getTrace(); |
| 105 |
if (t.isDebugEnabled()) { |
| 106 |
t.debug("Plan : calculate cost for plan {0}", Arrays.toString(allFilters)); |
| 107 |
} |
| 108 |
double cost = 1; |
| 109 |
boolean invalidPlan = false; |
| 110 |
for (int i = 0; i < allFilters.length; i++) { |
| 111 |
TableFilter tableFilter = allFilters[i]; |
| 112 |
if (t.isDebugEnabled()) { |
| 113 |
t.debug("Plan : for table filter {0}", tableFilter); |
| 114 |
} |
| 115 |
PlanItem item = tableFilter.getBestPlanItem(session, allFilters, i, allColumnsSet); |
| 116 |
planItems.put(tableFilter, item); |
| 117 |
if (t.isDebugEnabled()) { |
| 118 |
t.debug("Plan : best plan item cost {0} index {1}", |
| 119 |
item.cost, item.getIndex().getPlanSQL()); |
| 120 |
} |
| 121 |
cost += cost * item.cost; |
| 122 |
setEvaluatable(tableFilter, true); |
| 123 |
Expression on = tableFilter.getJoinCondition(); |
| 124 |
if (on != null) { |
| 125 |
if (!on.isEverything(ExpressionVisitor.EVALUATABLE_VISITOR)) { |
| 126 |
invalidPlan = true; |
| 127 |
break; |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
if (invalidPlan) { |
| 132 |
cost = Double.POSITIVE_INFINITY; |
| 133 |
} |
| 134 |
if (t.isDebugEnabled()) { |
| 135 |
session.getTrace().debug("Plan : plan cost {0}", cost); |
| 136 |
} |
| 137 |
for (TableFilter f : allFilters) { |
| 138 |
setEvaluatable(f, false); |
| 139 |
} |
| 140 |
return cost; |
| 141 |
} |
| 142 |
|
| 143 |
private void setEvaluatable(TableFilter filter, boolean b) { |
| 144 |
filter.setEvaluatable(filter, b); |
| 145 |
for (Expression e : allConditions) { |
| 146 |
e.setEvaluatable(filter, b); |
| 147 |
} |
| 148 |
} |
| 149 |
} |