Search in sources :

Example 26 with Plan

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

the class Optimizer method testPlan.

private boolean testPlan(TableFilter[] list) {
    Plan p = new Plan(list, list.length, condition);
    double costNow = p.calculateCost(session);
    if (cost < 0 || costNow < cost) {
        cost = costNow;
        bestPlan = p;
        return true;
    }
    return false;
}
Also used : Plan(org.h2.table.Plan)

Example 27 with Plan

use of org.h2.table.Plan 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 28 with Plan

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

the class Plan method calculateCost.

/**
 * Calculate the cost of this query plan.
 *
 * @param session the session
 * @return the cost
 */
public double calculateCost(Session session) {
    Trace t = session.getTrace();
    if (t.isDebugEnabled()) {
        t.debug("Plan       : calculate cost for plan {0}", Arrays.toString(allFilters));
    }
    double cost = 1;
    boolean invalidPlan = false;
    final HashSet<Column> allColumnsSet = ExpressionVisitor.allColumnsForTableFilters(allFilters);
    for (int i = 0; i < allFilters.length; i++) {
        TableFilter tableFilter = allFilters[i];
        if (t.isDebugEnabled()) {
            t.debug("Plan       :   for table filter {0}", tableFilter);
        }
        PlanItem item = tableFilter.getBestPlanItem(session, allFilters, i, allColumnsSet);
        planItems.put(tableFilter, item);
        if (t.isDebugEnabled()) {
            t.debug("Plan       :   best plan item cost {0} index {1}", item.cost, item.getIndex().getPlanSQL());
        }
        cost += cost * item.cost;
        setEvaluatable(tableFilter, true);
        Expression on = tableFilter.getJoinCondition();
        if (on != null) {
            if (!on.isEverything(ExpressionVisitor.EVALUATABLE_VISITOR)) {
                invalidPlan = true;
                break;
            }
        }
    }
    if (invalidPlan) {
        cost = Double.POSITIVE_INFINITY;
    }
    if (t.isDebugEnabled()) {
        session.getTrace().debug("Plan       : plan cost {0}", cost);
    }
    for (TableFilter f : allFilters) {
        setEvaluatable(f, false);
    }
    return cost;
}
Also used : Trace(org.h2.message.Trace) Expression(org.h2.expression.Expression)

Example 29 with Plan

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

the class Table method getBestPlanItem.

/**
 * Get the best plan for the given search mask.
 *
 * @param session the session
 * @param masks per-column comparison bit masks, null means 'always false',
 *              see constants in IndexCondition
 * @param filters all joined table filters
 * @param filter the current table filter index
 * @param sortOrder the sort order
 * @param allColumnsSet the set of all columns
 * @return the plan item
 */
public PlanItem getBestPlanItem(Session session, int[] masks, TableFilter[] filters, int filter, SortOrder sortOrder, HashSet<Column> allColumnsSet) {
    PlanItem item = new PlanItem();
    item.setIndex(getScanIndex(session));
    item.cost = item.getIndex().getCost(session, null, filters, filter, null, allColumnsSet);
    Trace t = session.getTrace();
    if (t.isDebugEnabled()) {
        t.debug("Table      :     potential plan item cost {0} index {1}", item.cost, item.getIndex().getPlanSQL());
    }
    ArrayList<Index> indexes = getIndexes();
    IndexHints indexHints = getIndexHints(filters, filter);
    if (indexes != null && masks != null) {
        for (int i = 1, size = indexes.size(); i < size; i++) {
            Index index = indexes.get(i);
            if (isIndexExcludedByHints(indexHints, index)) {
                continue;
            }
            double cost = index.getCost(session, masks, filters, filter, sortOrder, allColumnsSet);
            if (t.isDebugEnabled()) {
                t.debug("Table      :     potential plan item cost {0} index {1}", cost, index.getPlanSQL());
            }
            if (cost < item.cost) {
                item.cost = cost;
                item.setIndex(index);
            }
        }
    }
    return item;
}
Also used : Trace(org.h2.message.Trace) Index(org.h2.index.Index) Constraint(org.h2.constraint.Constraint)

Example 30 with Plan

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

the class Parser method parseExplain.

private Explain parseExplain() {
    Explain command = new Explain(session);
    if (readIf("ANALYZE")) {
        command.setExecuteCommand(true);
    } else {
        if (readIf("PLAN")) {
            readIf("FOR");
        }
    }
    if (isToken("SELECT") || isToken("FROM") || isToken("(") || isToken("WITH")) {
        Query query = parseSelect();
        query.setNeverLazy(true);
        command.setCommand(query);
    } else if (readIf("DELETE")) {
        command.setCommand(parseDelete());
    } else if (readIf("UPDATE")) {
        command.setCommand(parseUpdate());
    } else if (readIf("INSERT")) {
        command.setCommand(parseInsert());
    } else if (readIf("MERGE")) {
        command.setCommand(parseMerge());
    } else {
        throw getSyntaxError();
    }
    return command;
}
Also used : Query(org.h2.command.dml.Query) Explain(org.h2.command.dml.Explain)

Aggregations

PreparedStatement (java.sql.PreparedStatement)15 ResultSet (java.sql.ResultSet)14 Connection (java.sql.Connection)13 Statement (java.sql.Statement)13 SimpleResultSet (org.h2.tools.SimpleResultSet)9 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)7 Column (org.h2.table.Column)7 StatementBuilder (org.h2.util.StatementBuilder)6 ValueString (org.h2.value.ValueString)6 ArrayList (java.util.ArrayList)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 IgniteException (org.apache.ignite.IgniteException)4 GridH2RowDescriptor (org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor)4 Prepared (org.h2.command.Prepared)4 Expression (org.h2.expression.Expression)4 LinkedHashMap (java.util.LinkedHashMap)3 List (java.util.List)3 GridQueryCacheObjectsIterator (org.apache.ignite.internal.processors.query.GridQueryCacheObjectsIterator)3 UpdatePlan (org.apache.ignite.internal.processors.query.h2.dml.UpdatePlan)3 GridSqlColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn)3