From c375f90df6f719be58bde761a79604d73061240a Mon Sep 17 00:00:00 2001 From: Tglman Date: Mon, 21 Aug 2023 19:02:46 +0100 Subject: [PATCH] fix: optimization to avoid load record from indexes that do not match the target class --- .../sql/executor/OSelectExecutionPlanner.java | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/com/orientechnologies/orient/core/sql/executor/OSelectExecutionPlanner.java b/core/src/main/java/com/orientechnologies/orient/core/sql/executor/OSelectExecutionPlanner.java index 75420f97a0b..61e98407dd5 100755 --- a/core/src/main/java/com/orientechnologies/orient/core/sql/executor/OSelectExecutionPlanner.java +++ b/core/src/main/java/com/orientechnologies/orient/core/sql/executor/OSelectExecutionPlanner.java @@ -1975,6 +1975,22 @@ && handleClassWithIndexForSortOnly( plan.chain(fetcher); } + private int[] classClustersFiltered( + ODatabaseSession db, OClass clazz, Set filterClusters) { + int[] ids = clazz.getClusterIds(); + List filtered = new ArrayList<>(); + for (int id : ids) { + if (filterClusters.contains(db.getClusterNameById(id))) { + filtered.add(id); + } + } + int[] result = new int[filtered.size()]; + for (int i = 0; i < filtered.size(); i++) { + result[i] = filtered.get(i); + } + return result; + } + private boolean handleClassAsTargetWithIndexedFunction( OSelectExecutionPlan plan, Set filterClusters, @@ -2025,8 +2041,9 @@ private boolean handleClassAsTargetWithIndexedFunction( subPlan.chain(step); int[] filterClusterIds = null; if (filterClusters != null) { - filterClusterIds = - ((ODatabaseDocumentInternal) ctx.getDatabase()).getClustersIds(filterClusters); + filterClusterIds = classClustersFiltered(ctx.getDatabase(), clazz, filterClusters); + } else { + filterClusterIds = clazz.getClusterIds(); } subPlan.chain(new GetValueFromIndexEntryStep(ctx, filterClusterIds, profilingEnabled)); if (requiresMultipleIndexLookups(bestIndex.getKeyCondition()) @@ -2265,8 +2282,9 @@ private boolean handleClassWithIndexForSortOnly( idx, orderType.equals(OOrderByItem.ASC), ctx, profilingEnabled)); int[] filterClusterIds = null; if (filterClusters != null) { - filterClusterIds = - ((ODatabaseDocumentInternal) ctx.getDatabase()).getClustersIds(filterClusters); + filterClusterIds = classClustersFiltered(ctx.getDatabase(), clazz, filterClusters); + } else { + filterClusterIds = clazz.getClusterIds(); } plan.chain(new GetValueFromIndexEntryStep(ctx, filterClusterIds, profilingEnabled)); if (info.serverToClusters.size() == 1) { @@ -2458,8 +2476,9 @@ private List handleClassAsTargetWithIndex( profilingEnabled)); int[] filterClusterIds = null; if (filterClusters != null) { - filterClusterIds = - ((ODatabaseDocumentInternal) ctx.getDatabase()).getClustersIds(filterClusters); + filterClusterIds = classClustersFiltered(ctx.getDatabase(), clazz, filterClusters); + } else { + filterClusterIds = clazz.getClusterIds(); } result.add(new GetValueFromIndexEntryStep(ctx, filterClusterIds, profilingEnabled)); if (requiresMultipleIndexLookups(desc.getKeyCondition()) || duplicateResultsForRecord(desc)) {