From e6cdbaa08e9a3eb826ba38ca45fcc2c21478c432 Mon Sep 17 00:00:00 2001 From: seawinde <149132972+seawinde@users.noreply.github.com> Date: Tue, 20 Aug 2024 20:16:51 +0800 Subject: [PATCH] [improvement](mtmv) Only Generate rewritten plan when generate mv plan 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. --- .../main/java/org/apache/doris/catalog/MTMV.java | 4 ++-- .../main/java/org/apache/doris/mtmv/MTMVCache.java | 13 ++++++++++--- .../nereids/trees/plans/visitor/TableCollector.java | 2 +- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/MTMV.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/MTMV.java index 1611951e64792c..c3d36ea39714c0 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/MTMV.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/MTMV.java @@ -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); @@ -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(); diff --git a/fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVCache.java b/fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVCache.java index aceb453c2c32d0..56061c75b9cee2 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVCache.java +++ b/fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVCache.java @@ -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)); @@ -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 @@ -111,7 +117,8 @@ public Plan visitLogicalResultSink(LogicalResultSink logicalResu Optional 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)); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/TableCollector.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/TableCollector.java index 5ab6b7ef015a17..2e2cdb810f0f72 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/TableCollector.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/TableCollector.java @@ -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); }