Search in sources :

Example 6 with PlanItem

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

the class TableView method getBestPlanItem.

@Override
public PlanItem getBestPlanItem(Session session, int[] masks, TableFilter[] filters, int filter, SortOrder sortOrder, HashSet<Column> allColumnsSet) {
    final CacheKey cacheKey = new CacheKey(masks, this);
    Map<Object, ViewIndex> indexCache = session.getViewIndexCache(topQuery != null);
    ViewIndex i = indexCache.get(cacheKey);
    if (i == null || i.isExpired()) {
        i = new ViewIndex(this, index, session, masks, filters, filter, sortOrder);
        indexCache.put(cacheKey, i);
    }
    PlanItem item = new PlanItem();
    item.cost = i.getCost(session, masks, filters, filter, sortOrder, allColumnsSet);
    item.setIndex(i);
    return item;
}
Also used : DbObject(org.h2.engine.DbObject) ViewIndex(org.h2.index.ViewIndex)

Example 7 with PlanItem

use of org.h2.table.PlanItem 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 8 with PlanItem

use of org.h2.table.PlanItem 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)

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