Repository: apache/skywalking (language: python)

Issue:
[BanyanDB][OAP] Refactor topology query
There are two issues here:

### Redundant tags
`sourceService` and `destService ` are not used in the final analysis. Entity ID should be enough for decoding.

### Use `groupBy` in the query 

`TopologyDAO` is not using `groupBy` query, so with 30-60 min, we face a huge dataset of the query results.

_Originally posted by @wu-sheng in https://github.com/apache/skywalking/pull/10170#discussion_r1050716464_
      


Discussion / hints:
`Redundant tags` had been fixed.

Fix patch:
diff --git a/docs/en/changes/changes.md b/docs/en/changes/changes.md
index a045c616996e..41e983a1c8b6 100644
--- a/docs/en/changes/changes.md
+++ b/docs/en/changes/changes.md
@@ -57,6 +57,7 @@
 * Remove unnecessary additional columns when storage is in logical sharding mode.
 * The cluster coordinator support watch mechanism for notifying `RemoteClientManager` and `ServerStatusService`.
 * Fix ServiceMeshServiceDispatcher overwrite ServiceDispatcher debug file when open SW_OAL_ENGINE_DEBUG.
+* Use `groupBy` and `in` operators to optimize topology query for BanyanDB storage plugin.
 
 #### UI
 
diff --git a/oap-server-bom/pom.xml b/oap-server-bom/pom.xml
index 91c8f97414e0..90b4b3c4d819 100644
--- a/oap-server-bom/pom.xml
+++ b/oap-server-bom/pom.xml
@@ -73,7 +73,8 @@
         <awaitility.version>3.0.0</awaitility.version>
         <httpcore.version>4.4.13</httpcore.version>
         <commons-compress.version>1.21</commons-compress.version>
-        <banyandb-java-client.version>0.2.1</banyandb-java-client.version>
+        <!-- TODO: use 0.3.0 before OAP 9.4.0 release -->
+        <banyandb-java-client.version>0.3.0-SNAPSHOT</banyandb-java-client.version>
         <kafka-clients.version>2.8.1</kafka-clients.version>
         <spring-kafka-test.version>2.4.6.RELEASE</spring-kafka-test.version>
     </properties>
diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/manual/relation/process/ProcessRelationClientSideMetrics.java b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/manual/relation/process/ProcessRelationClientSideMetrics.java
index 3693f5e68b8f..024cdf1c1991 100644
--- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/manual/relation/process/ProcessRelationClientSideMetrics.java
+++ b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/manual/relation/process/ProcessRelationClientSideMetrics.java
@@ -30,6 +30,7 @@
 import org.apache.skywalking.oap.server.core.source.DefaultScopeDefine;
 import org.apache.skywalking.oap.server.core.storage.ShardingAlgorithm;
 import org.apache.skywalking.oap.server.core.storage.StorageID;
+import org.apache.skywalking.oap.server.core.storage.annotation.BanyanDB;
 import org.apache.skywalking.oap.server.core.storage.annotation.Column;
 import org.apache.skywalking.oap.server.core.storage.annotation.SQLDatabase;
 import org.apache.skywalking.oap.server.core.storage.type.Convert2Entity;
@@ -67,10 +68,12 @@ public class ProcessRelationClientSideMetrics extends Metrics {
     @Setter
     @Getter
     @Column(columnName = ENTITY_ID, length = 512)
+    @BanyanDB.SeriesID(index = 0)
     private String entityId;
     @Setter
     @Getter
     @Column(columnName = COMPONENT_ID, storageOnly = true)
+    @BanyanDB.SeriesID(index = 1)
     private int componentId;
 
     @Override
diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/manual/relation/process/ProcessRelationServerSideMetrics.java b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/manual/relation/process/ProcessRelationServerSideMetrics.java
index 16fbfaa53ab5..eedf9ed3a28c 100644
--- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/manual/relation/process/ProcessRelationServerSideMetrics.java
+++ b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/manual/relation/process/ProcessRelationServerSideMetrics.java
@@ -30,6 +30,7 @@
 import org.apache.skywalking.oap.server.core.source.DefaultScopeDefine;
 import org.apache.skywalking.oap.server.core.storage.ShardingAlgorithm;
 import org.apache.skywalking.oap.server.core.storage.StorageID;
+import org.apache.skywalking.oap.server.core.storage.annotation.BanyanDB;
 import org.apache.skywalking.oap.server.core.storage.annotation.Column;
 import org.apache.skywalking.oap.server.core.storage.annotation.SQLDatabase;
 import org.apache.skywalking.oap.server.core.storage.type.Convert2Entity;
@@ -66,10 +67,12 @@ public class ProcessRelationServerSideMetrics extends Metrics {
     @Setter
     @Getter
     @Column(columnName = ENTITY_ID, length = 512)
+    @BanyanDB.SeriesID(index = 0)
     private String entityId;
     @Setter
     @Getter
     @Column(columnName = COMPONENT_ID, storageOnly = true)
+    @BanyanDB.SeriesID(index = 1)
     private int componentId;
 
     @Override
diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/manual/relation/service/ServiceRelationClientSideMetrics.java b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/manual/relation/service/ServiceRelationClientSideMetrics.java
index 83c40d8ad5bd..0fc9f479d5c2 100644
--- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/manual/relation/service/ServiceRelationClientSideMetrics.java
+++ b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/manual/relation/service/ServiceRelationClientSideMetrics.java
@@ -29,6 +29,7 @@
 import org.apache.skywalking.oap.server.core.source.DefaultScopeDefine;
 import org.apache.skywalking.oap.server.core.storage.ShardingAlgorithm;
 import org.apache.skywalking.oap.server.core.storage.StorageID;
+import org.apache.skywalking.oap.server.core.storage.annotation.BanyanDB;
 import org.apache.skywalking.oap.server.core.storage.annotation.Column;
 import org.apache.skywalking.oap.server.core.storage.annotation.ElasticSearch;
 import org.apache.skywalking.oap.server.core.storage.annotation.SQLDatabase;
@@ -64,10 +65,12 @@ public class ServiceRelationClientSideMetrics extends Metrics {
     @Getter
     @Column(columnName = COMPONENT_IDS, storageOnly = true)
     @ElasticSearch.Keyword
+    @BanyanDB.SeriesID(index = 1)
     private IntList componentIds = new IntList(3);
     @Setter
     @Getter
     @Column(columnName = ENTITY_ID, length = 512)
+    @BanyanDB.SeriesID(index = 0)
     private String entityId;
 
     @Override
diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/manual/relation/service/ServiceRelationServerSideMetrics.java b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/manual/relation/service/ServiceRelationServerSideMetrics.java
index d2752e11d3e8..4414d8cf2516 100644
--- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/manual/relation/service/ServiceRelationServerSideMetrics.java
+++ b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/manual/relation/service/ServiceRelationServerSideMetrics.java
@@ -67,6 +67,7 @@ public class ServiceRelationServerSideMetrics extends Metrics {
     @Getter
     @Column(columnName = COMPONENT_IDS, storageOnly = true)
     @ElasticSearch.Keyword
+    @BanyanDB.SeriesID(index = 1)
     private IntList componentIds = new IntList(3);
     @Setter
     @Getter
diff --git a/oap-server/server-storage-plugin/storage-banyandb-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/banyandb/measure/BanyanDBTopologyQueryDAO.java b/oap-server/server-storage-plugin/storage-banyandb-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/banyandb/measure/BanyanDBTopologyQueryDAO.java
index cc6f313b7a73..c641e4d7484c 100644
--- a/oap-server/server-storage-plugin/storage-banyandb-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/banyandb/measure/BanyanDBTopologyQueryDAO.java
+++ b/oap-server/server-storage-plugin/storage-banyandb-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/banyandb/measure/BanyanDBTopologyQueryDAO.java
@@ -19,12 +19,17 @@
 package org.apache.skywalking.oap.server.storage.plugin.banyandb.measure;
 
 import com.google.common.collect.ImmutableSet;
+
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
+import org.apache.skywalking.banyandb.v1.client.AbstractCriteria;
+import org.apache.skywalking.banyandb.v1.client.DataPoint;
 import org.apache.skywalking.banyandb.v1.client.MeasureQuery;
 import org.apache.skywalking.banyandb.v1.client.MeasureQueryResponse;
 import org.apache.skywalking.banyandb.v1.client.TimestampRange;
@@ -62,9 +67,9 @@ public List<Call.CallDetail> loadServiceRelationsDetectedAtServerSide(Duration d
             throw new UnexpectedException("Service id is empty");
         }
 
-        List<QueryBuilder<MeasureQuery>> queryBuilderList = buildServiceRelationsQueries(serviceIds);
+        QueryBuilder<MeasureQuery> queryBuilder = buildServiceRelationsQuery(serviceIds);
 
-        return queryServiceRelation(duration, queryBuilderList, DetectPoint.SERVER);
+        return queryServiceRelation(duration, queryBuilder, DetectPoint.SERVER);
     }
 
     @Override
@@ -74,43 +79,34 @@ public List<Call.CallDetail> loadServiceRelationDetectedAtClientSide(Duration du
             throw new UnexpectedException("Service id is empty");
         }
 
-        List<QueryBuilder<MeasureQuery>> queryBuilderList = buildServiceRelationsQueries(serviceIds);
+        QueryBuilder<MeasureQuery> queryBuilder = buildServiceRelationsQuery(serviceIds);
 
-        return queryServiceRelation(duration, queryBuilderList, DetectPoint.CLIENT);
+        return queryServiceRelation(duration, queryBuilder, DetectPoint.CLIENT);
     }
 
     @Override
     public List<Call.CallDetail> loadServiceRelationsDetectedAtServerSide(Duration duration) throws IOException {
-        return queryServiceRelation(duration, Collections.singletonList(emptyMeasureQuery()), DetectPoint.SERVER);
+        return queryServiceRelation(duration, emptyMeasureQuery(), DetectPoint.SERVER);
     }
 
     @Override
     public List<Call.CallDetail> loadServiceRelationDetectedAtClientSide(Duration duration) throws IOException {
-        return queryServiceRelation(duration, Collections.singletonList(emptyMeasureQuery()), DetectPoint.CLIENT);
+        return queryServiceRelation(duration, emptyMeasureQuery(), DetectPoint.CLIENT);
     }
 
-    private List<QueryBuilder<MeasureQuery>> buildServiceRelationsQueries(List<String> serviceIds) {
-        List<QueryBuilder<MeasureQuery>> queryBuilderList = new ArrayList<>(serviceIds.size());
-        for (final String serviceId : serviceIds) {
-            queryBuilderList.add(new QueryBuilder<MeasureQuery>() {
-                @Override
-                protected void apply(MeasureQuery query) {
-                    query.and(eq(ServiceRelationServerSideMetrics.SOURCE_SERVICE_ID, serviceId));
-                }
-            });
-
-            queryBuilderList.add(new QueryBuilder<MeasureQuery>() {
-                @Override
-                protected void apply(MeasureQuery query) {
-                    query.and(eq(ServiceRelationServerSideMetrics.DEST_SERVICE_ID, serviceId));
-                }
-            });
-        }
-        return queryBuilderList;
+    private QueryBuilder<MeasureQuery> buildServiceRelationsQuery(List<String> serviceIds) {
+        return new QueryBuilder<MeasureQuery>() {
+            @Override
+            protected void apply(MeasureQuery query) {
+                query.or(in(ServiceRelationServerSideMetrics.SOURCE_SERVICE_ID, serviceIds))
+                        .or(in(ServiceRelationServerSideMetrics.DEST_SERVICE_ID, serviceIds));
+                query.groupBy(Sets.newLinkedHashSet(Arrays.asList(Metrics.ENTITY_ID, ServiceRelationServerSideMetrics.COMPONENT_IDS)));
+            }
+        };
     }
 
     List<Call.CallDetail> queryServiceRelation(Duration duration,
-                                               List<QueryBuilder<MeasureQuery>> queryBuilderList,
+                                               QueryBuilder<MeasureQuery> queryBuilder,
                                                DetectPoint detectPoint) throws IOException {
         long startTB = 0;
         long endTB = 0;
@@ -123,72 +119,77 @@ List<Call.CallDetail> queryServiceRelation(Duration duration,
             timestampRange = new TimestampRange(TimeBucket.getTimestamp(startTB), TimeBucket.getTimestamp(endTB));
         }
         final String modelName = detectPoint == DetectPoint.SERVER ? ServiceRelationServerSideMetrics.INDEX_NAME :
-            ServiceRelationClientSideMetrics.INDEX_NAME;
-        final Map<String, Call.CallDetail> callMap = new HashMap<>();
-        for (final QueryBuilder<MeasureQuery> q : queryBuilderList) {
-            MeasureQueryResponse resp = query(modelName,
-                                              ImmutableSet.of(
-                                                  ServiceRelationClientSideMetrics.COMPONENT_IDS,
-                                                  Metrics.ENTITY_ID
-                                              ),
-                                              Collections.emptySet(), timestampRange, q
-            );
-            if (resp.size() == 0) {
-                continue;
-            }
-            final Call.CallDetail call = new Call.CallDetail();
-            final String entityId = resp.getDataPoints().get(0).getTagValue(Metrics.ENTITY_ID);
+                ServiceRelationClientSideMetrics.INDEX_NAME;
+
+        MeasureQueryResponse resp = query(modelName,
+                ImmutableSet.of(
+                        ServiceRelationClientSideMetrics.COMPONENT_IDS,
+                        Metrics.ENTITY_ID
+                ),
+                Collections.emptySet(), timestampRange, queryBuilder
+        );
+        if (resp.size() == 0) {
+            return Collections.emptyList();
+        }
+        List<Call.CallDetail> calls = new ArrayList<>(resp.size());
+        for (final DataPoint dataPoint : resp.getDataPoints()) {
+            final String entityId = dataPoint.getTagValue(Metrics.ENTITY_ID);
             final IntList componentIds = new IntList(
-                resp.getDataPoints().get(0).getTagValue(ServiceRelationClientSideMetrics.COMPONENT_IDS));
+                    dataPoint.getTagValue(ServiceRelationClientSideMetrics.COMPONENT_IDS));
+            final Call.CallDetail call = new Call.CallDetail();
             for (int i = 0; i < componentIds.size(); i++) {
                 call.buildFromServiceRelation(entityId, componentIds.get(i), detectPoint);
-                callMap.putIfAbsent(entityId, call);
+                calls.add(call);
             }
         }
-        return new ArrayList<>(callMap.values());
+        return calls;
     }
 
     @Override
     public List<Call.CallDetail> loadInstanceRelationDetectedAtServerSide(String clientServiceId,
                                                                           String serverServiceId,
                                                                           Duration duration) throws IOException {
-        List<QueryBuilder<MeasureQuery>> queryBuilderList = buildInstanceRelationsQueries(
-            clientServiceId, serverServiceId);
-        return queryInstanceRelation(duration, queryBuilderList, DetectPoint.SERVER);
+        QueryBuilder<MeasureQuery> queryBuilder = buildInstanceRelationsQuery(
+                clientServiceId, serverServiceId);
+        return queryInstanceRelation(duration, queryBuilder, DetectPoint.SERVER);
     }
 
     @Override
     public List<Call.CallDetail> loadInstanceRelationDetectedAtClientSide(String clientServiceId,
                                                                           String serverServiceId,
                                                                           Duration duration) throws IOException {
-        List<QueryBuilder<MeasureQuery>> queryBuilderList = buildInstanceRelationsQueries(
-            clientServiceId, serverServiceId);
-        return queryInstanceRelation(duration, queryBuilderList, DetectPoint.CLIENT);
+        QueryBuilder<MeasureQuery> queryBuilder = buildInstanceRelationsQuery(
+                clientServiceId, serverServiceId);
+        return queryInstanceRelation(duration, queryBuilder, DetectPoint.CLIENT);
     }
 
-    private List<QueryBuilder<MeasureQuery>> buildInstanceRelationsQueries(String clientServiceId,
-                                                                           String serverServiceId) {
-        List<QueryBuilder<MeasureQuery>> queryBuilderList = new ArrayList<>(2);
-        queryBuilderList.add(new QueryBuilder<MeasureQuery>() {
+    private QueryBuilder<MeasureQuery> buildInstanceRelationsQuery(String clientServiceId,
+                                                                   String serverServiceId) {
+        return new QueryBuilder<MeasureQuery>() {
             @Override
             protected void apply(MeasureQuery query) {
-                query.and(eq(ServiceInstanceRelationServerSideMetrics.SOURCE_SERVICE_ID, clientServiceId))
-                     .and(eq(ServiceInstanceRelationServerSideMetrics.DEST_SERVICE_ID, serverServiceId));
-            }
-        });
+                List<AbstractCriteria> instanceRelationsQueryConditions = new ArrayList<>(2);
 
-        queryBuilderList.add(new QueryBuilder<MeasureQuery>() {
-            @Override
-            protected void apply(MeasureQuery query) {
-                query.and(eq(ServiceInstanceRelationServerSideMetrics.DEST_SERVICE_ID, clientServiceId))
-                     .and(eq(ServiceInstanceRelationServerSideMetrics.SOURCE_SERVICE_ID, serverServiceId));
+                instanceRelationsQueryConditions.add(
+                        // source_service_id = clientServiceId AND dest_service_id = serverServiceId
+                        and(Lists.newArrayList(
+                                eq(ServiceInstanceRelationServerSideMetrics.SOURCE_SERVICE_ID, clientServiceId),
+                                eq(ServiceInstanceRelationServerSideMetrics.DEST_SERVICE_ID, serverServiceId))
+                        ));
+                instanceRelationsQueryConditions.add(
+                        // dest_service_id = clientServiceId AND source_service_id = serverServiceId
+                        and(Lists.newArrayList(
+                                eq(ServiceInstanceRelationServerSideMetrics.DEST_SERVICE_ID, clientServiceId),
+                                eq(ServiceInstanceRelationServerSideMetrics.SOURCE_SERVICE_ID, serverServiceId)
+                        ))
+                );
+                query.criteria(or(instanceRelationsQueryConditions));
             }
-        });
-        return queryBuilderList;
+        };
     }
 
     List<Call.CallDetail> queryInstanceRelation(Duration duration,
-                                                List<QueryBuilder<MeasureQuery>> queryBuilderList,
+                                                QueryBuilder<MeasureQuery> queryBuilder,
                                                 DetectPoint detectPoint) throws IOException {
         long startTB = 0;
         long endTB = 0;
@@ -201,30 +202,32 @@ List<Call.CallDetail> queryInstanceRelation(Duration duration,
             timestampRange = new TimestampRange(TimeBucket.getTimestamp(startTB), TimeBucket.getTimestamp(endTB));
         }
         final String modelName = detectPoint == DetectPoint.SERVER ? ServiceInstanceRelationServerSideMetrics.INDEX_NAME :
-            ServiceInstanceRelationClientSideMetrics.INDEX_NAME;
-        final Map<String, Call.CallDetail> callMap = new HashMap<>();
-        for (final QueryBuilder<MeasureQuery> q : queryBuilderList) {
-            MeasureQueryResponse resp = query(modelName,
-                                              ImmutableSet.of(
-                                                  Metrics.ENTITY_ID
-                                              ),
-                                              Collections.emptySet(), timestampRange, q
-            );
-            if (resp.size() == 0) {
-                continue;
-            }
+                ServiceInstanceRelationClientSideMetrics.INDEX_NAME;
+
+        MeasureQueryResponse resp = query(modelName,
+                ImmutableSet.of(
+                        Metrics.ENTITY_ID
+                ),
+                Collections.emptySet(), timestampRange, queryBuilder
+        );
+        if (resp.size() == 0) {
+            return Collections.emptyList();
+        }
+
+        List<Call.CallDetail> calls = new ArrayList<>(resp.size());
+        for (final DataPoint dataPoint : resp.getDataPoints()) {
             final Call.CallDetail call = new Call.CallDetail();
-            final String entityId = resp.getDataPoints().get(0).getTagValue(Metrics.ENTITY_ID);
+            final String entityId = dataPoint.getTagValue(Metrics.ENTITY_ID);
             call.buildFromInstanceRelation(entityId, detectPoint);
-            callMap.putIfAbsent(entityId, call);
+            calls.add(call);
         }
-        return new ArrayList<>(callMap.values());
+        return calls;
     }
 
     @Override
     public List<Call.CallDetail> loadEndpointRelation(Duration duration, String destEndpointId) throws IOException {
-        List<QueryBuilder<MeasureQuery>> queryBuilderList = buildEndpointRelationsQueries(destEndpointId);
-        return queryEndpointRelation(duration, queryBuilderList, DetectPoint.SERVER);
+        QueryBuilder<MeasureQuery> queryBuilder = buildEndpointRelationsQueries(destEndpointId);
+        return queryEndpointRelation(duration, queryBuilder, DetectPoint.SERVER);
     }
 
     @Override
@@ -239,26 +242,20 @@ public List<Call.CallDetail> loadProcessRelationDetectedAtServerSide(String serv
         return queryProcessRelation(duration, serviceInstanceId, DetectPoint.SERVER);
     }
 
-    private List<QueryBuilder<MeasureQuery>> buildEndpointRelationsQueries(String destEndpointId) {
-        List<QueryBuilder<MeasureQuery>> queryBuilderList = new ArrayList<>(2);
-        queryBuilderList.add(new QueryBuilder<MeasureQuery>() {
+    private QueryBuilder<MeasureQuery> buildEndpointRelationsQueries(String destEndpointId) {
+        return new QueryBuilder<MeasureQuery>() {
             @Override
             protected void apply(MeasureQuery query) {
-                query.and(eq(EndpointRelationServerSideMetrics.SOURCE_ENDPOINT, destEndpointId));
-            }
-        });
+                query.or(eq(EndpointRelationServerSideMetrics.SOURCE_ENDPOINT, destEndpointId))
+                        .or(eq(EndpointRelationServerSideMetrics.DEST_ENDPOINT, destEndpointId));
 
-        queryBuilderList.add(new QueryBuilder<MeasureQuery>() {
-            @Override
-            protected void apply(MeasureQuery query) {
-                query.and(eq(EndpointRelationServerSideMetrics.DEST_ENDPOINT, destEndpointId));
+                query.groupBy(Sets.newHashSet(Metrics.ENTITY_ID));
             }
-        });
-        return queryBuilderList;
+        };
     }
 
     List<Call.CallDetail> queryEndpointRelation(Duration duration,
-                                                List<QueryBuilder<MeasureQuery>> queryBuilderList,
+                                                QueryBuilder<MeasureQuery> queryBuilder,
                                                 DetectPoint detectPoint) throws IOException {
         long startTB = 0;
         long endTB = 0;
@@ -270,23 +267,24 @@ List<Call.CallDetail> queryEndpointRelation(Duration duration,
         if (startTB > 0 && endTB > 0) {
             timestampRange = new TimestampRange(TimeBucket.getTimestamp(startTB), TimeBucket.getTimestamp(endTB));
         }
-        final Map<String, Call.CallDetail> callMap = new HashMap<>();
-        for (final QueryBuilder<MeasureQuery> q : queryBuilderList) {
-            MeasureQueryResponse resp = query(EndpointRelationServerSideMetrics.INDEX_NAME,
-                                              ImmutableSet.of(
-                                                  Metrics.ENTITY_ID
-                                              ),
-                                              Collections.emptySet(), timestampRange, q
-            );
-            if (resp.size() == 0) {
-                continue;
-            }
+
+        MeasureQueryResponse resp = query(EndpointRelationServerSideMetrics.INDEX_NAME,
+                ImmutableSet.of(
+                        Metrics.ENTITY_ID
+                ),
+                Collections.emptySet(), timestampRange, queryBuilder
+        );
+        if (resp.size() == 0) {
+            return Collections.emptyList();
+        }
+        List<Call.CallDetail> resultSet = new ArrayList<>(resp.size());
+        for (final DataPoint dataPoint : resp.getDataPoints()) {
             final Call.CallDetail call = new Call.CallDetail();
-            final String entityId = resp.getDataPoints().get(0).getTagValue(Metrics.ENTITY_ID);
+            final String entityId = dataPoint.getTagValue(Metrics.ENTITY_ID);
             call.buildFromEndpointRelation(entityId, detectPoint);
-            callMap.putIfAbsent(entityId, call);
+            resultSet.add(call);
         }
-        return new ArrayList<>(callMap.values());
+        return resultSet;
     }
 
     List<Call.CallDetail> queryProcessRelation(Duration duration,
@@ -303,25 +301,35 @@ List<Call.CallDetail> queryProcessRelation(Duration duration,
             timestampRange = new TimestampRange(TimeBucket.getTimestamp(startTB), TimeBucket.getTimestamp(endTB));
         }
         final String modelName = detectPoint == DetectPoint.SERVER ? ProcessRelationServerSideMetrics.INDEX_NAME :
-            ProcessRelationClientSideMetrics.INDEX_NAME;
-        final Map<String, Call.CallDetail> callMap = new HashMap<>();
+                ProcessRelationClientSideMetrics.INDEX_NAME;
+
         MeasureQueryResponse resp = query(modelName,
-                                          ImmutableSet.of(
-                                              Metrics.ENTITY_ID, ProcessRelationClientSideMetrics.COMPONENT_ID),
-                                          Collections.emptySet(), timestampRange, new QueryBuilder<MeasureQuery>() {
-                @Override
-                protected void apply(MeasureQuery query) {
-                    query.and(eq(ProcessRelationServerSideMetrics.SERVICE_INSTANCE_ID, serviceInstanceId));
+                ImmutableSet.of(Metrics.ENTITY_ID, ProcessRelationClientSideMetrics.COMPONENT_ID),
+                Collections.emptySet(), timestampRange, new QueryBuilder<MeasureQuery>() {
+                    @Override
+                    protected void apply(MeasureQuery query) {
+                        query.and(eq(ProcessRelationServerSideMetrics.SERVICE_INSTANCE_ID, serviceInstanceId));
+                        query.groupBy(Sets.newLinkedHashSet(Arrays.asList(Metrics.ENTITY_ID, ProcessRelationClientSideMetrics.COMPONENT_ID)));
+                    }
                 }
-            }
         );
-        final Call.CallDetail call = new Call.CallDetail();
-        final String entityId = resp.getDataPoints().get(0).getTagValue(Metrics.ENTITY_ID);
-        final int componentId = ((Number) resp.getDataPoints()
-                                              .get(0)
-                                              .getTagValue(ProcessRelationClientSideMetrics.COMPONENT_ID)).intValue();
-        call.buildProcessRelation(entityId, componentId, detectPoint);
-        callMap.putIfAbsent(entityId, call);
-        return new ArrayList<>(callMap.values());
+
+        if (resp.size() == 0) {
+            return Collections.emptyList();
+        }
+
+        List<Call.CallDetail> calls = new ArrayList<>(resp.size());
+        for (final DataPoint dataPoint : resp.getDataPoints()) {
+            final String entityId = dataPoint.getTagValue(Metrics.ENTITY_ID);
+            final IntList componentIds = new IntList(
+                    dataPoint.getTagValue(ProcessRelationClientSideMetrics.COMPONENT_ID));
+            Call.CallDetail call = new Call.CallDetail();
+            for (int i = 0; i < componentIds.size(); i++) {
+                call.buildFromServiceRelation(entityId, componentIds.get(i), detectPoint);
+                calls.add(call);
+            }
+        }
+
+        return calls;
     }
 }
diff --git a/oap-server/server-storage-plugin/storage-banyandb-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/banyandb/stream/AbstractBanyanDBDAO.java b/oap-server/server-storage-plugin/storage-banyandb-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/banyandb/stream/AbstractBanyanDBDAO.java
index 4978b3f64a8a..62f3facf0c19 100644
--- a/oap-server/server-storage-plugin/storage-banyandb-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/banyandb/stream/AbstractBanyanDBDAO.java
+++ b/oap-server/server-storage-plugin/storage-banyandb-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/banyandb/stream/AbstractBanyanDBDAO.java
@@ -134,6 +134,14 @@ protected PairQueryCondition<String> eq(String name, String value) {
             return PairQueryCondition.StringQueryCondition.eq(name, value);
         }
 
+        protected PairQueryCondition<List<String>> in(String name, List<String> values) {
+            return PairQueryCondition.StringArrayQueryCondition.in(name, values);
+        }
+
+        protected PairQueryCondition<List<String>> notIn(String name, List<String> values) {
+            return PairQueryCondition.StringArrayQueryCondition.in(name, values);
+        }
+
         protected PairQueryCondition<Long> ne(String name, long value) {
             return PairQueryCondition.LongQueryCondition.ne(name, value);
         }


Test patch:
diff --git a/test/e2e-v2/script/env b/test/e2e-v2/script/env
index 241b59264af6..cbb66b5aa32c 100644
--- a/test/e2e-v2/script/env
+++ b/test/e2e-v2/script/env
@@ -23,6 +23,6 @@ SW_AGENT_CLIENT_JS_COMMIT=af0565a67d382b683c1dbd94c379b7080db61449
 SW_AGENT_CLIENT_JS_TEST_COMMIT=4f1eb1dcdbde3ec4a38534bf01dded4ab5d2f016
 SW_KUBERNETES_COMMIT_SHA=b670c41d94a82ddefcf466d54bab5c492d88d772
 SW_ROVER_COMMIT=d956eaede57b62108b78bca48045bd09ba88e653
-SW_BANYANDB_COMMIT=005b02210caacee0141de8085edebed367ef5a6f
+SW_BANYANDB_COMMIT=e7b08bea242e76c68950509529339995ac0646df
 
 SW_CTL_COMMIT=e684fae0107045fc23799146d62f04cb68bd5a3b