[feat](Nereids): Put the Child with Least Row Count in the First Position of Intersect (#34290) (#35339)

In this pull request, we optimize the ordering of children in the Intersect operator to improve query performance. The proposed change is to place the child with the least row count in the first position of the Intersect operator.

The rationale behind this optimization is that the Intersect operator works by first evaluating the leftmost child and then iterating through the results of the other children to find matching rows. By placing the child with the least row count first, we can minimize the number of iterations required to find the matching rows, thereby reducing the overall execution time of the query.
This commit is contained in:
谢健
2024-05-27 11:52:35 +08:00
committed by GitHub
parent a9bd98d65b
commit af986c370b
14 changed files with 198 additions and 40 deletions

View File

@ -35,6 +35,7 @@ import org.apache.doris.nereids.trees.plans.physical.PhysicalFileScan;
import org.apache.doris.nereids.trees.plans.physical.PhysicalGenerate;
import org.apache.doris.nereids.trees.plans.physical.PhysicalHashAggregate;
import org.apache.doris.nereids.trees.plans.physical.PhysicalHashJoin;
import org.apache.doris.nereids.trees.plans.physical.PhysicalIntersect;
import org.apache.doris.nereids.trees.plans.physical.PhysicalJdbcScan;
import org.apache.doris.nereids.trees.plans.physical.PhysicalNestedLoopJoin;
import org.apache.doris.nereids.trees.plans.physical.PhysicalOdbcScan;
@ -384,6 +385,16 @@ class CostModelV1 extends PlanVisitor<Cost, PlanContext> {
);
}
@Override
public Cost visitPhysicalIntersect(PhysicalIntersect physicalIntersect, PlanContext context) {
double cpuCost = 0.0;
for (int i = 0; i < physicalIntersect.children().size(); i++) {
cpuCost += context.getChildStatistics(i).getRowCount();
}
double memoryCost = context.getChildStatistics(0).computeSize();
return CostV1.of(context.getSessionVariable(), cpuCost, memoryCost, 0);
}
@Override
public Cost visitPhysicalGenerate(PhysicalGenerate<? extends Plan> generate, PlanContext context) {
Statistics statistics = context.getStatisticsWithCheck();

View File

@ -17,6 +17,7 @@
package org.apache.doris.nereids.rules;
import org.apache.doris.nereids.rules.exploration.IntersectReorder;
import org.apache.doris.nereids.rules.exploration.MergeProjectsCBO;
import org.apache.doris.nereids.rules.exploration.TransposeAggSemiJoin;
import org.apache.doris.nereids.rules.exploration.TransposeAggSemiJoinProject;
@ -123,6 +124,7 @@ public class RuleSet {
public static final List<Rule> EXPLORATION_RULES = planRuleFactories()
.add(new MergeProjectsCBO())
.add(IntersectReorder.INSTANCE)
.build();
public static final List<Rule> OTHER_REORDER_RULES = planRuleFactories()

View File

@ -334,6 +334,7 @@ public enum RuleType {
BUILD_AGG_FOR_RANDOM_DISTRIBUTED_TABLE_FILTER_SCAN(RuleTypeClass.REWRITE),
BUILD_AGG_FOR_RANDOM_DISTRIBUTED_TABLE_AGG_SCAN(RuleTypeClass.REWRITE),
// exploration rules
REORDER_INTERSECT(RuleTypeClass.EXPLORATION),
TEST_EXPLORATION(RuleTypeClass.EXPLORATION),
OR_EXPANSION(RuleTypeClass.EXPLORATION),
LOGICAL_JOIN_COMMUTE(RuleTypeClass.EXPLORATION),

View File

@ -0,0 +1,69 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.doris.nereids.rules.exploration;
import org.apache.doris.nereids.rules.Rule;
import org.apache.doris.nereids.rules.RuleType;
import org.apache.doris.nereids.trees.expressions.SlotReference;
import org.apache.doris.nereids.trees.plans.GroupPlan;
import org.apache.doris.nereids.trees.plans.Plan;
import org.apache.doris.nereids.trees.plans.algebra.SetOperation.Qualifier;
import com.google.common.collect.Lists;
import java.util.List;
/**
* this rule put the child with least row count to the first child of intersect op
*/
public class IntersectReorder extends OneExplorationRuleFactory {
public static final IntersectReorder INSTANCE = new IntersectReorder();
@Override
public Rule build() {
return logicalIntersect()
.when(logicalIntersect -> logicalIntersect.getQualifier().equals(Qualifier.DISTINCT))
.then(logicalIntersect -> {
int minChildIdx = 0;
double minRowCount = Double.MAX_VALUE;
for (int i = 0; i < logicalIntersect.children().size(); i++) {
GroupPlan child = (GroupPlan) logicalIntersect.child(i);
if (child.getGroup().getStatistics().getRowCount() < minRowCount) {
minChildIdx = i;
minRowCount = child.getGroup().getStatistics().getRowCount();
}
}
if (minChildIdx == 0) {
return null;
}
List<Plan> children = Lists.newArrayList(logicalIntersect.children());
List<List<SlotReference>> regularOutput =
Lists.newArrayList(logicalIntersect.getRegularChildrenOutputs());
children.set(0, logicalIntersect.child(minChildIdx));
children.set(minChildIdx, logicalIntersect.child(0));
if (regularOutput.isEmpty()) {
return logicalIntersect.withChildren(children);
}
regularOutput.set(0, logicalIntersect.getRegularChildOutput(minChildIdx));
regularOutput.set(minChildIdx, logicalIntersect.getRegularChildOutput(0));
return logicalIntersect.withChildrenAndTheirOutputs(children, regularOutput);
})
.toRule(RuleType.REORDER_INTERSECT);
}
}