private int doNestedIterations(int level, SelectResults results, ExecutionContext context,
      boolean evaluateWhereClause, int numElementsInResult)
      throws TypeMismatchException, FunctionDomainException, NameResolutionException,
      QueryInvocationTargetException, CompiledSelect.NullIteratorException {
    List iterList = context.getCurrentIterators();
    if (level == iterList.size()) {
      boolean addToResults = true;
      if (evaluateWhereClause) {
        Object result = whereClause.evaluate(context);
        QueryObserver observer = QueryObserverHolder.getInstance();
        observer.afterIterationEvaluation(result);
        if (result == null) {
          addToResults = false;
        } else if (result instanceof Boolean) {
          addToResults = (Boolean) result;
        } else if (result == QueryService.UNDEFINED) {
          // add UNDEFINED to results only for NOT EQUALS queries
          if (whereClause.getType() == COMPARISON) {
            int operator = ((Filter) whereClause).getOperator();
            if ((operator != TOK_NE && operator != TOK_NE_ALT)) {
              addToResults = false;
            }
          } else {
            addToResults = false;
          }
        } else {
          throw new TypeMismatchException(
              String.format("The WHERE clause was type ' %s ' instead of boolean",
                  result.getClass().getName()));
        }
      }
      if (addToResults) {
        int occurrence =
            applyProjectionAndAddToResultSet(context, results, orderByAttrs == null);
        // If the occurrence is greater than 1, then only in case of
        // non distinct query should it be treated as contributing to size
        // else duplication will be eliminated when making it distinct using
        // ResultsCollectionWrapper and we will fall short of limit
        if (occurrence == 1 || (occurrence > 1 && !distinct)) {
          // (Unique i.e first time occurrence) or subsequent occurrence
          // for non distinct query
          ++numElementsInResult;
        }
      }
    } else {
      RuntimeIterator rIter = (RuntimeIterator) iterList.get(level);
      SelectResults sr = rIter.evaluateCollection(context);
      if (sr == null) {
        return 0; // continue iteration if a collection evaluates to UNDEFINED
      }

      // Check if its a non-distinct count(*) query without where clause, in that case,
      // we can size directly on the region.
      if (whereClause == null && iterators.size() == 1 && isCount() && !isDistinct()
          && sr instanceof QRegion) {
        QRegion qr = (QRegion) sr;
        countStartQueryResult = qr.getRegion().size();
        return 1;
      }

      // #44807: select * query should not deserialize objects
      // In case of "select *" queries we can keep the results in serialized
      // form and send it to the client.
      if (context.getQuery() != null && ((DefaultQuery) context.getQuery()).isKeepSerialized()
          && sr instanceof QRegion) {
        ((QRegion) sr).setKeepSerialized(true);
      }

      // Iterate through the data set.
      for (Object aSr : sr) {
        // Check if query execution on this thread is canceled.
        QueryMonitor.throwExceptionIfQueryOnCurrentThreadIsCanceled();

        rIter.setCurrent(aSr);
        QueryObserver observer = QueryObserverHolder.getInstance();
        observer.beforeIterationEvaluation(rIter, aSr);
        numElementsInResult = doNestedIterations(level + 1, results, context, evaluateWhereClause,
            numElementsInResult);
        Integer limitValue = evaluateLimitValue(context, limit);
        if (orderByAttrs == null && limitValue > -1 && numElementsInResult == limitValue) {
          break;
        }
      }
    }
    return numElementsInResult;
  }
Check for the bulk-get pattern and if it applies do an optimized execution. The pattern is:
SELECT ?? FROM <Region>.entrySet e WHERE e.key IN <Collection>.

@return a List of entries if optimization was executed, or null if it wasn't because the
        optimization pattern didn't match
geode-core/src/main/java/org/apache/geode/cache/query/internal/CompiledSelect.java