diff --git a/paimon-core/src/main/java/org/apache/paimon/table/source/snapshot/SnapshotReaderImpl.java b/paimon-core/src/main/java/org/apache/paimon/table/source/snapshot/SnapshotReaderImpl.java index a6710ff00848..00f7e6e9577e 100644 --- a/paimon-core/src/main/java/org/apache/paimon/table/source/snapshot/SnapshotReaderImpl.java +++ b/paimon-core/src/main/java/org/apache/paimon/table/source/snapshot/SnapshotReaderImpl.java @@ -526,8 +526,19 @@ private Plan toIncrementalPlan( totalBuckets = beforeEntries.get(0).totalBuckets(); } - // deduplicate - beforeEntries.removeIf(dataEntries::remove); + // deduplicate: remove entries common to both lists + // Use HashSet for O(n+m) instead of O(n*m) with List.remove() + Set afterSet = new HashSet<>(dataEntries); + Set commonEntries = new HashSet<>(); + beforeEntries.removeIf( + entry -> { + if (afterSet.contains(entry)) { + commonEntries.add(entry); + return true; + } + return false; + }); + dataEntries.removeAll(commonEntries); List before = beforeEntries.stream()