Skip to content

Commit

Permalink
[improvement](mtmv) Only Generate rewritten plan when generate mv pl…
Browse files Browse the repository at this point in the history
…an for performance (#39541)

Before query rewrite by materialized view, we collecet the table which
query used by method org.apache.doris.mtmv.MTMVCache#from.

In MTMVCache#from we calcute the cost of plan which is useless for
collecting table.
So add boolean needCost param in method MTMVCache#from to identify
that if need cost of plan or not for performance.
  • Loading branch information
seawinde authored and dataroaring committed Aug 26, 2024
1 parent 11820aa commit e6cdbaa
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
4 changes: 2 additions & 2 deletions fe/fe-core/src/main/java/org/apache/doris/catalog/MTMV.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public void addTaskResult(MTMVTask task, MTMVRelation relation,
this.relation = relation;
if (!Env.isCheckpointThread() && !Config.enable_check_compatibility_mode) {
try {
this.cache = MTMVCache.from(this, MTMVPlanUtil.createMTMVContext(this));
this.cache = MTMVCache.from(this, MTMVPlanUtil.createMTMVContext(this), true);
} catch (Throwable e) {
this.cache = null;
LOG.warn("generate cache failed", e);
Expand Down Expand Up @@ -272,7 +272,7 @@ public MTMVCache getOrGenerateCache(ConnectContext connectionContext) throws Ana
writeMvLock();
try {
if (cache == null) {
this.cache = MTMVCache.from(this, connectionContext);
this.cache = MTMVCache.from(this, connectionContext, true);
}
} finally {
writeMvUnlock();
Expand Down
13 changes: 10 additions & 3 deletions fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public StructInfo getStructInfo() {
return structInfo;
}

public static MTMVCache from(MTMV mtmv, ConnectContext connectContext) {
public static MTMVCache from(MTMV mtmv, ConnectContext connectContext, boolean needCost) {
LogicalPlan unboundMvPlan = new NereidsParser().parseSingle(mtmv.getQuerySql());
StatementContext mvSqlStatementContext = new StatementContext(connectContext,
new OriginStatement(mtmv.getQuerySql(), 0));
Expand All @@ -89,7 +89,13 @@ public static MTMVCache from(MTMV mtmv, ConnectContext connectContext) {
}
// Can not convert to table sink, because use the same column from different table when self join
// the out slot is wrong
planner.planWithLock(unboundMvPlan, PhysicalProperties.ANY, ExplainLevel.ALL_PLAN);
if (needCost) {
// Only in mv rewrite, we need plan with eliminated cost which is used for mv chosen
planner.planWithLock(unboundMvPlan, PhysicalProperties.ANY, ExplainLevel.ALL_PLAN);
} else {
// No need cost for performance
planner.planWithLock(unboundMvPlan, PhysicalProperties.ANY, ExplainLevel.REWRITTEN_PLAN);
}
Plan originPlan = planner.getCascadesContext().getRewritePlan();
// Eliminate result sink because sink operator is useless in query rewrite by materialized view
// and the top sort can also be removed
Expand All @@ -111,7 +117,8 @@ public Plan visitLogicalResultSink(LogicalResultSink<? extends Plan> logicalResu
Optional<StructInfo> structInfoOptional = MaterializationContext.constructStructInfo(mvPlan, originPlan,
planner.getCascadesContext(),
new BitSet());
return new MTMVCache(mvPlan, originPlan, planner.getCascadesContext().getMemo().getRoot().getStatistics(),
return new MTMVCache(mvPlan, originPlan, needCost
? planner.getCascadesContext().getMemo().getRoot().getStatistics() : null,
structInfoOptional.orElseGet(() -> null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private void expandMvAndCollect(MTMV mtmv, TableCollectorContext context) {
}
// Make sure use only one connection context when in query to avoid ConnectionContext.get() wrong
MTMVCache expandedMv = MTMVCache.from(mtmv, context.getConnectContext() == null
? MTMVPlanUtil.createMTMVContext(mtmv) : context.getConnectContext());
? MTMVPlanUtil.createMTMVContext(mtmv) : context.getConnectContext(), false);
expandedMv.getLogicalPlan().accept(this, context);
}

Expand Down

0 comments on commit e6cdbaa

Please sign in to comment.