| 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.command.query; |
| 7 |
|
| 8 |
import java.util.BitSet; |
| 9 |
import java.util.Random; |
| 10 |
import org.h2.engine.SessionLocal; |
| 11 |
import org.h2.expression.Expression; |
| 12 |
import org.h2.table.Plan; |
| 13 |
import org.h2.table.PlanItem; |
| 14 |
import org.h2.table.TableFilter; |
| 15 |
import org.h2.util.Permutations; |
| 16 |
|
| 17 |
/** |
| 18 |
* The optimizer is responsible to find the best execution plan |
| 19 |
* for a given query. |
| 20 |
*/ |
| 21 |
class Optimizer { |
| 22 |
|
| 23 |
private static final int MAX_BRUTE_FORCE_FILTERS = 7; |
| 24 |
private static final int MAX_BRUTE_FORCE = 2000; |
| 25 |
private static final int MAX_GENETIC = 500; |
| 26 |
private long startNs; |
| 27 |
private BitSet switched; |
| 28 |
|
| 29 |
// possible plans for filters, if using brute force: |
| 30 |
// 1 filter 1 plan |
| 31 |
// 2 filters 2 plans |
| 32 |
// 3 filters 6 plans |
| 33 |
// 4 filters 24 plans |
| 34 |
// 5 filters 120 plans |
| 35 |
// 6 filters 720 plans |
| 36 |
// 7 filters 5040 plans |
| 37 |
// 8 filters 40320 plan |
| 38 |
// 9 filters 362880 plans |
| 39 |
// 10 filters 3628800 filters |
| 40 |
|
| 41 |
private final TableFilter[] filters; |
| 42 |
private final Expression condition; |
| 43 |
private final SessionLocal session; |
| 44 |
|
| 45 |
private Plan bestPlan; |
| 46 |
private TableFilter topFilter; |
| 47 |
private double cost; |
| 48 |
private Random random; |
| 49 |
private final AllColumnsForPlan allColumnsSet; |
| 50 |
|
| 51 |
Optimizer(TableFilter[] filters, Expression condition, SessionLocal session) { |
| 52 |
this.filters = filters; |
| 53 |
this.condition = condition; |
| 54 |
this.session = session; |
| 55 |
allColumnsSet = new AllColumnsForPlan(filters); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* How many filter to calculate using brute force. The remaining filters are |
| 60 |
* selected using a greedy algorithm which has a runtime of (1 + 2 + ... + |
| 61 |
* n) = (n * (n-1) / 2) for n filters. The brute force algorithm has a |
| 62 |
* runtime of n * (n-1) * ... * (n-m) when calculating m brute force of n |
| 63 |
* total. The combined runtime is (brute force) * (greedy). |
| 64 |
* |
| 65 |
* @param filterCount the number of filters total |
| 66 |
* @return the number of filters to calculate using brute force |
| 67 |
*/ |
| 68 |
private static int getMaxBruteForceFilters(int filterCount) { |
| 69 |
int i = 0, j = filterCount, total = filterCount; |
| 70 |
while (j > 0 && total * (j * (j - 1) / 2) < MAX_BRUTE_FORCE) { |
| 71 |
j--; |
| 72 |
total *= j; |
| 73 |
i++; |
| 74 |
} |
| 75 |
return i; |
| 76 |
} |
| 77 |
|
| 78 |
private void calculateBestPlan() { |
| 79 |
cost = -1; |
| 80 |
if (filters.length == 1) { |
| 81 |
testPlan(filters); |
| 82 |
} else { |
| 83 |
startNs = System.nanoTime(); |
| 84 |
if (filters.length <= MAX_BRUTE_FORCE_FILTERS) { |
| 85 |
calculateBruteForceAll(); |
| 86 |
} else { |
| 87 |
calculateBruteForceSome(); |
| 88 |
random = new Random(0); |
| 89 |
calculateGenetic(); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
private void calculateFakePlan() { |
| 95 |
cost = -1; |
| 96 |
bestPlan = new Plan(filters, filters.length, condition); |
| 97 |
} |
| 98 |
|
| 99 |
private boolean canStop(int x) { |
| 100 |
return (x & 127) == 0 |
| 101 |
// don't calculate for simple queries (no rows or so) |
| 102 |
&& cost >= 0 |
| 103 |
// 100 microseconds * cost |
| 104 |
&& System.nanoTime() - startNs > cost * 100_000L; |
| 105 |
} |
| 106 |
|
| 107 |
private void calculateBruteForceAll() { |
| 108 |
TableFilter[] list = new TableFilter[filters.length]; |
| 109 |
Permutations<TableFilter> p = Permutations.create(filters, list); |
| 110 |
for (int x = 0; !canStop(x) && p.next(); x++) { |
| 111 |
testPlan(list); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
private void calculateBruteForceSome() { |
| 116 |
int bruteForce = getMaxBruteForceFilters(filters.length); |
| 117 |
TableFilter[] list = new TableFilter[filters.length]; |
| 118 |
Permutations<TableFilter> p = Permutations.create(filters, list, bruteForce); |
| 119 |
for (int x = 0; !canStop(x) && p.next(); x++) { |
| 120 |
// find out what filters are not used yet |
| 121 |
for (TableFilter f : filters) { |
| 122 |
f.setUsed(false); |
| 123 |
} |
| 124 |
for (int i = 0; i < bruteForce; i++) { |
| 125 |
list[i].setUsed(true); |
| 126 |
} |
| 127 |
// fill the remaining elements with the unused elements (greedy) |
| 128 |
for (int i = bruteForce; i < filters.length; i++) { |
| 129 |
double costPart = -1.0; |
| 130 |
int bestPart = -1; |
| 131 |
for (int j = 0; j < filters.length; j++) { |
| 132 |
if (!filters[j].isUsed()) { |
| 133 |
if (i == filters.length - 1) { |
| 134 |
bestPart = j; |
| 135 |
break; |
| 136 |
} |
| 137 |
list[i] = filters[j]; |
| 138 |
Plan part = new Plan(list, i+1, condition); |
| 139 |
double costNow = part.calculateCost(session, allColumnsSet); |
| 140 |
if (costPart < 0 || costNow < costPart) { |
| 141 |
costPart = costNow; |
| 142 |
bestPart = j; |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
filters[bestPart].setUsed(true); |
| 147 |
list[i] = filters[bestPart]; |
| 148 |
} |
| 149 |
testPlan(list); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
private void calculateGenetic() { |
| 154 |
TableFilter[] best = new TableFilter[filters.length]; |
| 155 |
TableFilter[] list = new TableFilter[filters.length]; |
| 156 |
for (int x = 0; x < MAX_GENETIC; x++) { |
| 157 |
if (canStop(x)) { |
| 158 |
break; |
| 159 |
} |
| 160 |
boolean generateRandom = (x & 127) == 0; |
| 161 |
if (!generateRandom) { |
| 162 |
System.arraycopy(best, 0, list, 0, filters.length); |
| 163 |
if (!shuffleTwo(list)) { |
| 164 |
generateRandom = true; |
| 165 |
} |
| 166 |
} |
| 167 |
if (generateRandom) { |
| 168 |
switched = new BitSet(); |
| 169 |
System.arraycopy(filters, 0, best, 0, filters.length); |
| 170 |
shuffleAll(best); |
| 171 |
System.arraycopy(best, 0, list, 0, filters.length); |
| 172 |
} |
| 173 |
if (testPlan(list)) { |
| 174 |
switched = new BitSet(); |
| 175 |
System.arraycopy(list, 0, best, 0, filters.length); |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
private boolean testPlan(TableFilter[] list) { |
| 181 |
Plan p = new Plan(list, list.length, condition); |
| 182 |
double costNow = p.calculateCost(session, allColumnsSet); |
| 183 |
if (cost < 0 || costNow < cost) { |
| 184 |
cost = costNow; |
| 185 |
bestPlan = p; |
| 186 |
return true; |
| 187 |
} |
| 188 |
return false; |
| 189 |
} |
| 190 |
|
| 191 |
private void shuffleAll(TableFilter[] f) { |
| 192 |
for (int i = 0; i < f.length - 1; i++) { |
| 193 |
int j = i + random.nextInt(f.length - i); |
| 194 |
if (j != i) { |
| 195 |
TableFilter temp = f[i]; |
| 196 |
f[i] = f[j]; |
| 197 |
f[j] = temp; |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
private boolean shuffleTwo(TableFilter[] f) { |
| 203 |
int a = 0, b = 0, i = 0; |
| 204 |
for (; i < 20; i++) { |
| 205 |
a = random.nextInt(f.length); |
| 206 |
b = random.nextInt(f.length); |
| 207 |
if (a == b) { |
| 208 |
continue; |
| 209 |
} |
| 210 |
if (a < b) { |
| 211 |
int temp = a; |
| 212 |
a = b; |
| 213 |
b = temp; |
| 214 |
} |
| 215 |
int s = a * f.length + b; |
| 216 |
if (switched.get(s)) { |
| 217 |
continue; |
| 218 |
} |
| 219 |
switched.set(s); |
| 220 |
break; |
| 221 |
} |
| 222 |
if (i == 20) { |
| 223 |
return false; |
| 224 |
} |
| 225 |
TableFilter temp = f[a]; |
| 226 |
f[a] = f[b]; |
| 227 |
f[b] = temp; |
| 228 |
return true; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Calculate the best query plan to use. |
| 233 |
* |
| 234 |
* @param parse If we do not need to really get the best plan because it is |
| 235 |
* a view parsing stage. |
| 236 |
*/ |
| 237 |
void optimize(boolean parse) { |
| 238 |
if (parse) { |
| 239 |
calculateFakePlan(); |
| 240 |
} else { |
| 241 |
calculateBestPlan(); |
| 242 |
bestPlan.removeUnusableIndexConditions(); |
| 243 |
} |
| 244 |
TableFilter[] f2 = bestPlan.getFilters(); |
| 245 |
topFilter = f2[0]; |
| 246 |
for (int i = 0; i < f2.length - 1; i++) { |
| 247 |
f2[i].addJoin(f2[i + 1], false, null); |
| 248 |
} |
| 249 |
if (parse) { |
| 250 |
return; |
| 251 |
} |
| 252 |
for (TableFilter f : f2) { |
| 253 |
PlanItem item = bestPlan.getItem(f); |
| 254 |
f.setPlanItem(item); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
public TableFilter getTopFilter() { |
| 259 |
return topFilter; |
| 260 |
} |
| 261 |
|
| 262 |
double getCost() { |
| 263 |
return cost; |
| 264 |
} |
| 265 |
|
| 266 |
} |