diff --git a/fe/fe-core/src/main/java/org/apache/doris/planner/OlapScanNode.java b/fe/fe-core/src/main/java/org/apache/doris/planner/OlapScanNode.java index f1d89b63d51a25..150e79f720da8e 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/planner/OlapScanNode.java +++ b/fe/fe-core/src/main/java/org/apache/doris/planner/OlapScanNode.java @@ -370,9 +370,20 @@ private Collection distributionPrune( DistributionInfo distributionInfo, boolean pruneTablesByNereids) throws AnalysisException { if (pruneTablesByNereids) { - return nereidsPrunedTabletIds.isEmpty() - ? null - : new ArrayList<>(nereidsPrunedTabletIds); + if (nereidsPrunedTabletIds.isEmpty()) { + return null; + } + // Filter to tablets belonging to this partition. Without this, the caller's + // per-partition loop in computeTabletInfo becomes O(partitionNum * globalPrunedSize) + // getTablet hash lookups (most returning null), which dominates plan time + // when both partition count and pruned tablet count are large. + List result = new ArrayList<>(); + for (Long id : tabletIdsInOrder) { + if (nereidsPrunedTabletIds.contains(id)) { + result.add(id); + } + } + return result; } DistributionPruner distributionPruner = null; switch (distributionInfo.getType()) { @@ -918,8 +929,9 @@ private void computeTabletInfo() throws UserException { boolean notExistsSampleAndPrunedTablets = sampleTabletIds.isEmpty() && nereidsPrunedTabletIds.isEmpty(); if (prunedTabletIds != null) { for (Long id : prunedTabletIds) { - if (selectedTable.getTablet(id) != null) { - tablets.add(selectedTable.getTablet(id)); + Tablet tablet = selectedTable.getTablet(id); + if (tablet != null) { + tablets.add(tablet); scanTabletIds.add(id); } else if (notExistsSampleAndPrunedTablets) { // The tabletID specified in query does not exist in this partition, skip scan partition.