Search in sources :

Example 1 with ExecutionPlan

use of org.apache.jackrabbit.oak.query.plan.ExecutionPlan in project jackrabbit-oak by apache.

the class QueryImpl method prepare.

@Override
public void prepare() {
    if (prepared) {
        return;
    }
    prepared = true;
    List<SourceImpl> sources = source.getInnerJoinSelectors();
    List<JoinConditionImpl> conditions = source.getInnerJoinConditions();
    if (sources.size() <= 1) {
        // simple case (no join)
        estimatedCost = source.prepare().getEstimatedCost();
        isSortedByIndex = canSortByIndex();
        return;
    }
    // use a greedy algorithm
    SourceImpl result = null;
    Set<SourceImpl> available = new HashSet<SourceImpl>();
    // the query is only slow if all possible join orders are slow
    // (in theory, due to using the greedy algorithm, a query might be considered
    // slow even thought there is a plan that doesn't need to use traversal, but
    // only for 3-way and higher joins, and only if traversal is considered very fast)
    boolean isPotentiallySlowJoin = true;
    while (sources.size() > 0) {
        int bestIndex = 0;
        double bestCost = Double.POSITIVE_INFINITY;
        ExecutionPlan bestPlan = null;
        SourceImpl best = null;
        for (int i = 0; i < sources.size(); i++) {
            SourceImpl test = buildJoin(result, sources.get(i), conditions);
            if (test == null) {
                // no join condition
                continue;
            }
            ExecutionPlan testPlan = test.prepare();
            double cost = testPlan.getEstimatedCost();
            if (best == null || cost < bestCost) {
                bestPlan = testPlan;
                bestCost = cost;
                bestIndex = i;
                best = test;
            }
            if (!potentiallySlowTraversalQuery) {
                isPotentiallySlowJoin = false;
            }
            test.unprepare();
        }
        available.add(sources.remove(bestIndex));
        result = best;
        best.prepare(bestPlan);
    }
    potentiallySlowTraversalQuery = isPotentiallySlowJoin;
    estimatedCost = result.prepare().getEstimatedCost();
    source = result;
    isSortedByIndex = canSortByIndex();
}
Also used : ExecutionPlan(org.apache.jackrabbit.oak.query.plan.ExecutionPlan) SelectorExecutionPlan(org.apache.jackrabbit.oak.query.plan.SelectorExecutionPlan) SourceImpl(org.apache.jackrabbit.oak.query.ast.SourceImpl) ChildNodeJoinConditionImpl(org.apache.jackrabbit.oak.query.ast.ChildNodeJoinConditionImpl) JoinConditionImpl(org.apache.jackrabbit.oak.query.ast.JoinConditionImpl) EquiJoinConditionImpl(org.apache.jackrabbit.oak.query.ast.EquiJoinConditionImpl) DescendantNodeJoinConditionImpl(org.apache.jackrabbit.oak.query.ast.DescendantNodeJoinConditionImpl) SameNodeJoinConditionImpl(org.apache.jackrabbit.oak.query.ast.SameNodeJoinConditionImpl) HashSet(java.util.HashSet)

Example 2 with ExecutionPlan

use of org.apache.jackrabbit.oak.query.plan.ExecutionPlan in project jackrabbit-oak by apache.

the class JoinImpl method prepare.

@Override
public ExecutionPlan prepare() {
    if (plan != null) {
        return plan;
    }
    applyJoinConditions();
    // the estimated cost is the cost of the left selector,
    // plus twice the cost of the right selector (we expect
    // two rows for the right selector for each node 
    // on the left selector)
    ExecutionPlan leftPlan = left.prepare();
    ExecutionPlan rightPlan = right.prepare();
    double cost = leftPlan.getEstimatedCost() + 2 * rightPlan.getEstimatedCost();
    plan = new JoinExecutionPlan(this, leftPlan, rightPlan, cost);
    return plan;
}
Also used : JoinExecutionPlan(org.apache.jackrabbit.oak.query.plan.JoinExecutionPlan) JoinExecutionPlan(org.apache.jackrabbit.oak.query.plan.JoinExecutionPlan) ExecutionPlan(org.apache.jackrabbit.oak.query.plan.ExecutionPlan)

Aggregations

ExecutionPlan (org.apache.jackrabbit.oak.query.plan.ExecutionPlan)2 HashSet (java.util.HashSet)1 ChildNodeJoinConditionImpl (org.apache.jackrabbit.oak.query.ast.ChildNodeJoinConditionImpl)1 DescendantNodeJoinConditionImpl (org.apache.jackrabbit.oak.query.ast.DescendantNodeJoinConditionImpl)1 EquiJoinConditionImpl (org.apache.jackrabbit.oak.query.ast.EquiJoinConditionImpl)1 JoinConditionImpl (org.apache.jackrabbit.oak.query.ast.JoinConditionImpl)1 SameNodeJoinConditionImpl (org.apache.jackrabbit.oak.query.ast.SameNodeJoinConditionImpl)1 SourceImpl (org.apache.jackrabbit.oak.query.ast.SourceImpl)1 JoinExecutionPlan (org.apache.jackrabbit.oak.query.plan.JoinExecutionPlan)1 SelectorExecutionPlan (org.apache.jackrabbit.oak.query.plan.SelectorExecutionPlan)1