Search in sources :

Example 1 with PlanItem

use of org.h2.table.PlanItem in project h2database by h2database.

the class Delete method prepare.

@Override
public void prepare() {
    if (condition != null) {
        condition.mapColumns(targetTableFilter, 0);
        if (sourceTableFilter != null) {
            condition.mapColumns(sourceTableFilter, 0);
        }
        condition = condition.optimize(session);
        condition.createIndexConditions(session, targetTableFilter);
    }
    TableFilter[] filters;
    if (sourceTableFilter == null) {
        filters = new TableFilter[] { targetTableFilter };
    } else {
        filters = new TableFilter[] { targetTableFilter, sourceTableFilter };
    }
    PlanItem item = targetTableFilter.getBestPlanItem(session, filters, 0, ExpressionVisitor.allColumnsForTableFilters(filters));
    targetTableFilter.setPlanItem(item);
    targetTableFilter.prepare();
}
Also used : TableFilter(org.h2.table.TableFilter) PlanItem(org.h2.table.PlanItem)

Example 2 with PlanItem

use of org.h2.table.PlanItem in project h2database by h2database.

the class TableFilter method getBestPlanItem.

/**
 * Get the best plan item (index, cost) to use use for the current join
 * order.
 *
 * @param s the session
 * @param filters all joined table filters
 * @param filter the current table filter index
 * @param allColumnsSet the set of all columns
 * @return the best plan item
 */
public PlanItem getBestPlanItem(Session s, TableFilter[] filters, int filter, HashSet<Column> allColumnsSet) {
    PlanItem item1 = null;
    SortOrder sortOrder = null;
    if (select != null) {
        sortOrder = select.getSortOrder();
    }
    if (indexConditions.isEmpty()) {
        item1 = new PlanItem();
        item1.setIndex(table.getScanIndex(s, null, filters, filter, sortOrder, allColumnsSet));
        item1.cost = item1.getIndex().getCost(s, null, filters, filter, sortOrder, allColumnsSet);
    }
    int len = table.getColumns().length;
    int[] masks = new int[len];
    for (IndexCondition condition : indexConditions) {
        if (condition.isEvaluatable()) {
            if (condition.isAlwaysFalse()) {
                masks = null;
                break;
            }
            int id = condition.getColumn().getColumnId();
            if (id >= 0) {
                masks[id] |= condition.getMask(indexConditions);
            }
        }
    }
    PlanItem item = table.getBestPlanItem(s, masks, filters, filter, sortOrder, allColumnsSet);
    item.setMasks(masks);
    // The more index conditions, the earlier the table.
    // This is to ensure joins without indexes run quickly:
    // x (x.a=10); y (x.b=y.b) - see issue 113
    item.cost -= item.cost * indexConditions.size() / 100 / (filter + 1);
    if (item1 != null && item1.cost < item.cost) {
        item = item1;
    }
    if (nestedJoin != null) {
        setEvaluatable(true);
        item.setNestedJoinPlan(nestedJoin.getBestPlanItem(s, filters, filter, allColumnsSet));
        // TODO optimizer: calculate cost of a join: should use separate
        // expected row number and lookup cost
        item.cost += item.cost * item.getNestedJoinPlan().cost;
    }
    if (join != null) {
        setEvaluatable(true);
        do {
            filter++;
        } while (filters[filter] != join);
        item.setJoinPlan(join.getBestPlanItem(s, filters, filter, allColumnsSet));
        // TODO optimizer: calculate cost of a join: should use separate
        // expected row number and lookup cost
        item.cost += item.cost * item.getJoinPlan().cost;
    }
    return item;
}
Also used : SortOrder(org.h2.result.SortOrder) IndexCondition(org.h2.index.IndexCondition)

Example 3 with PlanItem

use of org.h2.table.PlanItem in project h2database by h2database.

the class Optimizer method optimize.

/**
 * Calculate the best query plan to use.
 *
 * @param parse If we do not need to really get the best plan because it is
 *            a view parsing stage.
 */
void optimize(boolean parse) {
    if (parse) {
        calculateFakePlan();
    } else {
        calculateBestPlan();
        bestPlan.removeUnusableIndexConditions();
    }
    TableFilter[] f2 = bestPlan.getFilters();
    topFilter = f2[0];
    for (int i = 0; i < f2.length - 1; i++) {
        f2[i].addJoin(f2[i + 1], false, null);
    }
    if (parse) {
        return;
    }
    for (TableFilter f : f2) {
        PlanItem item = bestPlan.getItem(f);
        f.setPlanItem(item);
    }
}
Also used : TableFilter(org.h2.table.TableFilter) PlanItem(org.h2.table.PlanItem)

Example 4 with PlanItem

use of org.h2.table.PlanItem in project h2database by h2database.

the class ScriptCommand method generateInsertValues.

private int generateInsertValues(int count, Table table) throws IOException {
    PlanItem plan = table.getBestPlanItem(session, null, null, -1, null, null);
    Index index = plan.getIndex();
    Cursor cursor = index.find(session, null, null);
    Column[] columns = table.getColumns();
    StatementBuilder buff = new StatementBuilder("INSERT INTO ");
    buff.append(table.getSQL()).append('(');
    for (Column col : columns) {
        buff.appendExceptFirst(", ");
        buff.append(Parser.quoteIdentifier(col.getName()));
    }
    buff.append(") VALUES");
    if (!simple) {
        buff.append('\n');
    }
    buff.append('(');
    String ins = buff.toString();
    buff = null;
    while (cursor.next()) {
        Row row = cursor.get();
        if (buff == null) {
            buff = new StatementBuilder(ins);
        } else {
            buff.append(",\n(");
        }
        for (int j = 0; j < row.getColumnCount(); j++) {
            if (j > 0) {
                buff.append(", ");
            }
            Value v = row.getValue(j);
            if (v.getPrecision() > lobBlockSize) {
                int id;
                if (v.getType() == Value.CLOB) {
                    id = writeLobStream(v);
                    buff.append("SYSTEM_COMBINE_CLOB(").append(id).append(')');
                } else if (v.getType() == Value.BLOB) {
                    id = writeLobStream(v);
                    buff.append("SYSTEM_COMBINE_BLOB(").append(id).append(')');
                } else {
                    buff.append(v.getSQL());
                }
            } else {
                buff.append(v.getSQL());
            }
        }
        buff.append(')');
        count++;
        if ((count & 127) == 0) {
            checkCanceled();
        }
        if (simple || buff.length() > Constants.IO_BUFFER_SIZE) {
            add(buff.toString(), true);
            buff = null;
        }
    }
    if (buff != null) {
        add(buff.toString(), true);
    }
    return count;
}
Also used : Column(org.h2.table.Column) ExpressionColumn(org.h2.expression.ExpressionColumn) StatementBuilder(org.h2.util.StatementBuilder) Value(org.h2.value.Value) PlanItem(org.h2.table.PlanItem) Index(org.h2.index.Index) ValueString(org.h2.value.ValueString) Row(org.h2.result.Row) Cursor(org.h2.index.Cursor) Constraint(org.h2.constraint.Constraint)

Example 5 with PlanItem

use of org.h2.table.PlanItem in project h2database by h2database.

the class Update method prepare.

@Override
public void prepare() {
    if (condition != null) {
        condition.mapColumns(targetTableFilter, 0);
        condition = condition.optimize(session);
        condition.createIndexConditions(session, targetTableFilter);
    }
    for (Column c : columns) {
        Expression e = expressionMap.get(c);
        e.mapColumns(targetTableFilter, 0);
        if (sourceTableFilter != null) {
            e.mapColumns(sourceTableFilter, 0);
        }
        expressionMap.put(c, e.optimize(session));
    }
    TableFilter[] filters;
    if (sourceTableFilter == null) {
        filters = new TableFilter[] { targetTableFilter };
    } else {
        filters = new TableFilter[] { targetTableFilter, sourceTableFilter };
    }
    PlanItem item = targetTableFilter.getBestPlanItem(session, filters, 0, ExpressionVisitor.allColumnsForTableFilters(filters));
    targetTableFilter.setPlanItem(item);
    targetTableFilter.prepare();
}
Also used : Column(org.h2.table.Column) ValueExpression(org.h2.expression.ValueExpression) Expression(org.h2.expression.Expression) TableFilter(org.h2.table.TableFilter) PlanItem(org.h2.table.PlanItem)

Aggregations

PlanItem (org.h2.table.PlanItem)4 TableFilter (org.h2.table.TableFilter)3 Constraint (org.h2.constraint.Constraint)2 Expression (org.h2.expression.Expression)2 Index (org.h2.index.Index)2 Trace (org.h2.message.Trace)2 Column (org.h2.table.Column)2 DbObject (org.h2.engine.DbObject)1 ExpressionColumn (org.h2.expression.ExpressionColumn)1 ValueExpression (org.h2.expression.ValueExpression)1 Cursor (org.h2.index.Cursor)1 IndexCondition (org.h2.index.IndexCondition)1 ViewIndex (org.h2.index.ViewIndex)1 Row (org.h2.result.Row)1 SortOrder (org.h2.result.SortOrder)1 StatementBuilder (org.h2.util.StatementBuilder)1 Value (org.h2.value.Value)1 ValueString (org.h2.value.ValueString)1