yujun777 commented on code in PR #68141:
URL: https://github.com/apache/doris/pull/68141#discussion_r4056391252


##########
fe/fe-core/src/main/java/org/apache/doris/catalog/MTMV.java:
##########
@@ -497,51 +500,43 @@ public Set<TableNameInfo> 
getQueryRewriteConsistencyRelaxedTables() {
      */
     public MTMVCache getOrGenerateCache(ConnectContext connectionContext) 
throws
             org.apache.doris.nereids.exceptions.AnalysisException {
-        // store two MTMVCaches: one is a cache where SessionVariables differ 
from those at creation time,
-        // and the MTMV plan includes a guardexpr;
-        // the other is a cache where SessionVariables are the same as at 
creation time, and the MTMV plan
-        // does not include a guardexpr;
-        // This way, when sessionVariables are the same, rewriting is possible;
-        // When sessionVariables are different, there are two cases:
-        // 1. If a guardexpr is present, rewriting is not possible;
-        // 2. If no guardexpr is present, rewriting is possible.
-        // Determine if current session variables match MV creation session 
variables
         Map<String, String> currentSessionVars =
                 
connectionContext.getSessionVariable().getAffectQueryResultInPlanVariables();
         boolean sessionVarsMatch = 
SessionVarGuardRewriter.checkSessionVariablesMatch(
                 currentSessionVars, this.sessionVariables);
+        boolean guarded = !sessionVarsMatch;
+        MTMVCacheManager manager = Env.getCurrentEnv().getMtmvCacheManager();
 
         while (true) {
             long cacheGeneration;
-            // Select appropriate cache based on session variable match
+            MTMVCache cached;
             readMvLock();
             try {
-                MTMVCache cache = getCache(sessionVarsMatch);
-                if (cache != null) {
-                    return cache;
-                }
+                cached = manager.getIfPresent(this.id, guarded);
                 cacheGeneration = rewriteCacheGeneration;
             } finally {
                 readMvUnlock();
             }
-
-            // Generate cache if not exists
-            // Concurrent situations may result in duplicate cache generation,
-            // but we tolerate this in order to prevent nested use of readLock 
and write MvLock for the table
-            MTMVCache mtmvCache = createRewriteCache(connectionContext, false, 
!sessionVarsMatch);
-            writeMvLock();
+            if (cached != null) {
+                return cached;
+            }
+            MTMVCache generated = createRewriteCache(connectionContext, false, 
guarded);
+            readMvLock();

Review Comment:
   **[P2] 避免在禁用缓存时重复构建计划**
   
   当 `mtmv_cache_manage_num = 0` 时,`MTMVCacheManager` 使用 `maximumSize(0)`,每次 
`put()` 都会立即丢弃缓存。但这里每次 cache miss 仍会执行完整的 
`createRewriteCache()`。因此同一个查询中的多个优化路径可能反复构建相同的 MTMV rewrite plan,导致 FE CPU 
和查询延迟增加。
   
   既然配置说明支持 `0` 表示禁用缓存,建议在一次规划流程内复用已生成的 plan,避免重复构建;或者直接禁止将 
`mtmv_cache_manage_num` 设置为 `0`。建议补充 `maximumSize = 0` 时的 plan 构建次数测试。



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to